如何从c#连接到SQLite db文件?

Nic*_*ick 11 c# database sqlite ado.net connection-string

我正在尝试使用ac#application连接到sqllite数据库.我之前从未使用过SQLLite.

var connectionString = @"data source='C:\TestData\StressData.s3db'";
            connection = new SQLiteConnection(connectionString);
            connection.Open();
Run Code Online (Sandbox Code Playgroud)

当我尝试打开连接时,我得到以下异常:

System.NotSupportedException: The given path's format is not supported.
   at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
   at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

谢谢..

缺口

更新:

我按照建议将"数据源"更改为"DataSource".现在我收到一个新错误:

更改此内容后,我收到一个新错误:System.ArgumentException:Data Source不能为空.使用:memory:在System.Data.SQLite.SQLiteConnection.Open()中打开内存数据库

还有什么建议吗?

Nic*_*ick 13

得到它了..

"data source=c:\TestData\StressData.s3db; Version=3;"
Run Code Online (Sandbox Code Playgroud)

看起来"版本"属性不是可选的.有趣的是.NET提供程序没有在设计器属性窗口中显示它.

  • 使用SQLiteConnectionStringBuilder对象来构建连接字符串(而不是将其硬编码为字符串)消除了大多数这样的问题(模糊的参数名称,拼写错误).说到Version属性,至少我的SQLite不需要它. (3认同)