在 SQLite 中打开连接

发布于 2024-12-05 11:14:00 字数 980 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

树深时见影 2024-12-12 11:14:01

将数据库放在构建输出的调试文件夹中并不意味着该文件最终会出现在目标设备上。您需要将数据库复制到目标设备(有多种机制,如 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.

凡间太子 2024-12-12 11:14:01

您的数据库文件很可能不在正确的文件夹中。尝试给出这一行:

myConnection = new SQLiteConnection(@"Data Source=Stock.s3db; Version=3;");

数据库的完整路径。

Most likely your database file is not in the correct folder. Try giving this line:

myConnection = new SQLiteConnection(@"Data Source=Stock.s3db; Version=3;");

the full path to your database.

凉城 2024-12-12 11:14:00

使用连接字符串生成器确保您获得格式正确的字符串

SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
builder.FailIfMissing = true;
builder.DataSource = "Insert the fully qualified path to your sqlite db";
SQLiteConnection connection = new SQLiteConnection(builder.ConnectionString);
connection.Open();

Use the connection string builder to ensure you get a correctly formatted string

SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
builder.FailIfMissing = true;
builder.DataSource = "Insert the fully qualified path to your sqlite db";
SQLiteConnection connection = new SQLiteConnection(builder.ConnectionString);
connection.Open();
红颜悴 2024-12-12 11:14:00

如果您查看“调试/发布”文件夹,您可能会发现为您创建的空数据库文件,因为您指定的文件不存在。 'main' 是 sqlite 的标准数据库名称。

将您的数据库复制到所述文件夹中进行测试,或在连接字符串中指定完整路径。

尝试此查询以确定您正在使用的文件所在的位置。

PRAGMA database_list

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.

PRAGMA database_list
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文