用C#删除mysql表项

时间:2017-07-15 17:22:29

标签: c# mysql sql-delete

基本上,我想删除一个条目形成一个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();
    }

}

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设备的销售和库存管理。

0 个答案:

没有答案