lire les données de sqlite en C#, puis à sqlite

Reproductible Exemple:

sqlite db test3.s3db a un tableau avec le nom "MathRec":

name score
Bill 2
Mary 3
John 3

Code:

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

Questions:
Tableau MathRec est créé dans test4.s3db avec les noms de colonne: nom et la note, mais la table est vide sans les enregistrements insérés.

Besoin d'aide!

S'il vous plaît ne me demandez pas pourquoi, je suis juste de copier l'un db à l'autre, parce que je suis en train de tester une partie du code d'un projet plus vaste, où je vais faire les calculs à la base de données dans l'étape intermédiaire dans l'avenir.

Merci!


Pour simplifier la question:

Cela fonctionne (utiliser datatable et l'adaptateur de mise à jour de l'original de la table 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");
}
}
}

Et cela fonctionne (créer un nouveau sqlite table à partir de l'ancien):

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();
}
}
}

Mais c'est ce que je voulais (utiliser datatable et l'adaptateur pour créer une table dans sqlite) et ne fonctionne pas:

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

S'il vous plaît aider!!!

  • Tout ce que je peux dire, c'est sqlite peut être un GROS parfois de la douleur
  • donc je devrais oublier sqlite et aller juste pour SQLServer? quelle est votre suggestion?
  • En général, le paramètre nom doit inclure tous les caractères spéciaux, par exemple @nom ou :nom, lorsque vous ajoutez le paramètre de la collection de Paramètres.
  • Êtes-vous sûr que SQLiteDataAdapter peut être utilisé pour accéder à deux connexions différentes en même temps?
  • Je n'en ai pas. Juste dire a son gros parfois de la douleur. Je voudrais aller avec SQLServer si je pouvais me le permettre.
InformationsquelleAutor TongZZZ | 2013-04-22