0

我是 C# 的初学者并使用三层编程。我无法通过数据网格视图更新或删除记录。以下是我的代码。请帮我。

达尔

 namespace DAL
    {
        public class TblkalaDal
        {
            SqlConnection cn=new SqlConnection("Data Source=(local); Initial Catalog=store;Integrated Security=True");
            public DataTable Getdata()
            {
                try
                {
                    DataSet ds=new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter("Sptblkala_getdata",cn);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.Fill(ds, "Sptblkala_getdata");
                    return ds.Tables["Sptblkala_getdata"];
                }
                catch (Exception)
                {
                    return null;
                    throw;
                }
            }
    public string createkala(int kala_id,string kala_name,int kala_qty,int kala_orderpoint)
    {
        try
        {
            SqlCommand cmd = new SqlCommand("Sptblkala_insert", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@kala_id", kala_id);
            cmd.Parameters.AddWithValue("@kala_name", kala_name);
            cmd.Parameters.AddWithValue("@kala_qty", kala_qty);
            cmd.Parameters.AddWithValue("@kala_orderpoint", kala_orderpoint);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            return "true";
        }
        catch (Exception ex)
        {
            return ex.Message;
            throw;
        }

    }
            public string Deletekala(int kala_id)
            {
                try
                {
                    SqlCommand dm = new SqlCommand("Sptblkala_delete", cn);
                    dm.CommandType = CommandType.StoredProcedure;
                    dm.Parameters.AddWithValue("@kala_id", kala_id);
                    cn.Open();
                    dm.ExecuteNonQuery();
                    cn.Close();
                    return "true";
                }
                catch (Exception ex)
                {
                 return   ex.Message;
                    throw;
                }

            }

            public string updatekala(int kala_id,string kala_name,int kala_qty,int kala_orderpoint)
            {
                try
                {
                    SqlCommand uk = new SqlCommand("Sptblkala_update", cn);
                    uk.CommandType = CommandType.StoredProcedure;
                    uk.Parameters.AddWithValue("@kala_id", kala_id);
                    uk.Parameters.AddWithValue("@kala_name", kala_name);
                    uk.Parameters.AddWithValue("@kala_qty", kala_qty);
                    uk.Parameters.AddWithValue("@kala_orderpoint", kala_orderpoint);
                    cn.Open();
                    uk.ExecuteNonQuery();
                    cn.Close();
                    return "true";
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
    }

BLL

namespace BLL
{
    public class bllkala
    {
        TblkalaDal tk=new TblkalaDal();
        public string createkala(int kala_id,string kala_name,int kala_qty,int kala_orderpoint)
        {
            return tk.createkala(kala_id, kala_name, kala_qty, kala_orderpoint);
        }
        public string deletekala(int kala_id)
        {
            return tk.Deletekala(kala_id);
        }
        public string updatekala(int kala_id,string kala_name,int kala_qty,int kala_orderpoint)
        {
            return tk.updatekala(kala_id, kala_name, kala_qty, kala_orderpoint);
        }
        public DataTable Getdata()
        {
          return  tk.Getdata();
        }
    }
}

介绍

namespace store
{
    public partial class kala : Form
    {
        public kala()
        {
            InitializeComponent();
        }

       bllkala bk=new bllkala();
        DataTable dt=new DataTable();

        private void btnkalainsert_Click(object sender, EventArgs e)
        {
            string check = bk.createkala(int.Parse(txtkalacode.Text), txtkalaname.Text, int.Parse(txtkqty.Text),
                                         int.Parse(txtkalapoint.Text));
            if (check=="true")
            {
                dt = bk.Getdata();
                dataGridViewkala.DataSource = dt;
            }
            else
            {
                MessageBox.Show(check);
            }
        }

        private void kala_Load(object sender, EventArgs e)
        {
            dt = bk.Getdata();
            dataGridViewkala.DataSource = dt;
        }

        private void btnkaladel_Click(object sender, EventArgs e)
        {
            dataGridViewkala.Rows.RemoveAt(dataGridViewkala.CurrentRow.Index);
            dt = bk.deletekala(int.Parse(txtkalacode.Text));
            dataGridViewkala.DataSource = dt;
        }
    }
}
4

2 回答 2

0

这种语法通常有效:http://www.w3schools.com/sql/sql_update.asp 基本上构建一个 sql 查询并让数据库执行它。这种方法的好处是您可以先在数据库管理程序中测试查询。

class Database
{
    DataGridView grid = new DataGridView();
    System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection();

    public Database()
    {
        grid.CellEndEdit += new DataGridViewCellEventHandler(grid_CellEndEdit);
        grid.UserDeletedRow += new DataGridViewRowEventHandler(grid_UserDeletedRow);
    }

    void grid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        string query = string.Format(
            "UPDATE Sptblkala {0}='{1}' WHERE kala_id={2}",
            grid.Columns[e.ColumnIndex].Name, grid[e.ColumnIndex, e.RowIndex].Value,
            grid[0, e.RowIndex].Value);

        try
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query,cn);
            cmd.CommandType = System.Data.CommandType.Text;
            cn.Open();
            cmd.ExecuteScalar();
            cn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace);
        }
    }

    void grid_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
    {
        string query = "DELETE FROM Sptblkala WHERE kala_id=" + e.Row.Cells[0].Value;

        try
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query, cn);
            cmd.CommandType = System.Data.CommandType.Text;
            cn.Open();
            cmd.ExecuteScalar();
            cn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace);
        }
    }
}
于 2012-10-08T13:14:50.583 回答
0

我在表示层找到了代码:

private void btnkaladel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure to delete?", "Deleting...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {

                dataGridViewkala.Rows.RemoveAt(dataGridViewkala.CurrentRow.Index);
                bk.deletekala(int.Parse(txtkalacode.Text));
                dataGridViewkala.DataSource = dt;
                txtkalacode.Text = null;
                txtkalaname.Text = null;
                txtkalapoint.Text = null;
                txtkqty.Text = null;
            }
        }



        private void dataGridViewkala_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtkalacode.Text = dataGridViewkala.Rows[e.RowIndex].Cells[0].Value.ToString();
            txtkalaname.Text = dataGridViewkala.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtkqty.Text = dataGridViewkala.Rows[e.RowIndex].Cells[2].Value.ToString();
            txtkalapoint.Text = dataGridViewkala.Rows[e.RowIndex].Cells[3].Value.ToString();
        }

        private void btnedit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure to edit?", "editing...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                bk.updatekala(int.Parse(txtkalacode.Text), txtkalaname.Text, int.Parse(txtkqty.Text),
                              int.Parse(txtkalapoint.Text));
                dt = bk.Getdata();
                dataGridViewkala.DataSource = dt;
                txtkalacode.Text = null;
                txtkalaname.Text = null;
                txtkalapoint.Text = null;
                txtkqty.Text = null;
            }
        }


    }
于 2012-10-08T15:33:20.623 回答