简体   繁体   English

在C#winforms应用程序中使用SQLite-一些基本问题?

[英]Using SQLite with C# winforms application - a few basic questions?

Would be great to get some direction re trying to use sqlite for my WinForms application I'm building in VS2008. 尝试将sqlite用于我在VS2008中构建的WinForms应用程序时,获得一些指导非常有用。

  1. Installation - Is it just drop the "System.Data.SQLite.DLL" file into some folder in my VS2008 project (eg create a folder for it), and then create a "reference" to it? 安装-是否只是将“ System.Data.SQLite.DLL”文件拖放到我的VS2008项目中的某个文件夹中(例如,为其创建一个文件夹),然后为其创建“引用”? I've made the property of the reference CopyGlobal = TRUE. 我已经创建了引用CopyGlobal = TRUE的属性。 Is the idea that when I deploy my application this should work (eg deploy the DLL for the application) 是一个想法,当我部署我的应用程序时,它应该可以工作(例如,为应用程序部署DLL)

  2. Initial Database - Do I have to create an initial database or not? 初始数据库-是否必须创建一个初始数据库? I see the below mentioned code in the Help file but what is the DB it actually connects to and where would the DB file be? 我在帮助文件中看到了下面提到的代码,但是它实际连接到的数据库是什么,该数据库文件在哪里?

     DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite"); using (DbConnection cnn = fact.CreateConnection()) { cnn.ConnectionString = "Data Source=test.db3"; cnn.Open(); } 
  3. What methods to use - Is this typically how I would use/make calls? 使用什么方法-这通常是我使用/拨打电话的方式吗?

     DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite"); using (DbConnection myconnection = fact.CreateConnection()) { myconnection.ConnectionString = "Data Source=test.db3"; myconnection.Open(); SQLiteTransaction mytransaction = SQLiteTransaction)myconnection.BeginTransaction(); SQLiteCommand mycommand = new SQLiteCommand((SQLiteConnection)myconnection); mycommand.CommandText = "SELECT * FROM SYSTEM"; mycommand.ExecuteNonQuery(); mytransaction.Commit(); myconnection.Close(); } 
  4. How would I setup the database tables? 我将如何设置数据库表? Would I do this and store it in my VS2008 project as a template? 我会这样做并将其作为模板存储在VS2008项目中吗? Or would I want to automatic the creation of the database in code if it wasn't there? 还是如果代码不在数据库中,我是否想自动创建数据库?

  5. If the idea from 4 is to setup tables prior, where would I store this initial database file? 如果4中的想法是事先设置表,那么该初始数据库文件应存储在哪里? such that when I run the project to test it and then I use the database file, the one I'm testing with gets scrapped afterwards. 这样,当我运行项目进行测试然后使用数据库文件时,我随后测试的那个文件便会被废弃。 I guess I'm asking how to ensure I have a separate blank but configured (with tables) database as "source" in my VS2008 project, but then when I run/debug it, it would take a copy of this for use in testing? 我想我想问的是如何确保我的VS2008项目中有一个单独的空白但已配置(带有表)的数据库作为“源”,但是当我运行/调试它时,它将获得一份副本以用于测试?

Thanks 谢谢

  1. Yes. 是。 SQLite is an xcopy-deployment database; SQLite是一个xcopy部署数据库; there's no registration and your app only needs the dll to use it. 没有注册,您的应用程序只需要dll即可使用。

  2. The database is specified in the connection string. 在连接字符串中指定数据库。 In this particular case, it's in the test.db3 file in the application working folder. 在这种情况下,它位于应用程序工作文件夹中的test.db3文件中。

  3. You might want to cache the connection to the database to avoid the expensive operation of opening it every time you need to access it. 您可能希望缓存与数据库的连接,以避免每次需要访问它时都要进行昂贵的打开操作。 Also, I am not sure why you need a transaction, since all you do is just read from the database. 另外,我不确定为什么需要事务,因为您所做的只是从数据库中读取。

  4. You have two choices - either store the empty database with the precreated schema and copy it to the output dir during your build process; 您有两种选择-将空数据库与预先创建的架构存储在一起,然后在构建过程中将其复制到输出目录中;或者 or create a set of SQL scripts to execute against the database from your code on the first connection to the database. 或创建一组SQL脚本以在您第一次连接到数据库时从您的代码对数据库执行。 Which one you choose depends on whether you want your application to be responsible for creating the schema or your build process. 选择哪一个取决于您是希望应用程序负责创建架构还是构建过程。

  5. If you create an empty database with precreated schema, you can add it as a file along your sources and instruct VS to copy it to the output dir (as I already mentioned). 如果使用预先创建的模式创建一个空数据库,则可以将其作为文件沿源添加,并指示VS将其复制到输出目录(如上所述)。

I almost always use using so: 我几乎总是使用so:

using (DbConnection conn = new SQLiteConnection(...)) {
   using (DbTransaction tran = conn.BeginTransaction()) {
      using (DbCommand comm = conn.CreateCommand()) {
          ...
      }
      tran.Commit();
   }
   conn.Close();
}

To make sqlite perform you have to use transactions for insert, update and delete and you have to use paramaterized queries. 为了使sqlite执行,您必须使用事务进行插入,更新和删除,并且必须使用参数化查询。 Concatenated queries are a lot slower: How do I get around the "'" problem in sqlite and c#? 串联查询要慢很多: 如何解决sqlite和C#中的“'”问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM