无法从智能设备应用程序的 SQLite 数据库检索记录
我将 SQLite.NET 与 C#[ Visual Studio 2008] 一起使用,并添加了“System.Data.SQLite.dll”作为参考。以下代码与 Windows 应用程序一起使用,用于将数据从我的数据库文件提取到数据网格中。
private void SetConnection()
{
sql_con = new SQLiteConnection("Data Source=emp.db;Version=3;");
}
public DataTable LoadData()
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "select * from employeedetails";
transaction=sql_con.BeginTransaction();
DA = new SQLiteDataAdapter(CommandText, sql_con);
DS.Reset();
DA.Fill(DS);
DT = DS.Tables[0];
transaction.Commit();
sql_con.Close();
return DT;
}
调用:
private void Form1_Load(object sender, EventArgs e)
{
EmpTable obj = new EmpTable();
this.dataGridView1.DataSource = obj.LoadData();
}
相同的代码不适用于 C#“智能设备应用程序”。它显示了一个例外:没有这样的表“employeedetails”。
对于智能设备应用程序,我从“SQLite/CompactFramework”文件夹中添加了 System.Data.SQLite.dll。
请帮我解决这个问题。
提前致谢。
I am using SQLite.NET with C#[ visual studio 2008] and added the 'System.Data.SQLite.dll' as reference. The following code works with Windows Application for fetching data from my database file into a datagrid.
private void SetConnection()
{
sql_con = new SQLiteConnection("Data Source=emp.db;Version=3;");
}
public DataTable LoadData()
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "select * from employeedetails";
transaction=sql_con.BeginTransaction();
DA = new SQLiteDataAdapter(CommandText, sql_con);
DS.Reset();
DA.Fill(DS);
DT = DS.Tables[0];
transaction.Commit();
sql_con.Close();
return DT;
}
Invoking in:
private void Form1_Load(object sender, EventArgs e)
{
EmpTable obj = new EmpTable();
this.dataGridView1.DataSource = obj.LoadData();
}
The same code not working with C# 'Smart Device Application'. It shows an exception: no such table 'employeedetails'.
For Smart Device Application I have added the System.Data.SQLite.dll from "SQLite/CompactFramework" folder.
Please help me to resolve this.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定这是否是导致您问题的原因,但我们过去在移动设备和 sqlite 方面遇到过问题,这解决了这个问题:
对于使用紧凑框架的应用程序,您需要包含 SQLite.Interop.xxx项目中的 .DLL(除了常规的 System.Data.SQLite.DLL 之外)。确保将文件设置为如果较新则复制或始终复制。
请记住,您无法设置对互操作 dll 的引用。您必须通过将其添加为文件来将其包含在项目中。
有关详细信息,请查看此网站下的分发 SQLite 引擎和 ADO.NET 程序集< /em> 部分。
I'm not sure if this is what is causing your issue, but we've had issues in the past with mobile devices and sqlite where this solved it:
For an application using the compact framework, you need to include SQLite.Interop.xxx.DLL in your project (in addition to the regular System.Data.SQLite.DLL). Make sure you set the file to copy if newer or copy always.
Keep in mind that you can't set a reference to the interop dll. You must include it in the project by adding it as a file.
For more information check this website under the Distributing The SQLite Engine and ADO.NET Assembly section.
谢谢大家,
我已经解决了问题。正如“siyw”所说,问题是应用程序找不到“db 文件”。
对于 Windows 应用程序,
db 文件必须位于生成 .exe 文件的调试文件夹中。这样,就不需要指定DB文件的路径。
对于智能设备应用程序,
DB 文件将在设备的根文件夹中生成。如果我们用 exe 方式绑定 db 文件,它不会找到它,并且会在根文件夹中创建一个新数据库。
为了避免这种情况,在打包 CAB 文件时,我们必须创建自定义文件夹 [ 例如: DB文件夹]并将db文件放入该文件夹中。
将连接字符串中的路径设置为,
现在,应用程序找到了数据库。
Thank You Guys,
I've resolved the Problem. As 'siyw' said, the problem was the application could not find the "db file".
For windows application,
The db file has to be located in debug folder where the .exe file is generated. So that, no need to specify the path of the DB file.
For Smart Device Application,
The DB file will be generated in the root folder of the device. If we bind the db file with exe means, it wont find it and it will create a new database in root folder.
To avoid that, while crating CAB file we have to create custom folder [ ex. DBfolder ] and put the db file in the folder.
Set the path in the connection String as,
Now, the db is found by the Application.
我认为您需要在此处指定完整路径,并确保其正确。如果路径指向不存在的文件,它将在那里创建一个新的 emp.db 数据库文件。这个新文件显然是空的,没有任何表,因此它会给出一个
no such table 'employeedetails'
异常,就像您查询它时得到的那样。I think you need to specify the full path here, and make sure it's correct. If the path is to a non-existent file, it will create a new emp.db database file there instead. This new file will obviously be empty and without any tables, hence it would give a
no such table 'employeedetails'
exception such as you're getting if you query it.