【问题标题】:Desktop application deploy — attempt to write a readonly database sqlite桌面应用程序部署 — 尝试编写只读数据库 sqlite
【发布时间】:2015-10-10 16:25:07
【问题描述】:

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

谢谢..

这是一些代码片段。

//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% 工作环代码。但它只会在我创建应用程序的设置时产生错误

【问题讨论】:

  • 在您的设置应用程序中,您是否正在执行任何创建命令。如果这是一个现有的 SqlLite 文件,您尝试部署它的方式可能存在问题。您可以显示使用代码吗?您能否逐步检查安装程序代码.. 看看您是否可以查明和/或重新创建错误..?
  • 没有。我只使用 INSERTSELECT 命令..
  • 您必须在插入或选择命令方面做一些事情,您能否为这两个 CRUD 函数显示一些代码...?
  • 我会尽快给你看...
  • 我已将代码片段添加到您可以检查的问题中......

标签: c# sqlite desktop-application


【解决方案1】:

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

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

-- 重要--

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

【讨论】:

  • 我遇到了同样的潜在问题,但我没有将数据库文件移动到不同的文件夹,而是通过运行试图以管理员身份写入数据库的可执行文件来绕过它跨度>
  • @JoeIrby 你是对的。我后来发现了这一点,现在每次我都必须像这样使用数据库。我总是让 exe 以管理员身份运行,因此 DB 文件具有正确的读写权限。
  • @Awais 能否提供新的连接字符串
  • @priyankas 您可以做两件事,1:让您的应用程序以管理员身份运行(管理员访问权限),然后您可以将您的 db 文件放在项目目录中的任何位置,它会完美运行。 2:只需将db文件放入BIN文件夹并使用像这样的连接字符串“Data Source=YourDatabase.db;Version=3;”。
【解决方案2】:

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

我使用 Advanced Installer 创建 Windows 安装程序设置文件。

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

【讨论】:

    【解决方案3】:

    将您的 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();   
    

    【讨论】:

      【解决方案4】:

      (可能有点晚了……)

      我刚刚解决了这个问题。

      应用程序可以任意读写其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}")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-27
        • 2011-01-20
        • 2014-12-07
        • 1970-01-01
        • 1970-01-01
        • 2021-09-11
        • 2023-03-09
        • 2015-05-30
        相关资源
        最近更新 更多