基本上我有 ac# 应用程序,它从文本文件中获取值,将它们存储在列表中并将它们传输到 SQLite 表中。
这三个表分别命名为 abc、t2 和 t3。
然后我想获取这些 sqlite 表并将它们呈现在 datagridviews 的 c# 应用程序中。到目前为止,我已经能够将 1 个表(表 abc)传输到网格视图。 我想将所有 3 个表转移到 gridview。
问题:我想让 3 个表格出现在 ONE 网格中,但我根本不知道如何做到这一点的语法。
以下是这 3 个表的填充方式。更重要的代码(将数据传输到网格)在此下方
// TRANSFERING VALUES TO THE SQLITE DB
private void button4_Click(object sender, EventArgs e)
{
// We use these three SQLite objects:
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connection: // Maybe error here - video was different
sqlite_conn = new SQLiteConnection(@"Data Source=database.db;Version=3;");
// open the connection:
sqlite_conn.Open();
// create a new SQL command:
sqlite_cmd = sqlite_conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS 'abc' (Seq text, Field text, Desc text, Len text, Dec text, Typ text, Percnt text, Pop text, Alzero text, MaxLen text );";
// Now lets execute the SQL
sqlite_cmd.ExecuteNonQuery();
// **** SQLITE TRANSFER SECTION 1 - transfer values from list1 to table1 *****
sqlite_cmd.CommandText = " DELETE FROM abc";
sqlite_cmd.ExecuteNonQuery();
sqlite_cmd.CommandText = "INSERT INTO abc (Seq, Field, Desc, Len, Dec, Typ, Percnt, Pop, Alzero, MaxLen) VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)";
sqlite_cmd.Parameters.AddWithValue("@p1", 6); // dummy initial values
sqlite_cmd.Parameters.AddWithValue("@p2", 878);
sqlite_cmd.Parameters.AddWithValue("@p3", 56);
sqlite_cmd.Parameters.AddWithValue("@p4", 6);
sqlite_cmd.Parameters.AddWithValue("@p5", 546);
sqlite_cmd.Parameters.AddWithValue("@p6", 565);
sqlite_cmd.Parameters.AddWithValue("@p7", 568);
sqlite_cmd.Parameters.AddWithValue("@p8", 526);
sqlite_cmd.Parameters.AddWithValue("@p9", 586);
sqlite_cmd.Parameters.AddWithValue("@p10", 526);
for (int i = 0; i < NumListValues; i += 10) // Filling SQlite table rows and columns with values from our list
{
sqlite_cmd.Parameters.AddWithValue("@p1", list[i]);
sqlite_cmd.Parameters.AddWithValue("@p2", list[i+1]);
sqlite_cmd.Parameters.AddWithValue("@p3", list[i+2]);
sqlite_cmd.Parameters.AddWithValue("@p4", list[i+3]);
sqlite_cmd.Parameters.AddWithValue("@p5", list[i+4]);
if (i > 490)
break;
sqlite_cmd.Parameters.AddWithValue("@p6", list[i+5]);
sqlite_cmd.Parameters.AddWithValue("@p7", list[i+6]);
sqlite_cmd.Parameters.AddWithValue("@p8", list[i+7]);
sqlite_cmd.Parameters.AddWithValue("@p9", list[i+8]);
sqlite_cmd.Parameters.AddWithValue("@p10", list[i+9]);
sqlite_cmd.ExecuteNonQuery();
}
// **** SQLITE TRANSFER SECTION 2 - transfer values from list2 to 2nd table *****
sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS 't2' (YYMM text, MinDate text, MaxDate text, TotalTrans text, DebitTrans text, AMOUNTINDOCUMENTCURREN text );";
sqlite_cmd.ExecuteNonQuery();
sqlite_cmd.CommandText = " DELETE FROM t2";
sqlite_cmd.ExecuteNonQuery();
sqlite_cmd.CommandText = "INSERT INTO t2 (YYMM, MinDate, MaxDate, TotalTrans, DebitTrans, AMOUNTINDOCUMENTCURREN ) VALUES (@b1, @b2, @b3, @b4, @b5, @b6)";
sqlite_cmd.Parameters.AddWithValue("@b1", 6); // dummy initial values
sqlite_cmd.Parameters.AddWithValue("@b2", 878);
sqlite_cmd.Parameters.AddWithValue("@b3", 56);
sqlite_cmd.Parameters.AddWithValue("@b4", 6);
sqlite_cmd.Parameters.AddWithValue("@b5", 546);
sqlite_cmd.Parameters.AddWithValue("@b6", 565);
for (int i = 0; i < NumList2Values; i+= 6) // Filling SQlite table rows and columns with values from list2
{
sqlite_cmd.Parameters.AddWithValue("@b1", list2[i]);
sqlite_cmd.Parameters.AddWithValue("@b2", list2[i+1]);
sqlite_cmd.Parameters.AddWithValue("@b3", list2[i+2]);
sqlite_cmd.Parameters.AddWithValue("@b4", list2[i+3]);
sqlite_cmd.Parameters.AddWithValue("@b5", list2[i+4]);
sqlite_cmd.Parameters.AddWithValue("@b6", list2[i+5]);
sqlite_cmd.ExecuteNonQuery();
}
// Create table to transfer values from list 3
sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS 't3' (YYWW text, MinDate text, MaxDate text, TotalTrans text, DebitTrans text, AMOUNTINDOCUMENTCURREN text );";
sqlite_cmd.ExecuteNonQuery();
sqlite_cmd.CommandText = " DELETE FROM t3";
sqlite_cmd.ExecuteNonQuery();
sqlite_cmd.CommandText = "INSERT INTO t3 (YYWW, MinDate, MaxDate, TotalTrans, DebitTrans, AMOUNTINDOCUMENTCURREN ) VALUES (@c1, @c2, @c3, @c4, @c5, @c6)";
sqlite_cmd.Parameters.AddWithValue("@c1", 6); // dummy initial values
sqlite_cmd.Parameters.AddWithValue("@c2", 878);
sqlite_cmd.Parameters.AddWithValue("@c3", 56);
sqlite_cmd.Parameters.AddWithValue("@c4", 6);
sqlite_cmd.Parameters.AddWithValue("@c5", 546);
sqlite_cmd.Parameters.AddWithValue("@c6", 565);
for (int i = 0; i < NumList3Values ; i+= 6) // Filling SQlite table rows and columns with values from list2
{
sqlite_cmd.Parameters.AddWithValue("@c1", list3[i]);
sqlite_cmd.Parameters.AddWithValue("@c2", list3[i+1]);
sqlite_cmd.Parameters.AddWithValue("@c3", list3[i+2]);
sqlite_cmd.Parameters.AddWithValue("@c4", list3[i+3]);
sqlite_cmd.Parameters.AddWithValue("@c5", list3[i+4]);
sqlite_cmd.Parameters.AddWithValue("@c6", list3[i+5]);
sqlite_cmd.ExecuteNonQuery();
}
以下是我如何使用第一个表中的值填充数据网格
string sql = " SELECT * FROM abc";
SQLiteDataAdapter da;
DataTable dt;
sqlite_cmd.CommandText = sql;
//Create New Datatable to fill with data
dt = new DataTable();
//Create DataAdapter to fill data in DataTable via Adapter
da = new SQLiteDataAdapter(sql, sqlite_conn);
da.Fill(dt);
// Lets populate the datagrid
dataGridView1.DataSource = dt;
dataGridView1.Refresh();