从datagridview中删除数据库

时间:2014-07-14 19:14:22

标签: c# mysql database datagridview sql-delete

我在网站上遇到了很多其他主题,似乎无法让这个东西发挥作用。我正在创建一个库存数据库程序来跟踪我的枪支。我有一个包含DataGridView(ammoDGV)和删除按钮的表单(AmmunitionForm.cs)。我可以从网格中删除它,但我似乎无法获得影响数据库的更改。这是关于这个主题的知识的边缘,以记住这一点。这是我目前的代码(请注意删除按钮方法):

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Data.Sql;

    namespace Firearm_Asset_Management_Database
    {
      public partial class AmmunitionForm : Form
    {
        public AmmunitionForm()
       {
            InitializeComponent();
       }

      private void AmmunitionForm_Load(object sender, EventArgs e)
      {
        // TODO: This line of code loads data into the         'fAMDatabaseDataSet.AmmunitionTable' table. You can move, or remove it, as needed.
        this.ammunitionTableTableAdapter.Fill(this.fAMDatabaseDataSet.AmmunitionTable);

    }

    private void addAmmoButton_Click(object sender, EventArgs e)
    {
        NewAmmunition newAmmunition = new NewAmmunition();
        newAmmunition.Show();
    }

    private void refreshButton_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\brmcbrid\documents\visual studio 2010\Projects\Firearm Asset Management Database\Firearm Asset Management Database\FAMDatabase.mdf;Integrated Security=True;User Instance=True");
        string query = "select * from AmmunitionTable";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        ammoDGV.DataSource = dt;
    }

    private void closeButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void deleteButton_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\brmcbrid\documents\visual studio 2010\Projects\Firearm Asset Management Database\Firearm Asset Management Database\FAMDatabase.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand();
        if (ammoDGV.Rows.Count > 1 && ammoDGV.SelectedRows[0].Index != ammoDGV.Rows.Count - 1)
        {
            cmd.CommandText = "DELETE FROM AmmunitionTable WHERE Caliber=" + ammoDGV.SelectedRows[0].Cells[0].Value.ToString() + "";
            con.Open();
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            con.Close();
            ammoDGV.Rows.RemoveAt(ammoDGV.SelectedRows[0].Index);
            MessageBox.Show("Row has been Deleted");
        }
      }
    }
 }

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

你的第一栏究竟是什么?我认为它的水准。它在数据库中保存为整数吗?如果没有,您应该在SQL命令中插入单个配额,如下所示:

"DELETE FROM AmmunitionTable WHERE Caliber='" + ammoDGV.SelectedRows[0].Cells[0].Value.ToString() + "'";