【问题标题】:Save Dataset to SQLite format file将数据集保存为 SQLite 格式文件
【发布时间】:2010-10-06 20:16:18
【问题描述】:

我有一个包含多个表的数据集。 我显然可以做一个 Dataset.WriteToXML("Somefile.xml")

如果我想将数据集导出到 SQLite 格式的文件。

换句话说,我希望能够将数据集的内容写入(即序列化)到 SQLite 文件。 Dataset.SerializeToSQLite("Sqliteformatted.bin")

同样,我希望能够将 SQLite 文件读入数据集。

我想在 c# 中执行此操作。

提前感谢任何指点。

鲍勃

【问题讨论】:

    标签: sqlite serialization dataset


    【解决方案1】:

    SQLite 不是一种文件格式,它是一个数据库。

    如果要将DataSet中的所有数据放入数据库,则需要创建与数据库的连接(可以是空白文件,SQLite会在初始连接时创建它),然后创建数据库结构体。然后发出INSERT 语句将数据附加到数据库。您可以使用SQLiteDataAdapter 来简化插入语句的创建。

    【讨论】:

      【解决方案2】:

      这个例子可能会回答你的问题。

      using System;
      using System.Data;
      using System.Data.SQLite;
      
      namespace DataAdapterExample
          {
          class Program
          {
              static void Main(string[] args)
              {
                  // Create a simple dataset
                  DataTable table = new DataTable("Students");
                  table.Columns.Add("name", typeof(string));
                  table.Columns.Add("id", typeof(int));
                  table.Rows.Add("Bill Jones", 1);
                  table.Rows.Add("Laurie Underwood", 2);
                  table.Rows.Add("Jeffrey Sampson", 3);
                  DataSet ds = new DataSet();
                  ds.Tables.Add(table);
                  // Save in an SQLite file
                  string desktopPath = 
                      Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                  string fullPath = desktopPath + "\\class.db";
                  SQLiteConnection con = new SQLiteConnection("Data Source=" + fullPath);
                  con.Open();
                  // Create a table in the database to receive the information from the DataSet
                  SQLiteCommand cmd = new SQLiteCommand(con);
                  cmd.CommandText = "DROP TABLE IF EXISTS Students";
                  cmd.ExecuteNonQuery();
                  cmd.CommandText = "CREATE TABLE Students(name text, id integer PRIMARY  KEY)";
                  cmd.ExecuteNonQuery();
                  SQLiteDataAdapter adaptor = new SQLiteDataAdapter("SELECT * from Students", con);
                  adaptor.InsertCommand = new SQLiteCommand("INSERT INTO Students  VALUES(:name, :id)", con);
                  adaptor.InsertCommand.Parameters.Add("name", DbType.String, 0, "name");
                  adaptor.InsertCommand.Parameters.Add("id", DbType.Int32, 0, "id");
                  adaptor.Update(ds, "Students");
                  // Check database by filling the dataset in the other direction and displaying
                  ds = new DataSet();
                  adaptor.Fill(ds, "Students");
                  foreach (DataTable dt in ds.Tables)
                  {
                      Console.WriteLine("Table {0}", dt.TableName);
                      foreach (DataRow dr in dt.Rows)
                      {
                          foreach (DataColumn dc in dt.Columns)
                          {
                              Console.Write("{0,-18}", dr[dc]);
                          }
                          Console.WriteLine();
                      }
                  }
              }
          }
      }
      

      您可以在SQLiteDataAdapter Class documentation 中找到一个非常相似的示例。

      【讨论】:

        猜你喜欢
        • 2021-06-05
        • 1970-01-01
        • 2011-02-14
        • 2015-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多