gpt4 book ai didi

c# - 从 sqlite 读取数据到 C# 然后到 sqlite

转载 作者:行者123 更新时间:2023-11-30 12:11:06 27 4
gpt4 key购买 nike

可重现的例子:

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");


}
}
}

请帮忙!!!

最佳答案

我找到了答案!

Adapter.Update 只能用于更新数据库中的原始表,不能用于将数据表保存到新表中。请参阅主题以获取答案:

C# Dataset to Access DB

干杯!

关于c# - 从 sqlite 读取数据到 C# 然后到 sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16137697/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com