我是 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;
}
}
}