由于 App.config 文件中的错误,SQLite 无法加载数据
SQLite can't load data because of an error in App.config file
我在 SQLite 中有一个名为 MiDonations.db 的数据库,在应用程序的目录中有一个 table 名为 tblDonationsList ,这个 table 是用一些通用数据手动填充的。当我尝试填充 gridcontrol 时,它 returns null.
更新:删除 try 和 catch 后,它现在显示一条消息 "configuration system failed to initialize"
这是我在 DataAccessLayer 中使用的代码 class
public DataTable Select(string selectStatment, string parameterName = "", byte[] x = null,string id="Default")
{
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings[id].ConnectionString))
using (var da = new SqlDataAdapter(selectStatment, con))
using (var dt = new DataTable())
{
if (parameterName != "")
{
da.SelectCommand.Parameters.Add(parameterName, SqlDbType.VarBinary).Value = x;
}
da.Fill(dt);
return dt;
}
}
}
连接字符串
<connectionStrings>
<add name="Default" connectionString="Data Source=.\MiDonations.db; Version=3;" providerName="System.Data.SqlClient" />
获取数据的代码
// Get data and cast it to datatable
var dt = new DataAccessLayer().Select($"SELECT * FROM tblDonationsList ");
// Set datasource in datagridview to datatable
gridControl1.DataSource = dt;
重置 App.config 文件后,我能够修复 "configuration system failed to initialize" 但我遇到了另一个问题,即我的应用程序无法找到数据库,经过一番搜索后我发现问题出在DataAccessLayer Class 并通过更改
修复它
public DataTable Select(string selectStatment, string parameterName = "", byte[] x = null,string id="Default")
{
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings[id].ConnectionString))
using (var da = new SqlDataAdapter(selectStatment, con))
using (var dt = new DataTable())
{
if (parameterName != "")
{
da.SelectCommand.Parameters.Add(parameterName, SqlDbType.VarBinary).Value = x;
}
da.Fill(dt);
return dt;
}
}
至
public DataTable Select(string selectStatment, string parameterName = "", byte[] x = null)
{
using (var con = new SQLiteConnection(ConfigurationManager.ConnectionStrings["default"].ConnectionString))
//using (var con = new SqlConnection(@"Data Source=.\MiDonations.db; Version=3;"))
using (var da = new SQLiteDataAdapter(selectStatment, con))
using (var dt = new DataTable())
{
if (parameterName != "")
{
da.SelectCommand.Parameters.Add(parameterName, DbType.String).Value = x;
}
da.Fill(dt);
return dt;
}
}
你试过这个吗:
new SQLiteConnection("Default")
我不太记得了,但是对于 SQLConnection,我认为它在传递给构造函数时使用了连接字符串的名称而不是整个连接字符串。
我在 SQLite 中有一个名为 MiDonations.db 的数据库,在应用程序的目录中有一个 table 名为 tblDonationsList ,这个 table 是用一些通用数据手动填充的。当我尝试填充 gridcontrol 时,它 returns null.
更新:删除 try 和 catch 后,它现在显示一条消息 "configuration system failed to initialize"
这是我在 DataAccessLayer 中使用的代码 class
public DataTable Select(string selectStatment, string parameterName = "", byte[] x = null,string id="Default")
{
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings[id].ConnectionString))
using (var da = new SqlDataAdapter(selectStatment, con))
using (var dt = new DataTable())
{
if (parameterName != "")
{
da.SelectCommand.Parameters.Add(parameterName, SqlDbType.VarBinary).Value = x;
}
da.Fill(dt);
return dt;
}
}
}
连接字符串
<connectionStrings>
<add name="Default" connectionString="Data Source=.\MiDonations.db; Version=3;" providerName="System.Data.SqlClient" />
获取数据的代码
// Get data and cast it to datatable
var dt = new DataAccessLayer().Select($"SELECT * FROM tblDonationsList ");
// Set datasource in datagridview to datatable
gridControl1.DataSource = dt;
重置 App.config 文件后,我能够修复 "configuration system failed to initialize" 但我遇到了另一个问题,即我的应用程序无法找到数据库,经过一番搜索后我发现问题出在DataAccessLayer Class 并通过更改
修复它 public DataTable Select(string selectStatment, string parameterName = "", byte[] x = null,string id="Default")
{
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings[id].ConnectionString))
using (var da = new SqlDataAdapter(selectStatment, con))
using (var dt = new DataTable())
{
if (parameterName != "")
{
da.SelectCommand.Parameters.Add(parameterName, SqlDbType.VarBinary).Value = x;
}
da.Fill(dt);
return dt;
}
}
至
public DataTable Select(string selectStatment, string parameterName = "", byte[] x = null)
{
using (var con = new SQLiteConnection(ConfigurationManager.ConnectionStrings["default"].ConnectionString))
//using (var con = new SqlConnection(@"Data Source=.\MiDonations.db; Version=3;"))
using (var da = new SQLiteDataAdapter(selectStatment, con))
using (var dt = new DataTable())
{
if (parameterName != "")
{
da.SelectCommand.Parameters.Add(parameterName, DbType.String).Value = x;
}
da.Fill(dt);
return dt;
}
}
你试过这个吗:
new SQLiteConnection("Default")
我不太记得了,但是对于 SQLConnection,我认为它在传递给构造函数时使用了连接字符串的名称而不是整个连接字符串。