【问题标题】:I'am not able to delete information from my database我无法从我的数据库中删除信息
【发布时间】:2014-06-11 21:04:41
【问题描述】:

我在从数据库中删除选定行时遇到问题,事实上,我在 C# 中有一个表单,其中包含一个连接到数据库的 dataGridView 和一个“删除”按钮,当我单击该按钮时,这应该删除信息从 dataGridView 和数据库中的选定行(单元格 [0] 和单元格 [1])。现在,我遇到了从数据库中删除选定行的问题,这是我的代码:

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_journal 和 initule” 感谢您的帮助

【问题讨论】:

  • inituleintitule ??
  • 这是 intitule @Andy G :)

标签: c# sql database datagridview


【解决方案1】:

您正在删除该行,然后使用 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);

【讨论】:

    【解决方案2】:

    除了 sorton9999 提供的 answer 之外,另一个问题是您没有对您的 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 注入敞开心扉,请改用参数化查询。此外,您应该将SqlConnectionSqlCommand 包装在using 语句中,以确保它们被正确处理。像这样的:

    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();
        }
    }
    

    【讨论】:

      【解决方案3】:

      在您的单引号 (') 和 SQL 语句中的单词 AND 之间没有空格是不是很简单?

      值得一试...

      【讨论】:

      • 感谢 sorton9999 的回复,但我仍然遇到同样的问题,该行已从 dataGridView 中删除,但未从数据库中删除(我使用本地数据库)
      • 谢谢大家!!我的问题已解决,信息已从数据库中删除,但是当我第二次单击以显示表格时,信息再次出现在 dataGridView 中(我使用绑定“MainDataSet”)
      猜你喜欢
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多