[英]How to insert DataGridView data in to table of access database using c#?
[英]Insert data into table(from select) using 2 DataGridView c# windowsForm
請幫助我了解我要去哪里。 好了,走吧! 2 DataGridViews,首先,我將服務存儲在第二順序列表中。 當我按下“保存”按鈕時,將發生以下代碼:
public void insert_sales_list()
{
conn.Open();
foreach (DataGridViewRow row in dgvService.SelectedRows)
{
SQLiteCommand cmd = new SQLiteCommand("insert into sales_list (sales_created_date, sales_created_name, emp_name, cust_phone, cust_name, planned_date, planned_time, service_name, discount, price, order_id) values (@ocd, @ocn, @emp, @c_phone, @c_name, @p_date, @p_time, @sn, @disc, @price, @o_id)", conn);
cmd.Parameters.AddWithValue("@ocd", DateTime.Now);
cmd.Parameters.AddWithValue("@ocn", lblLoginUser.Text);
cmd.Parameters.AddWithValue("@emp", dgvOrderList.CurrentRow.Cells[1].Value.ToString());
cmd.Parameters.AddWithValue("@c_phone", dgvOrderList.CurrentRow.Cells[2].Value.ToString());
cmd.Parameters.AddWithValue("@c_name", dgvOrderList.CurrentRow.Cells[3].Value.ToString());
cmd.Parameters.AddWithValue("@p_date", dgvOrderList.CurrentRow.Cells[5].Value);
cmd.Parameters.AddWithValue("@p_time", dgvOrderList.CurrentRow.Cells[6].Value.ToString());
cmd.Parameters.AddWithValue("@sn", row.Cells[0].Value.ToString());
cmd.Parameters.AddWithValue("@disc", dgvOrderList.CurrentRow.Cells[4].Value.ToString());
cmd.Parameters.AddWithValue("@price", row.Cells[1].Value.ToString());
cmd.Parameters.AddWithValue("@o_id", dgvOrderList.CurrentRow.Cells["order id"].Value);
cmd.ExecuteNonQuery();
string sql = "update order_list set status = 'Saved' where id = '" + dgvOrderList.CurrentRow.Cells["order id"].Value + "'";
cmd = new SQLiteCommand(sql, conn);
cmd.ExecuteNonQuery();
}
conn.Close();
通過此代碼,您看到我只是將數據從訂單列表插入到銷售列表中,用戶從DataGridView.Service中選擇一個或多個服務,他可以從列表中獲取任何服務。 此代碼效果很好。
下一步。 我有另一個桌子,每個服務都有自己的材料,例如-男士理發材料有肥皂,洗發水和薄紙。 而且我需要在SalesMaterials表中插入這些數據。 而且我認為代碼是錯誤的,請幫助我找到此錯誤? 碼:
public void insert_sales_materials()
{
conn.Open();
foreach (DataGridViewRow row in dgvService.SelectedRows)
{
string Query = "insert into sales_list_materials(order_id, material_id, norma, created_name, creation_date) " +
"values( select '" + dgvOrderList.CurrentRow.Cells["order id"].Value + "', a.material_id, a.norma, '" + lblLoginUser.Text + "', '" + DateTime.Now + "' from service_materials a where a.service_id = '" + row.Cells[2].Value + "')";
SQLiteCommand cmd = new SQLiteCommand(Query, conn);
cmd.ExecuteNonQuery();
}
conn.Close();
}
錯誤:
附加信息:SQLite錯誤
“選擇”附近:語法錯誤
好,我知道了! 當您使用select插入數據時,請不要使用單詞值=))對所有人正確的代碼:
public void insert_sales_materials()
{
conn.Open();
foreach (DataGridViewRow row in dgvService.SelectedRows)
{
string Query = "insert into sales_list_materials(order_id, material_id, norma, created_name, creation_date) " +
"select '" + dgvOrderList.CurrentRow.Cells["order id"].Value + "', a.material_id, a.norma, '" + lblLoginUser.Text + "', '" + DateTime.Now + "' from service_materials a where a.service_id = '" + row.Cells[2].Value + "'";
SQLiteCommand cmd = new SQLiteCommand(Query, conn);
cmd.ExecuteNonQuery();
}
conn.Close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.