C#sqlite以更快的方式将数据表复制到另一个数据表

时间:2017-05-25 13:20:16

标签: c# sqlite ado.net

对于C#sqlite,有没有人可以分享如何以更快的方式将数据表复制到另一个数据表?谢谢。 而且还需要更改列名。我试图手动选择和更新,但遇到错误。感谢您的分享。感谢。

或者如何在datagridview中显示数据表时修改列标题?感谢。

dbConnect = new SQLiteConnection("Data Source=school.db;Version=3;");
dbConnect.Open();
cmd4 = new SQLiteCommand();
cmd4 = dbConnect.CreateCommand();
cmd4.CommandText = "DELETE FROM GroupEven";
cmd4.ExecuteNonQuery();
cmd4.CommandText = "SELECT Day, Day_ID, Standard, Timeslot1_TeacherName, Timeslot1_Subject, Timeslot2_TeacherName, Timeslot2_Subject, Timeslot3_TeacherName, Timeslot3_Subject, Timeslot4_TeacherName, Timeslot4_Subject, Timeslot5_TeacherName, Timeslot5_Subject, Timeslot6, Timeslot7_TeacherName, Timeslot7_Subject, Timeslot8_TeacherName, Timeslot8_Subject, Timeslot9_TeacherName, Timeslot9_Subject, Timeslot10_TeacherName, Timeslot10_Subject, Timeslot11_TeacherName, Timeslot11_Subject FROM TimetableFinal";
DataTable dt4 = new DataTable();
SQLiteDataAdapter da4 = new SQLiteDataAdapter(cmd4);
da4.Fill(dt4);
foreach (DataRow dr4 in dt4.Rows)
{
    cmd4.CommandText = "INSERT INTO TimetableFinal2 (Day, Day_ID, Standard, 7:30am-8:00am, 7.30am-8.00am, 8:00am-8:30am, 8.00am-8.30am, 8:30am-9:00am, 8.30am-9.00am, 9:00am-9:30am, 9.00am-9.30am, 9:30am-10:00am, 9.30am-10.00am, 10:00am-10:20am, 10:20am-10:50am, 10.20am-10.50am, 10:50am-11:20am, 10.50am-11.20am, 11:20am-11:50am, 11.20am-11.50am, 11:50am-12:20pm, 11.50am-12.20pm, 12:20pm-12:50pm, 12.20pm-12.50pm) VALUES (@Day, @Day_ID, @Standard, @7:30am-8:00am, @7.30am-8.00am, @8:00am-8:30am, @8.00am-8.30am, @8:30am-9:00am, @8.30am-9.00am, @9:00am-9:30am, @9.00am-9.30am, @9:30am-10:00am, @9.30am-10.00am, @10:00am-10:20am, @10:20am-10:50am, @10.20am-10.50am, @10:50am-11:20am, @10.50am-11.20am, @11:20am-11:50am, @11.20am-11.50am, @11:50am-12:20pm, @11.50am-12.20pm, @12:20pm-12:50pm, @12.20pm-12.50pm)";
    cmd4.Parameters.AddWithValue("@Day", dr4["Day"].ToString());

1 个答案:

答案 0 :(得分:0)

SQLite确实支持Joinings Insert语句,类似这样。

INSERT INTO 'tablename' ('column1', 'column2') 
VALUES
 ('data1', 'data2'),
 ('data3', 'data4'),
 ('data5', 'data6'),
 ('data7', 'data8');

见这个.. http://www.sqlite.org/lang_insert.html

然后一次执行。另外,请确保在事务中执行此操作并使用语句

using(var dbConnect = new SQLiteConnection("DataSource=school.db;Version=3;"))
{
   dbConnect.Open();
   using(var transaction = dbConnect.BeginTransaction())
   {
       string insertQuery = ...// your insert query
       using (var cmd = dbConnect.CreateCommand())
       {
          cmd.CommandText = insertQuery;
         foreach (DataRow dr4 in dt4.Rows)
         {
           cmd.Parameters.AddWithValue(...);
         }
         cmd.ExecuteNonQuery()
       }
       transaction.Commit();
   }
}

关于你的第二部分:如何在datagridview中显示数据表时修改列标题,

那是完全分开的,与Sqlite插入无关。 获取数据源后,您可以执行类似这样的操作

dataGridView1.Columns[i].HeaderText = "My New header";