Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言删除mysql表项#_C#_Mysql_Sql Delete - Fatal编程技术网

C# 用C语言删除mysql表项#

C# 用C语言删除mysql表项#,c#,mysql,sql-delete,C#,Mysql,Sql Delete,基本上,我想从dataviewtable中删除一个条目,它从MySql中提取数据。我认为通过有效地复制修改代码并将其替换为“DELETE”可以公平地做到这一点。然而,从下面的代码中,您可以清楚地看到这并没有起作用。我将为您复制我的大部分代码: private void button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=DESKTOP-HNR3NJB\

基本上,我想从dataviewtable中删除一个条目,它从MySql中提取数据。我认为通过有效地复制修改代码并将其替换为“DELETE”可以公平地做到这一点。然而,从下面的代码中,您可以清楚地看到这并没有起作用。我将为您复制我的大部分代码:

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=DESKTOP-HNR3NJB\\mysql;Initial Catalog=stock;Integrated Security=True");
    var sqlQuery = "";
    if (IfProductsExists(con, textboxProductID.Text))
    {
        con.Open();
        sqlQuery = @"DELETE FROM [Products] WHERE [ProductID] = '" + textboxProductID.Text + "'";
        SqlCommand cmd = new SqlCommand(sqlQuery, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    else
    {
        MessageBox.Show("Record doesn't exist!", "ERROR:", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    //Reading Data
    LoadData();
}
这就是“添加”按钮和“加载数据”功能的“删除”按钮的代码

添加按钮:

private void button2_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-HNR3NJB\mysql;Initial Catalog=stock;Integrated Security=True");
    //insert logic
    con.Open();
    if(textboxProductID.Text == "" || textboxProductName.Text == "")
    {
        MessageBox.Show("You have to enter either a product ID or product name", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    else
    {
        bool status = false;
        if (comboboxStatus.SelectedIndex == 0)
        {
            status = true;
        }
        else
        {
            status = false;
        }

        var sqlQuery = "";

        if (IfProductsExists(con, textboxProductID.Text))
        {
            sqlQuery = @"UPDATE [Products] SET [ProductName] = '" + textboxProductName.Text + "' ,[ProductStatus] = '" + status + "' WHERE [ProductID] = '" + textboxProductID.Text + "'";
        }
        else
        {
            sqlQuery = @"INSERT INTO [Stock].[dbo].[Products] ([ProductID],[ProductName],[ProductStatus]) VALUES
                    ('" + textboxProductID.Text + "','" + textboxProductName.Text + "','" + status + "')";
        }

        SqlCommand cmd = new SqlCommand(sqlQuery, con);
        cmd.ExecuteNonQuery();
        con.Close();


        textboxProductID.Clear();
        textboxProductName.Clear();

        LoadData();
    }

}
加载数据
功能:

public void LoadData()
{
    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-HNR3NJB\mysql;Initial Catalog=stock;Integrated Security=True");

    //reading data from sql
    SqlDataAdapter sda = new SqlDataAdapter("Select * From [stock].[dbo].[Products]", con);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    dataGridView1.Rows.Clear();
    foreach (DataRow item in dt.Rows)
    {
        int n = dataGridView1.Rows.Add();
        dataGridView1.Rows[n].Cells[0].Value = item["ProductID"].ToString();
        dataGridView1.Rows[n].Cells[1].Value = item["ProductName"].ToString();
        if ((bool)item["ProductStatus"])
        {
            dataGridView1.Rows[n].Cells[2].Value = "Active";
        }
        else
        {
            dataGridView1.Rows[n].Cells[2].Value = "Deactive";
        }
    }
}
您还需要
IfProductsExists
方法:

private bool IfProductsExists(SqlConnection con, string productCode)
{
    SqlDataAdapter sda = new SqlDataAdapter("Select 1 From [Products] WHERE [ProductID]='" + productCode + "'", con);
    DataTable dt = new DataTable();
    if (dt.Rows.Count > 0)
        return true;
    else
        return false;
}

这将是一个库存系统,将用于It设备的销售和库存管理。

“您可以清楚地看到它没有工作”-我们为什么可以清楚地看到它没有工作?这不是您问题的答案,但请使用参数-您当前的代码易受sql注入攻击。有人可能会在您的文本框中输入正确的SQL脚本,从而删除您的数据库。请查找文章好的,我为我的措辞道歉,你不能清楚地看到它不起作用,但它不起作用。非常感谢您提供了这篇文章的链接,我一定会查看它的更多应用程序,但是我相信我应该可以接受这个应用程序,因为它目前只供我和我的老板在工作中使用。当然,如果我要发布这个应用程序,我会关注它。你能解释一下什么不起作用吗?代码的哪一部分?是无法从数据库中删除记录,还是只是显示问题?请提供问题的更多详细信息,以便我们可以帮助您。如果只是DataGrid未更新的问题-您需要通过执行以下命令显式更新它:dataGridView1.update();