【问题标题】:read data from sqlite into C# then to sqlite将数据从 sqlite 读入 C#,然后再读入 sqlite
【发布时间】:2013-09-16 14:05:02
【问题描述】:

可重现的例子:

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 和 socre,但表为空,没有插入任何记录。

需要帮助!

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

谢谢!


为了简化问题:

这可行(使用数据表和适配器来更新原始的 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 有时会很痛苦
  • 所以我应该忘记 sqlite 而只使用 SQLServer 吗?那你有什么建议呢?
  • 通常参数名称应包含任何特殊字符,例如@name 或 :name,当您将参数添加到参数集合时。
  • 你确定SQLiteDataAdapter可以同时访问两个不同的连接吗?
  • @TongZZZ 我没有。只是说它有时很痛苦。如果我能负担得起,我会选择 SQLServer。

标签: c# sqlite system.data.sqlite


【解决方案1】:

我找到了答案!

Adapter.Update 只能用于更新数据库中的原始表,不能将数据表保存到新表中。答案请参考帖子:

C# Dataset to Access DB

干杯!

【讨论】:

    【解决方案2】:

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

    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。)

    【讨论】:

    • 感谢您的回复,现在我决定只使用一个数据库,您的建议本身就有效。但是,我真的想将 DataTable 中的表插入 SQLite 表,而不是像 INSERT INTO test4.MathRec SELECT * FROM MathRec; 那样从另一个 SQLite 表中插入。因此我认为我必须使用 SQLiteDataAdapter 适配器,直到现在我都没有运气。
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 2019-07-25
    • 2012-07-22
    • 2015-07-07
    • 2021-09-11
    • 2014-06-10
    • 1970-01-01
    • 2017-12-23
    相关资源
    最近更新 更多