一个己问了许多次的问题.我还是不能解决呀.帮下吧.大虾们.

binaryfox 2003-07-30 02:23:12
我用一dataset存放了从数据库中读出的一个表.然后在一个FORM上建上许多textbox用于显示从dataset中读出的记录各项.并且我把各textbox与各项进行了数据绑定.同时也想通过这个form对数据库的表进行插入,修改,删除操作.当我插入一个新记录时,只能写到我内存中的dataset中,而不能插入到我的数据库中.(我用了update操作)同时,会修改我内存中的其他记录.比如说:当在显示记录A之后,进行了插入记录B操作,会把dataset中A与B两条记录修改成一样.那位大虾帮我了答一下.我把代码给出了.

rivate void button2_Click(object sender, System.EventArgs e)
{
if(button2.Text!="确定")
{textBox1.Text="";
this.label12.Text="?/?";
textBox1.Text+=(this.BindingContext[productdataset.Tables["product"]].Count)+1;
temp=textBox1.Text;
textBox2.Text="";textBox3.Text="";textBox4.Text="";textBox5.Text="";textBox6.Text="";textBox7.Text="";textBox8.Text="";textBox9.Text="";textBox10.Text="";//把所有的textbox清空.
button1.Enabled=false;button3.Enabled=false;button5.Enabled=false;button6.Enabled=false;button7.Enabled=false;button8.Enabled=false;button2.Text="确定";
}
else
{
DataRow dm=productdataset.Tables["product"].NewRow();//定义一新行.
productdataset.Tables["product"].Rows.Add(dm);
CurrencyManager tem=(CurrencyManager)BindingContext[productdataset];
tem.Position=(this.BindingContext[productdataset.Tables["product"]].Count)-1;
try
{//进行负值.
dm["productid"]=temp;
dm["productname"]=textBox2.Text;dm["productsorport"]=textBox3.Text;
dm["prodecttype"]=textBox4.Text;dm["prodectsigl"]=textBox5.Text;dm["prodectrice"]=textBox6.Text;
dm["stordnumber"]=textBox7.Text;
dm["addnumber"]=textBox8.Text;
dm["addaginnumber"]=textBox9.Text;dm["stopornot"]=textBox10.Text;

sqlConnection2.Open();
DataSet dt=productdataset.GetChanges();
sqlDataAdapter1.Update(dt);
productdataset.AcceptChanges();
sqlConnection2.Close();
this.label12.Text=(this.BindingContext[productdataset.Tables["product"]].Position+1)+"/"+(this.BindingContext[productdataset.Tables["product"]].Count);

button1.Enabled=true;button3.Enabled=true;button5.Enabled=true;button6.Enabled=true;button7.Enabled=true;button8.Enabled=true;button2.Text="插入";}
catch(Exception d)
{MessageBox.Show(d.Message,"错误");}
...全文
18 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪狼1234567 2003-07-30
  • 打赏
  • 举报
回复
参考如下的代码吧:
首先取得数据,放到DataGrid里

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"student");

然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
再绑定到某一个TextBox
this.textBox10.DataBindings.Add("Text",this.dt,"student.stuno");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"student"].AddNew();
删除:
this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
最后把结果写回数据库:


// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)";
this.sqlInsertCommand1.Connection = this.conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
this.sqlUpdateCommand1.Connection = this.conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
this.sqlDeleteCommand1.Connection = this.conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDa.InsertCommand = this.sqlInsertCommand1;
this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
try
{
sqlDa.Update(dt.GetChanges,"student");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{

return false;
}
finally
{
conn.Close();
}
remyxu 2003-07-30
  • 打赏
  • 举报
回复
*****把这句
SqlCommandBuilder myCommandBuider = new SqlCommandBuider(dataAdapterptfinfo);
加在UPDATE前面!
看看我以前的情况,

*****更好的方法是,直接使用SQL语句,简洁明快,容易理解!
例子:
UPDATE:

myConnection = new System.Data.SqlClient.SqlConnection
(System.Configuration.ConfigurationSettings.AppSettings["connectionstring"]);
myConnection.Open();

string renewstr = "UPDATE ptf_ptfinfo SET "
+ "cptf_testreq = '" + TextBox_testreq.Text+ "'"
+ " WHERE cptf_no = "
+ "'" + TextBox_ptfno.Text + "'";
SqlCommand renewcmd = new SqlCommand(renewstr,myConnection);
int rn = renewcmd.ExecuteNonQuery();
短短3句就OK!
donger2000 2003-07-30
  • 打赏
  • 举报
回复
1、你最好要为sqlDataAdapter1设定InsertCommand的SQL语句!(也可能用SQLCOMMANDBUILDER(好象是这个)自动生成)

2、tem.Position=(this.BindingContext[productdataset.Tables["product"]].Count)-1;
这一句不可靠,因为这样你的当前记录并不一定就是在新记录中的,如果有排序的话,很可能你的新记录是在最前面喔!
相关推荐
发帖
C#

10.9w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2003-07-30 02:23
社区公告

让您成为最强悍的C#开发者