将数据集保存为 SQLite 格式文件

Save Dataset to SQLite format file(将数据集保存为 SQLite 格式文件)
本文介绍了将数据集保存为 SQLite 格式文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dataset with multiple tables. I can obviously do a Dataset.WriteToXML("Somefile.xml")

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

What if I want to export the dataset to a SQLite formatted file.

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

In other words I want to be able to write (i.e. serialize) the contents of the dataset to a SQLite file. Dataset.SerializeToSQLite("Sqliteformatted.bin")

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

Similarly I want to be able to read the SQLite file into a Dataset.

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

I would like to do this in c#.

提前感谢任何指点.

鲍勃

推荐答案

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

This example may answer your question.

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 类文档.

这篇关于将数据集保存为 SQLite 格式文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

FastAPI + Tortoise ORM + FastAPI Users (Python) - Relationship - Many To Many(FastAPI+Tortoise ORM+FastAPI用户(Python)-关系-多对多)
Window functions not working in pd.read_sql; Its shows error(窗口函数在pd.read_sql中不起作用;它显示错误)
(Closed) Leaflet.js: How I can Do Editing Geometry On Specific Object I Select Only?((已关闭)Leaflet.js:如何仅在我选择的特定对象上编辑几何图形?)
in sqlite update trigger with multiple if/Case Conditions(在具有多个IF/CASE条件的SQLite UPDATE触发器中)
Android: Why is Room so slow?(Android:为什么Room这么慢?)
Remote Procedure call failed with sql server 2008 R2(使用 sql server 2008 R2 的远程过程调用失败)