Sqlite + c#:无法打开数据库文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10875612/
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
Sqlite + c#: Unable to open the database file
提问by Futureman
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 to sql_cmd.ExecuteNonQuery, Sqlexception is "Unable to open the database file". "lodeDb.db" file on my online hosting, i think data source is wrong. Can U tell me if db file in online hosting. How to set datasourse for connection Thanks you Note: Permission file is no problem in here
当我运行到 sql_cmd.ExecuteNonQuery 时,Sqlexception 是“无法打开数据库文件”。我的在线主机上的“lodeDb.db”文件,我认为数据源是错误的。你能告诉我 db 文件是否在在线托管中。如何为连接设置数据源 谢谢注意:这里的权限文件没问题
回答by Ahmed Ghoneim
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 :
使用 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 :
Connecting to remote database differs, you must check the following.
编辑 1:
连接到远程数据库不同,您必须检查以下内容。
- Firewall port permissibility.
- Company/Host providing database is allowing remote connection.
- 防火墙端口许可。
- 提供数据库的公司/主机允许远程连接。
回答by Bhanuprakash Reddy
My problem is solved when add "Journal Mode=Off;" in connection string
添加“Journal Mode=Off;”时我的问题解决了 在连接字符串中
Disable the Journal File This one disables the rollback journal entirely.
禁用日志文件 这将完全禁用回滚日志。
Data Source=c:\mydb.db;Version=3;Journal Mode=Off;
数据源=c:\mydb.db;Version=3;Journal Mode=Off;
回答by gajo357
I got the same exception when trying to open databases that are on the network drive (path starts with "\\myServer\myDbFile...") and I resolved it by putting trueto the parseViaFrameworkparameter in connection constructor.
我在尝试打开网络驱动器上的数据库时遇到了同样的异常(路径以“\\myServer\myDbFile...”开头),我通过将其放入连接构造函数中true的parseViaFramework参数来解决它。
sql_con = new SQLiteConnection(a, true);
回答by user2543087
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”时它不起作用
回答by CenGokhan
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);
}

