2015-03-31 93 views
0

你好我有一個問題,在缺勤 缺席的形式,我已經添加了一個datagridview,並在那裏我添加了兩個列組合框的形式之一缺席的開始時間另一個結束時間爲 在datagridview中,當我點擊「顯示」的底部時,它將添加名爲「Etudiant」的每個學生的名稱和ID,並且在datagridview的拐角處有一個datetimepicker所以我需要當用戶選擇缺席的時間和每個學生的日期,他點擊底部的「保存」,它必須保存在另一個名爲「缺席」的表中,這是我的嘗試,並有一個錯誤,請幫助我這是學士學位項目:)datagridview不在新表中更新

private void button2_Click(object sender, EventArgs e) 
    { // Open the connection using the connection string. 
     SqlConnection con = new SqlConnection(@"Data Source=WIN-6Q836P8JQ1C\oby;Initial Catalog=Etudiant;Integrated Security=True"); 
     con.Open(); 
     string sqlQuery = "INSERT INTO Abscence (CIN,Heure_debut,Heure_fin,Date)"; 
     sqlQuery += "VALUES ('CIN', 'Heure_debut', 'Heure_fin', 'Date')"; 
      // Insert into the Sql table. ExecuteNonQuery is best for inserts. 
      using (SqlCommand com = new SqlCommand(sqlQuery, con)) 
      { 
       for (int i = 0; i < dataGridView1.Rows.Count; i++) 
       { 
        com.Parameters.AddWithValue("CIN", dataGridView1.Rows[i].Cells["CIN"].Value); 
        com.Parameters.AddWithValue("Heure_debut", dataGridView1.Rows[i].Cells["column1"].Value); 
        com.Parameters.AddWithValue("Heure_fin",dataGridView1.Rows[i].Cells["column2"].Value); 
        com.Parameters.AddWithValue("Date", dateTimePicker1.Text); 
       } 
       com.ExecuteNonQuery(); 
       com.Parameters.Clear(); 
       con.Close(); 
      } 
     } 
    } 
+0

有錯誤不是很清楚,請解釋一下。 – Sybren 2015-03-31 18:03:00

+0

當我啓動程序時,它顯示錯誤「參數化查詢」(@CIN int,@ Heure_debut nvarchar(4000),@ heure_fin nvarchar(4000),'期望沒有提供的參數'@ Heure_debut'。「 – 2015-03-31 18:15:38

+0

我想你已經回答了你自己的問題 – user2526236 2015-03-31 18:18:08

回答

1

請確保您輸入的值正在插入。

private void button2_Click(object sender, EventArgs e) 
{ // Open the connection using the connection string. 
    SqlConnection con = new SqlConnection(@"Data Source=WIN-6Q836P8JQ1C\oby;Initial Catalog=Etudiant;Integrated Security=True"); 
    con.Open(); 
    string sqlQuery = "INSERT INTO Abscence (CIN,Heure_debut,Heure_fin,Date)"; 
    sqlQuery += "VALUES (@CIN, @Heure_debut, @Heure_fin, @Date)"; 

     // Insert into the Sql table. ExecuteNonQuery is best for inserts. 
     using (SqlCommand com = new SqlCommand(sqlQuery, con)) 
     { 
      for (int i = 0; i < dataGridView1.Rows.Count; i++) 
      { 
       com.Parameters.AddWithValue("@CIN", dataGridView1.Rows[i].Cells["CIN"].Value); 
       com.Parameters.AddWithValue("@Heure_debut", dataGridView1.Rows[i].Cells["column1"].Value); 
       com.Parameters.AddWithValue("@Heure_fin",dataGridView1.Rows[i].Cells["column2"].Value); 
       com.Parameters.AddWithValue("@Date", dateTimePicker1.TextToString("YYYY-mm-DD"); 
      } 
      com.ExecuteNonQuery(); 
      com.Parameters.Clear(); 
      con.Close(); 
     } 
    } 
} 
+0

另一個問題「附加信息:轉換日期和/或時間從字符串轉換失敗。」 – 2015-03-31 18:26:30

+0

添加此參數dateTimePicker1.Text.ToString(「YYYY-mm-DD」))。請標記爲答案,如果有幫助 – user2526236 2015-03-31 18:28:14

+0

做了這項工作? – user2526236 2015-03-31 18:31:17