桌面应用程序部署 - 尝试编写只读数据库sqlite

时间:2015-10-10 16:25:07

标签: c# sqlite desktop-application

我使用C#VS2013和SQLite数据库创建了一个小型桌面[ WinForm ]数据插入应用程序。它工作正常和所有CURD操作。但是当我使用Advance Installer创建此应用程序的安装程序时。然后每当我运行应用程序并尝试插入数据时,它会弹出此屏幕

This is The Error I Got.

谢谢..

以下是部分代码段。

//Add Property Function.
string ConnectionString = "Data Source=database/MyProperty.db;Version=3;Read Only=False";

    public static long AddPropertyToDatabae( Property property )
    {
        SQLiteConnection con = new SQLiteConnection( ConnectionString );
        SQLiteCommand cmd = new SQLiteCommand
        {
            Connection = con,
            CommandText =
                "INSERT INTO Properties (PropertyName,PropertyAddress,PropertyCity,PropertyState,PropertyZip,PropertyNotes)" +
                " values (@Name,@Address,@City,@State,@Zip,@Notes)"
        };
        cmd.Parameters.AddWithValue( "@Name", property.PropertyName );
        cmd.Parameters.AddWithValue( "@Address", property.PropertyAddress );
        cmd.Parameters.AddWithValue( "@City", property.PropertyCity );
        cmd.Parameters.AddWithValue( "@State", property.PropertyState );
        cmd.Parameters.AddWithValue( "@Zip", property.PropertyZip );
        cmd.Parameters.AddWithValue( "@Notes", property.PropertyNotes );

        con.Open();
        cmd.ExecuteNonQuery();

        // Get the Last Inserted RowId.
        cmd.CommandText = "select last_insert_rowid()";
        long rowid = ( long )cmd.ExecuteScalar();

        con.Close();
        return rowid;
    }

注意

这是VS上的100%工作代码。但它只在我创建应用程序的设置时生成错误

4 个答案:

答案 0 :(得分:2)

技术上错误不在SQLite数据库中。但这是文件夹错误。我正在创建Sub文件夹对于数据库文件,它没有作为Local Output文件夹的权限。所以我只是将我的数据库移动到它工作的主 BIN 文件夹。: - )

这是我找到解决方案的链接。 SQLite for Windows Runtime is returning an "ReadOnly" error SQLiteException object

- 重要 -

如果您遇到类似问题,请尝试将数据库文件的目录更改为主输出目录..

答案 1 :(得分:0)

将您的Sql命令更改为以下内容,这样您就不必运行2个单独的sql命令,如果2个人同时点击该代码并插入,则无法保证您将获得正确的最后插入行。我需要查看安装程序代码,看看那里会发生什么......

"INSERT INTO Properties (PropertyName,PropertyAddress,PropertyCity,PropertyState,PropertyZip,PropertyNotes)" +
  " values (@Name,@Address,@City,@State,@Zip,@Notes)SELECT CAST(scope_identity() AS int)";
rowid = (int)cmd.ExecuteScalar();   

答案 2 :(得分:0)

(也许有点晚...)

我刚刚解决了这个问题。

应用程序可以任意读写其LocalFolder,因此您只需要将数据文件复制到该文件夹​​中即可。

代码类似于:

StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("your-file-path"));
await file.CopyAsync(localFolder, "any name", NameCollisionOption.FailIfExists);

然后您可以通过以下方式创建SQLiteConnection:

string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "abc.db");
SQLiteConnection connection = new SQLiteConnection($"Data Source={dbpath}")

答案 3 :(得分:0)

<块引用>

在创建 Windows 安装程序安装文件时:设置应用程序文件夹路径:[WindowsVolume][ProductName]

我使用高级安装程序来创建 Windows 安装程序安装文件。

enter image description here

如果您尝试设置路径 [ProgramFilesFolder][Manufacturer][ProductName] 那么您将无法在 Sqlite 中写入数据,但如果您以管理员身份运行应用程序,则 Sqlite 只读问题将得到解决。