如何将SQLite .db添加到C#.NET项目 [英] How to add SQLite .db into C#.NET project

查看:44
本文介绍了如何将SQLite .db添加到C#.NET项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习C#,目前正在尝试创建一个简单的程序,该程序从SQLite数据库读取1行数据.

I just started to learn C#, and currently trying to create a simple program that read 1 row of data from SQLite database.

我已经成功创建了该程序,但是我仍然必须引用具有完整地址的.db文件,例如:

I've successfully created the program, but i still have to refer to the .db file with full address like :

SQLiteConnection("Data Source=C:/Users/../SQLiteStudio/dbs/mk_ii.db; Version=3;")

但是这样,当我创建安装程序时,.db就不会包括在内.我想使它像将图像添加到我的解决方案资源管理器中时一样,我可以使用短地址,例如:

But this way, the .db won't be included when i create an installer. I want to make it something like when i add an image to my Solution Explorer, i can use short address like :

res/img/logo.png  --i also have copy of .db file in res/ folder

那么,有什么方法可以将.db文件包含到我的项目中?我试图读取当前目录并使用| DataDirectory |,但是所有这些都引用.exe文件的活动文件夹,并且在我调试时不起作用,因为在调试.exe文件时,其目录不同于项目目录.

So, is there any way to include the .db file to my project? I've tried to read current directory and using |DataDirectory|, but all of those referred to the .exe file's active folder, and didn't work when i debugging since when debugging the .exe file have different directory than the project directory.

请注意,我使用的是VS Community 2015和System.Data.SQLite.我也曾尝试将.db文件添加到解决方案资源管理器中,但似乎无法通过它访问.

For a note, i'm using VS Community 2015 and System.Data.SQLite. Also i've tried added the .db file to Solution Explorer, but i can't seem to access through it.

推荐答案

将数据库文件包含在项目中.将类型设置为内容",以确保将文件复制到您的输出目录.在连接字符串中使用相对路径.

Include your database file in your project. Set the type to Content, that will make sure the file is copied to your output directory. Use relative path in connection string.

这篇关于如何将SQLite .db添加到C#.NET项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆