简体   繁体   English

无法打开数据库文件

[英]Unable to open the database file

private void SetConnection()
{
     string a = string.Format(@"Data Source={0};Version=3;New=False;Compress=True;", "~/lodeDb.db");
     sql_con = new SQLiteConnection(a);
}

private void ExecuteQuery(string txtQuery)
{
     SetConnection();
     sql_con.Open();
     sql_cmd = sql_con.CreateCommand();
     sql_cmd.CommandText = txtQuery;
     sql_cmd.ExecuteNonQuery();
     sql_con.Close();
}

When I run sql_cmd.ExecuteNonQuery , Sqlexception is:当我运行sql_cmd.ExecuteNonQuery时,Sqlexception 是:

"Unable to open the database file". “无法打开数据库文件”。

"lodeDb.db" file on my hosting, I think data source is wrong.我主机上的“lodeDb.db”文件,我认为数据源有误。 If database file in online hosting, how to set datasourse for connection?如果是在线托管的数据库文件,如何设置连接的datasource? Permission file is no problem here.权限文件在这里没有问题。

尝试打开网络驱动器上的数据库(路径以“\\\\ myServer \\ myDbFile ...”开头)时,我得到了同样的异常,我通过将true到连接构造函数中的parseViaFramework参数来解决它。

sql_con = new SQLiteConnection(a, true);

It's a Connection String Issue, 这是一个连接字符串问题,

SQL Lite Connection Strings Formats SQL Lite连接字符串格式

Basic : 基础:

Data Source=filename;Version=3;

Using UTF16 : 使用UTF16:

Data Source=filename;Version=3;UseUTF16Encoding=True;

With password : 使用密码:

Data Source=filename;Version=3;Password=myPassword;

Using the pre 3.3x database format : 使用pre 3.3x数据库格式:

Data Source=filename;Version=3;Legacy Format=True;

With connection pooling : 使用连接池:

Data Source=filename;Version=3;Pooling=False;Max Pool Size=100;

Read only connection : 只读连接:

Data Source=filename;Version=3;Read Only=True;

EDIT 1 : 编辑1:
Connecting to remote database differs, you must check the following. 连接到远程数据库不同,您必须检查以下内容。

  1. Firewall port permissibility. 防火墙端口允许。
  2. Company/Host providing database is allowing remote connection. 公司/主机提供数据库允许远程连接。

My problem is solved when add "Journal Mode=Off;" 添加“Journal Mode = Off”时我的问题解决了。 in connection string 在连接字符串中

Disable the Journal File This one disables the rollback journal entirely. 禁用日志文件此文件完全禁用回滚日志。

Data Source=c:\\mydb.db;Version=3;Journal Mode=Off; 数据源= c:\\ mydb.db;版本= 3;日志模式=关闭;

I had the same problem and fixed it using query string like: @"Data Source=C:\\ProgramData\\proj\\lodeDb.db;Version=3;FailIfMissing=False" 我遇到了同样的问题并使用查询字符串修复它:@“Data Source = C:\\ ProgramData \\ proj \\ lodeDb.db; Version = 3; FailIfMissing = False”

By that, I meant that when I use the full path for the location of the DB file, it works, when I use "~/lodeDb.db" it does not work 通过这个,我的意思是,当我使用完整路径的DB文件的位置,它工作,当我使用“〜/ lodeDb.db”它不起作用

This solution worked for me: 这个解决方案对我有用:

var index = dialog.FileName.IndexOf('\\'); example: "C:\TEST.DB"
if (0 != index) {
    Models.Context.CreateDatabase(dialog.FileName);
    OpenProject(dialog.FileName);
}
else //example: "\\Ceng\Share\MyPC"
{
    var path = dialog.FileName.Replace('\\', '/');
    Models.Context.CreateDatabase(path);
    OpenProject(path);
}

I have resolved this issue by setting up System.Data.SQLite dll=>properties=>Copy local=>"True"我通过设置 System.Data.SQLite dll=>properties=>Copy local=>"True" 解决了这个问题

I was facing same issue on the shared hosting server, My C# code was able to read data from SQLIte db file.我在共享托管服务器上遇到了同样的问题,我的 C# 代码能够从 SQLIte db 文件中读取数据。 But while add / update data it was throwing Error "unable to open database"但是在添加/更新数据时抛出错误“无法打开数据库”

I tried many options suggested on stackoverflow But after referring https://stackoverflow.com/a/17780808/2021073 and https://www.sqlite.org/pragma.html#pragma_journal_mode I tried adding journal mode=Off;我尝试了 stackoverflow 上建议的许多选项但是在参考https://stackoverflow.com/a/17780808/2021073https://www.sqlite.org/pragma.html#pragma_journal_mode 之后我尝试添加journal mode=Off; to the Connection string到连接字符串

it worked for me它对我有用

sample code示例代码

SQLiteConnection connection = new SQLiteConnection("Data Source=G:\dbfolder\sqlite3.db;Version=3;Mode=ReadWrite;journal mode=Off;", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM