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 :(得分:0)
在您的查询中,您没有参数,也没有必要将查询编写为2个字符串的连接:
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);