2015-03-31 59 views
-2
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(); 
      } 
     } 
    } 

請幫助我,這是我的學士學位項目如何在這段代碼聲明標量變量「@CIN」

+0

參數名稱應以'@'開頭。 – Blorgbeard 2015-03-31 21:05:42

+0

我不明白這種類型的評論'請幫助我這是我的學士學位項目'。爲什麼你認爲我們應該幫助你。對於前者,我不希望有一個隊友(也許在不久的將來)誰不能解決這個問題。 – EZI 2015-03-31 21:12:07

回答

0

首先在你的查詢您沒有參數,也沒有必要寫查詢作爲2串concatanation:

string sqlQuery = @"INSERT INTO 
         Abscence (CIN,Heure_debut,Heure_fin,Date) 
        VALUES 
         (@CIN, @Heure_debut, @Heure_fin, @Date)"; 

可以使用符號@,啓動串@""之前多行寫的字符串。

要再次聲明參數,請使用@符號。當你添加它們的值時,應該這樣寫:

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); 
+0

我這樣做,但有一個新問題「附加信息:參數化查詢'(@CIN int,@ Heure_debut nvarchar(4000),@ Heure_fin nvarchar(4000),'期望參數'@Heure_debut',它沒有提供。 「 – 2015-03-31 21:13:53

+0

如果仔細觀察我的代碼,您會發現在參數前後不應該有'' – mybirthname 2015-03-31 21:14:47

+0

我複製了您的解決方案,但同樣的問題其他信息:參數化查詢'(@CIN int,@ Heure_debut nvarchar(4000 ),@ Heure_fin nvarchar(4000),'期望參數'@Heure_debut',這是沒有提供 – 2015-03-31 21:20:42