2017-09-04 36 views
0

请帮我理解我出错的地方。好了,走吧! 2 DataGridViews,首先我是商店服务的二阶列表。 当我按下按钮保存,这个代码将会发生:使用2 DataGridView将数据插入表(从选择)c#windowsForm

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的错误

附近的 “选择”:语法错误

回答

1

好吧我知道了! 当你插入数据与选择,请不要使用字值=)) 正确的代码为你们所有人:

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(); 
    } 
相关问题