C# I';我无法从数据库中删除信息
我在从数据库中删除所选行时遇到问题,事实上,我在C#中有一个表单,其中包含一个连接到数据库的dataGridView和一个“Delete”按钮,当我点击该按钮时,这将删除dataGridView和数据库中所选行(单元格[0]和单元格[1])中的信息。现在,从数据库中删除所选行时遇到问题这是我的代码:C# I';我无法从数据库中删除信息,c#,sql,database,datagridview,C#,Sql,Database,Datagridview,我在从数据库中删除所选行时遇到问题,事实上,我在C#中有一个表单,其中包含一个连接到数据库的dataGridView和一个“Delete”按钮,当我点击该按钮时,这将删除dataGridView和数据库中所选行(单元格[0]和单元格[1])中的信息。现在,从数据库中删除所选行时遇到问题这是我的代码: private void button4_Click(object sender, EventArgs e) { if (journalDataGridVie
private void button4_Click(object sender, EventArgs e)
{
if (journalDataGridView.SelectedRows.Count == 1)
{
DataGridViewRow row = journalDataGridView.SelectedRows[0];
journalDataGridView.Rows.Remove(row);
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand sql = new SqlCommand("delete from journal where code_journal='" + journalDataGridView.CurrentRow.Cells[0].Value.ToString() + "'AND intitule='" + journalDataGridView.CurrentRow.Cells[1].Value.ToString() + "';", connection);
connection.Close();
}
}
dataGridView包含两列“code\u journal和initule”
感谢您的帮助您的SQL语句中的单引号(')和单词之间没有空格,这是否很简单
值得一试…是否可以简单到,在单引号(')和单词之间以及SQL语句中没有空格
值得一试…您正在删除该行,然后使用CurrentRow属性引用错误的行 您也没有使用参数来避免sql注入 您也没有执行以下命令:
DataGridViewRow row = journalDataGridView.SelectedRows[0];
connection.Open();
using (SqlCommand sql = new SqlCommand("delete from journal where code_journal=@codeJournal..", connection)) {
sql.Parameters.AddWithValue("@codeJournal", row.Cells[0].Value.ToString());
sql.ExecuteNonQuery();
}
connection.Close();
journalDataGridView.Rows.Remove(row);
您正在删除该行,然后使用CurrentRow属性引用错误的行 您也没有使用参数来避免sql注入 您也没有执行以下命令:
DataGridViewRow row = journalDataGridView.SelectedRows[0];
connection.Open();
using (SqlCommand sql = new SqlCommand("delete from journal where code_journal=@codeJournal..", connection)) {
sql.Parameters.AddWithValue("@codeJournal", row.Cells[0].Value.ToString());
sql.ExecuteNonQuery();
}
connection.Close();
journalDataGridView.Rows.Remove(row);
除了sorton9999提供的命令外,另一个问题是您没有使用SqlCommand
对象执行任何操作
创建后,您需要执行它:
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand sql = new SqlCommand("delete from journal where code_journal='" + journalDataGridView.CurrentRow.Cells[0].Value.ToString() + "'AND intitule='" + journalDataGridView.CurrentRow.Cells[1].Value.ToString() + "';", connection);
sql.ExecuteNonQuery();
connection.Close();
通过进行字符串连接,您可能会遇到Sql注入,请改用参数化查询。此外,您应该使用语句将SqlConnection
和SqlCommand
包装在中,以确保它们得到正确处理。大概是这样的:
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand sql = new SqlCommand("delete from journal where code_journal=@codeJournal AND initule=@inituleVal", connection))
{
cmd.Parameters.AddWithValue("@codeJournal", journalDataGridView.CurrentRow.Cells[0].Value.ToString());
cmd.Parameters.AddWithValue("@inituleVal", journalDataGridView.CurrentRow.Cells[1].Value.ToString());
connection.Open();
sql.ExecuteNonQuery();
}
}
除了sorton9999提供的命令外,另一个问题是您没有使用SqlCommand
对象执行任何操作
创建后,您需要执行它:
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand sql = new SqlCommand("delete from journal where code_journal='" + journalDataGridView.CurrentRow.Cells[0].Value.ToString() + "'AND intitule='" + journalDataGridView.CurrentRow.Cells[1].Value.ToString() + "';", connection);
sql.ExecuteNonQuery();
connection.Close();
通过进行字符串连接,您可能会遇到Sql注入,请改用参数化查询。此外,您应该使用
语句将SqlConnection
和SqlCommand
包装在中,以确保它们得到正确处理。大概是这样的:
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand sql = new SqlCommand("delete from journal where code_journal=@codeJournal AND initule=@inituleVal", connection))
{
cmd.Parameters.AddWithValue("@codeJournal", journalDataGridView.CurrentRow.Cells[0].Value.ToString());
cmd.Parameters.AddWithValue("@inituleVal", journalDataGridView.CurrentRow.Cells[1].Value.ToString());
connection.Open();
sql.ExecuteNonQuery();
}
}
initule或intitule??是intitule@Andy G:)initule或intitule??是intitule@Andy G:)谢谢sorton9999的回复,但我仍然有同样的问题,行从dataGridView中删除,但不是从数据库中删除(我使用本地数据库)谢谢大家!!我的问题已解决,信息已从数据库中删除,但当我第二次clic以显示表时,信息再次出现在dataGridView中(我使用绑定“MainDataSet”),感谢sorton9999的回复,但我仍然存在相同的问题,即行已从dataGridView中删除,而不是从数据库中删除(我使用本地数据库)谢谢大家!!我的问题解决了,信息从数据库中删除,但当我第二次clic显示表时,信息再次出现在dataGridView中(我使用绑定“MainDataSet”)