vb.net System.Data.SQLite.SQLiteException 尝试在 Windows 8 上编写只读数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20764269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:14:43  来源:igfitidea点击:

System.Data.SQLite.SQLiteException Attempt to write a read-only database on Windows 8

c#databasevb.netsqlitewindows-8.1

提问by cMinor

I developed an applicaiton that works fine <= Windows 7. It uses SQLlite database with VB and C#. However on Windows 8(Never had this specific problem on other windows os) there is an issue when trying to write to database

我开发了一个运行良好 <= Windows 7 的应用程序。它使用带有 VB 和 C# 的 SQLlite 数据库。但是,在Windows 8 上(在其他 Windows 操作系统上从未遇到过此特定问题)尝试写入数据库时​​出现问题

System.Data.SQLite.SQLiteException: Attempt to write a read-only database

I created database file on windows 8 pc like:

我在 Windows 8 pc 上创建了数据库文件,例如:

 Try
     If (Not File.Exists(System.Environment.CurrentDirectory & "\MY_DB.db")) Then
                Dim conn = New SQLite.SQLiteConnection("Version=3;New=True;Compress=False;Read Only=False;Data Source=" & System.Environment.CurrentDirectory & "\MY_DB.db")
                conn.Open()
                '...DO STUFF  
                conn.Close()
                conn.Dispose()
                conn = Nothing
     End If

 Catch ex As Exception
            'Throw ex
            Return False
 End Try

But didn't work.

但没有奏效。

So basically I have tried:

所以基本上我已经尝试过:

  1. Added Read Only=Falsewhen creating db file but didn't work.
  2. The folder the database resides in must have write permissions, as well as the actual database file. So did it, didn't work (on windows 7 would look like enter image description here).
  1. Read Only=False在创建 db 文件时添加但不起作用。
  2. 数据库所在的文件夹必须具有写入权限,以及实际的数据库文件。这样做了,没有用(在 Windows 7 上看起来像在此处输入图片说明)。

What can I do to make databse writable ?

我该怎么做才能使数据库可写?

采纳答案by Steve

I don't know what is the current directory at the instant of your call, but I would be sure to put my database in a folder where every user have read/write permission by design.

我不知道您通话时的当前目录是什么,但我一定会将我的数据库放在每个用户都设计有读/写权限的文件夹中。

Why don't use the folders represented by the Environment.SpecialFolderenum?

为什么不使用Environment.SpecialFolder枚举表示的文件夹?

You could change your code to something like this

你可以把你的代码改成这样

 Try
      Dim appFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
      appFolder = Path.Combine(appFolder, "MyAppDatabaseFolder")
      If Not Directory.Exists(appFolder) Then
           Directory.CreateDirectory(appFolder)
      End If
      Dim myDB = Path.Combine(appFolder, "MY_DB.db")
      If (Not File.Exists(myDB)) Then
          .....
      End If
 Catch ex As Exception
      'Throw ex
      Return False
 End Try