将数据从sqlite读入c#,然后再读入sqlite

本文关键字:sqlite 然后 数据 读入 | 更新日期: 2023-09-27 18:10:25

可复制示例:

sqlite db test3。s3db有一个名为"MathRec"的表:

name score
Bill 2
Mary 3
John 3
代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SQLite;
namespace ConsoleApplication7
{
    class Program
    {
    static void Main(string[] args)
    {
        string fullPath = "C:''Users''Desktop''dataset''test3.s3db";
        SQLiteConnection conread = new SQLiteConnection("Data Source=" + fullPath);
        conread.Open();
        string selectSQL = "SELECT * FROM MathRec";
        SQLiteCommand selectCommand = new SQLiteCommand(selectSQL, conread);
        SQLiteDataReader dataReader = selectCommand.ExecuteReader();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("MathRec");
        dt.Load(dataReader);
        ds.Tables.Add(dt);

        // Create a table in the database to receive the information from the DataSet
        string fullPath2 = "C:''Users''''Desktop''dataset''test4.s3db";
        SQLiteConnection conwrite = new SQLiteConnection("Data Source=" + fullPath2);
        conwrite.Open();
        SQLiteCommand cmd = new SQLiteCommand(conwrite);
        cmd.CommandText = "DROP TABLE IF EXISTS MathRec";
        cmd.ExecuteNonQuery();
        cmd.CommandText = "CREATE TABLE MathRec(name text , score integer)";
        cmd.ExecuteNonQuery();
        SQLiteDataAdapter adaptor = new SQLiteDataAdapter("SELECT * from MathRec", conwrite);
        adaptor.InsertCommand = new SQLiteCommand("INSERT INTO MathRec  VALUES(:name, :score)", conwrite);
        adaptor.InsertCommand.Parameters.Add("name", DbType.String, 0, "name");
        adaptor.InsertCommand.Parameters.Add("score", DbType.Int32, 0, "score");
        adaptor.Update(ds, "MathRec");

         }
    }
 }

问题:表MathRec在test4中创建。S3db有列名:name和score,但是表是空的,没有插入任何记录。

需要帮助!

请不要问我为什么我只是复制一个数据库到另一个,因为我正在测试一个更大的项目的代码的一部分,在那里我将做计算数据集在中间步骤在未来。

谢谢!


简化问题:

这个工作(使用datatable和adapter来更新原始sqlite表):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            string fullPath = "C:''Users''data''test.db";
            SQLiteConnection conread = new SQLiteConnection("Data Source=" + fullPath);
            conread.Open();

        SQLiteDataAdapter DB = new SQLiteDataAdapter("SELECT speed, dist FROM Cars2", conread);
        DataSet DS = new DataSet();
        DB.Fill(DS, "NewCars");

        object[] rowVals = new object[2];
        rowVals[0] = 10;
        rowVals[1] = 20;
        DS.Tables["NewCars"].Rows.Add(rowVals);

        DB.InsertCommand = new SQLiteCommand("INSERT INTO Cars2 (speed, dist)  
                                 " + " VALUES (:speed,  :dist)", conread);
        DB.InsertCommand.Parameters.Add("speed", DbType.Double, 0, "speed");
        DB.InsertCommand.Parameters.Add("dist", DbType.Double, 20, "dist");
        DB.Update(DS, "NewCars");

        }
    }
}

这是工作(从旧表创建一个新的sqlite表):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            string fullPath = "C:''Users''data''test.db";
            SQLiteConnection conread = new SQLiteConnection("Data Source=" + fullPath);
            conread.Open();

        SQLiteCommand cmd = new SQLiteCommand(conread);
        cmd.CommandText = "DROP TABLE IF EXISTS Cars";
        cmd.ExecuteNonQuery();
        cmd.CommandText = "CREATE TABLE Cars (speed REAL , dist REAL)";
        cmd.ExecuteNonQuery();
        cmd.CommandText = "INSERT INTO Cars SELECT * from DS";
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        }
    }
}

但这是我想要的(使用数据表和适配器在sqlite中创建新表),不工作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            string fullPath = "C:''Users''data''test.db";
            SQLiteConnection conread = new SQLiteConnection("Data Source=" + fullPath);
            conread.Open();
    SQLiteDataAdapter DB = new SQLiteDataAdapter("SELECT speed, dist FROM Cars2", conread);
    DataSet DS = new DataSet();
    DB.Fill(DS, "NewCars");

    object[] rowVals = new object[2];
    rowVals[0] = 10;
    rowVals[1] = 20;
    DS.Tables["NewCars"].Rows.Add(rowVals);

   SQLiteDataAdapter DB2 = new SQLiteDataAdapter("SELECT speed, dist FROM Cars3", conread);
    DB2.InsertCommand = new SQLiteCommand("INSERT INTO Cars3 (speed, dist)  
                        " + " VALUES (:speed,  :dist)", conread);
    DB2.InsertCommand.Parameters.Add("speed", DbType.Double, 0, "speed");
    DB2.InsertCommand.Parameters.Add("dist", DbType.Double, 20, "dist");
    DB2.Update(DS, "NewCars");

    }
}
}

请帮助! !

将数据从sqlite读入c#,然后再读入sqlite

我找到答案了!

适配器。Update只能用于更新数据库中的原始表,而不能将数据表保存到新的数据库中。请参考该主题的答案:

c# Dataset to Access DB

干杯!

将一个表复制到另一个数据库的最简单方法是将第二个数据库附加到第一个连接,并直接复制数据:

ATTACH '...'test4.s3db' AS 'test4';
DROP TABLE IF EXISTS test4.MathRec;
CREATE TABLE test4.MathRec(name text , score integer);
INSERT INTO test4.MathRec SELECT * FROM MathRec;

如果你想做计算,你可以在SQL中做,在SELECT语句中。如果没有,则必须使用SELECT读取数据,然后为要写入的每条记录执行一条INSERT语句。(在这种情况下,您不需要ATTACH。)