我在一个Form上放有4个textBox控件,和四个按钮分别是"检索","增加","删除","更新"。现在我已通过"检索"按钮把数据查询出来了,并显示在四个textBox上了。目前,
第一:我想通过按"增加"按钮后在4个textBox增加一空白记录,然后输入数据后再按"更新"保存到数据库;第二:我想通过"删除"按钮来删除当前记录,然后输入数据后再按"更新"保存到数据库;请问在"增加"、"删除"、"更新"按钮事件中应该如何写代码?
第一:我想通过按"增加"按钮后在4个textBox增加一空白记录,然后输入数据后再按"更新"保存到数据库;第二:我想通过"删除"按钮来删除当前记录,然后输入数据后再按"更新"保存到数据库;请问在"增加"、"删除"、"更新"按钮事件中应该如何写代码?
解决方案 »
- C#实现Discuz!论坛发贴,主题分类select怎么自动完成?
- 这个问题有解吗?关于抓取网页源代码的
- 加入了updatapanel,如何保持dropdownlist已选择的值
- Microsoft.Rtc.Sip.UnauthorizedException类型的异常,100分
- WebClient怎么设置"操作超时"时间?
- 如何实现页面在每天的固定时间段里自动刷新,不在这个时间段则不刷新。多谢了!
- 正则使用平衡组,匹配函数体
- C#中对2个word文档进行比较遇到问题,求助
- 一个简单的问题
- 求学C#的详细进程
- 《Windows程序设计中文版》下载地址,地址见内
- 在C#中如何获得进程的CPU利用率
DS数据集,DA数据适配器增加
this.BindingContext(this.DS,"table_name").AddNew();删除和修改
this.BindingContext(this.DS,"table_name").RemoveAt(this.Bindingtext(this.DS,"table_name").Position);DA.Update(DS,"table_name");
DS.AcceptChanges();
我的表里有三个字段,第一个是主键,int型,其它为字符串
更新和删除之前你可以添加测试代码以测试是不是存在相应的记录//////////////////////////////////////////////删除
private void button3_Click(object sender, System.EventArgs e)
{
SqlConnection myConnection=new SqlConnection("Server=localhost;Database=stu;Trusted_Connection=yes");
string delestr="delete articletype where atid="+this.textBox2.Text+" and articlestyle='"+this.textBox3.Text+"' and asdescription='"+this.textBox4.Text+"'";
SqlCommand listMess=new SqlCommand(delestr,myConnection);
try
{
listMess.Connection.Open();
listMess.ExecuteNonQuery();MessageBox.Show("delete success");}
catch(Exception ee){MessageBox.Show("delete failed!"+ee.Message);}
}
//////////////////////////////////////////////////////////////////添加
private void button5_Click(object sender, System.EventArgs e)
{
SqlConnection myConnection=new SqlConnection("Server=localhost;Database=stu;Trusted_Connection=yes");
string insertstr="insert into articletype values("+this.textBox2.Text+",'"+this.textBox3.Text+"','"+this.textBox4.Text+"')";
SqlCommand listMess=new SqlCommand(insertstr,myConnection);
try
{
listMess.Connection.Open();
listMess.ExecuteNonQuery();MessageBox.Show("add success");}
catch(Exception ee){MessageBox.Show("update failed!"+ee.Message);}
}
//////////////////////////////////////////////////////更新
private void button4_Click(object sender, System.EventArgs e)
{
SqlConnection myConnection=new SqlConnection("Server=localhost;Database=stu;Trusted_Connection=yes");
string updatestr="update articletype set articlestyle='"+this.textBox3.Text+"',asdescription='"+this.textBox4.Text+"' where atid="+this.textBox2.Text;
SqlCommand listMess=new SqlCommand(updatestr,myConnection);
try
{
listMess.Connection.Open();
listMess.ExecuteNonQuery();MessageBox.Show("update success");}
catch(Exception ee){MessageBox.Show("update failed!"+ee.Message);}
}
我试了一下你所说的增加记录的方法,现在是可以增加一空白记录了。但我在这些空记录上输入机关内容后再按保存按钮时却没有把我填的数据保存而是在数据库中保存了一条空记录,请问是怎么回事?
DS数据集,DA数据适配器增加
this.BindingContext(this.DS,"table_name").AddNew();删除和修改
this.BindingContext(this.DS,"table_name").RemoveAt(this.Bindingtext(this.DS,"table_name").Position);DA.Update(DS,"table_name");
DS.AcceptChanges();