在 SQLite 中打开连接
我正在尝试在 Windows ce 5.1.17 中使用 SQLite。 我在项目的调试文件夹中使用 SQLite Administrator 创建 SQLite 数据库。 然后我将 System.Data.SQLite.dll 和 SQLite.Interop.065.dll 添加到我的项目中。 我正在尝试以下代码来连接到数据库。
SQLiteConnection myConnection = null;
myConnection = new SQLiteConnection(@"Data Source=" + System.IO.Path.Combine(System.IO.Path. GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), Stock.s3db)+ "; Version=3; Cache Size =100");
myConnection.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = "select * from shops;";
cmd.CommandType = CommandType.Text;
cmd.Connection = myConnection;
cmd.ExecuteReader();// I get exception no such table: shops
另外,当我检查 myConnection 数据库属性时,它显示“Database =“main””。我不知道为什么数据库名称是“main”,但在 myConnection 中我将数据源设置为 Stock.s3db。
I am trying to use SQLite in Windows ce 5.1.17.
I create a SQLite database using SQLite Administrator in debug folder of my project.
Then I add System.Data.SQLite.dll and SQLite.Interop.065.dll to my project.
I am trying the following code to connect to database.
SQLiteConnection myConnection = null;
myConnection = new SQLiteConnection(@"Data Source=" + System.IO.Path.Combine(System.IO.Path. GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), Stock.s3db)+ "; Version=3; Cache Size =100");
myConnection.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = "select * from shops;";
cmd.CommandType = CommandType.Text;
cmd.Connection = myConnection;
cmd.ExecuteReader();// I get exception no such table: shops
Also when I check myConnection database property it shows 'Database = "main"'. I do not know why database name is 'main', but in myConnection I set datasource to Stock.s3db.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将数据库放在构建输出的调试文件夹中并不意味着该文件最终会出现在目标设备上。您需要将数据库复制到目标设备(有多种机制,如 USB 磁盘、远程文件查看器、将其作为内容项添加到项目中等)。
我还倾向于使用数据库的完全限定路径,因为 CE 没有任何工作目录的概念。
Putting the database in the debug folder of the build output does not mean the file will end up on the target device. You need to copy the database over to your target device (there are several mechanisms like a USB disk, Remote File Viewer, adding it as a Content item to your project, etc.).
I'd also be inclined to use a fully qualified path to the database, as CE doesn't have any concept of a working directory.
您的数据库文件很可能不在正确的文件夹中。尝试给出这一行:
数据库的完整路径。
Most likely your database file is not in the correct folder. Try giving this line:
the full path to your database.
使用连接字符串生成器确保您获得格式正确的字符串
Use the connection string builder to ensure you get a correctly formatted string
如果您查看“调试/发布”文件夹,您可能会发现为您创建的空数据库文件,因为您指定的文件不存在。 'main' 是 sqlite 的标准数据库名称。
将您的数据库复制到所述文件夹中进行测试,或在连接字符串中指定完整路径。
尝试此查询以确定您正在使用的文件所在的位置。
If you look inside your Debug/Release folder, you'll probably find the empty database files created for you since the file you specify does not exist. 'main' is the standard db name with sqlite.
Copy your db in said folder for testing, or specifiy the full path in the connection string.
try this query to determine where the file you are using is located.