2017-09-04 64 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(); 
    } 
相關問題