在C# WinForms应用程序中使用SQLite - 几个基本问题?

5

希望在使用VS2008构建WinForms应用程序时,能够提供一些关于尝试使用sqlite的方向。

  1. Installation - Is it just drop the "System.Data.SQLite.DLL" file into some folder in my VS2008 project (e.g. create a folder for it), and then create a "reference" to it? I've made the property of the reference CopyGlobal = TRUE. Is the idea that when I deploy my application this should work (e.g. deploy the DLL for the application)

  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? 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? 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?

谢谢


不确定为什么,但无法使Q3代码显示得好看。 - Greg
2个回答

4
  1. 是的,SQLite是一种容易部署的数据库;不需要注册,你的应用程序只需使用dll即可。

  2. 连接字符串中指定了数据库,在这种情况下,它在应用程序工作文件夹中的test.db3文件中。

  3. 您可能希望缓存连接到数据库,以避免每次访问数据库时都要打开它的昂贵操作。此外,我不确定为什么您需要事务,因为您所做的就是从数据库中读取。

  4. 您有两个选择 - 要么存储具有预创建模式的空数据库,并在构建过程中将其复制到输出目录;或者创建一组SQL脚本,在第一次连接到数据库时从您的代码中执行它们。您选择哪个取决于您想让您的应用程序负责创建模式还是您的构建过程。

  5. 如果创建一个具有预创建模式的空数据库,则可以将其作为文件添加到您的源文件中,并指示VS将其复制到输出目录(如我之前提到的)。


谢谢Franci - 你能给我一个快速指针,告诉我在VS2008中我应该在哪里安排“指示VS将其复制到输出目录”吗? - Greg
1
在VS2005中,您可以通过在“解决方案资源管理器”中选择文件并在“属性”中更改“复制到输出目录”属性为“始终复制”或“仅在更改时复制”来完成此操作。在VS2008中应该是相同的。 - Agg
是的,正如@Agg所说,您可以通过解决方案资源管理器中的文件属性来完成此操作。 - Franci Penov

2

我几乎总是使用 using 来:

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

为了让sqlite表现更好,您需要在插入、更新和删除时使用事务,并且您需要使用参数化查询。连接的查询速度要慢得多:如何解决sqlite和c#中的“'”问题?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接