如何在Web中对dataGrid中的数据进行修改,并更新到数据库中 加上SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 还是直接看msdn的示例吧 很详细了 朋友: SqlDataAdapter实例的UpdateCommand的用法是: 获取或设置一个 Transact-SQL 语句或存储过程,用于更新数据源中的记录。 你上面的写法有一个严重的问题:当两个人同时访问你的程序时,更新时就要错位了,你仔细考虑一下是不是? 建议你利用存储过程或SQL语句来更新,利用一个或几个关键字来更新 定义一个Sqlcommand对象,给它Transact-SQL 语句或存储过程,再把SqlDataAdapter的UpdateCommand指向这个Sqlcommand就OK了. to panning:我利用你的那句话,倒是更新了数据,可是数据库中的其它数据也都变成同样的数据了,这是为什么呢?to lightnings:我试过好几种方法了,有一种代码如下,但提示索引出错,请予以指教 SqlConnection MyConnection=new SqlConnection(); MyConnection.ConnectionString="server=ICELEAN;database=Score;uid=sa;pwd=1982"; SqlDataAdapter MyAda=new SqlDataAdapter("select * from score",MyConnection); DataSet MyDataSet=new DataSet(); MyAda.Fill(MyDataSet,"score"); DataGrid1.DataSource =MyDataSet; DataGrid1.DataSource=dataSet11; DataGrid1.DataKeyField=ID; string ksbh,xm, zzll,wgy,ywk1,ywk2,zf; string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); TextBox tb; tb = (TextBox)(e.Item.Cells[2].Controls[0]); ksbh= tb.Text; tb = (TextBox)(e.Item.Cells[3].Controls[0]); xm = tb.Text; tb = (TextBox)(e.Item.Cells[4].Controls[0]); zzll = tb.Text; tb = (TextBox)(e.Item.Cells[5].Controls[0]); wgy= tb.Text; tb = (TextBox)(e.Item.Cells[6].Controls[0]); ywk1 = tb.Text; tb = (TextBox)(e.Item.Cells[7].Controls[0]); ywk2= tb.Text; tb = (TextBox)(e.Item.Cells[8].Controls[0]); zf= tb.Text; DataSet1.scoreRow r; r = dataSet11.score.FindByID(int.Parse(key)); r.KSBH = ksbh; r.XM = xm; r.ZZLL=System.Convert.ToInt32(zzll); r.WGY=System.Convert.ToInt32(wgy); r.YWK1=System.Convert.ToInt32(ywk1); r.YWK2=System.Convert.ToInt32(ywk2); r.ZF=System.Convert.ToInt32(zf); sqlDataAdapter1.Update(dataSet11); DataGrid1.EditItemIndex = -1; DataGrid1.DataBind(); 换种方式试试看!string id(你自己数据中的ID) = DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim(); string ksbn = ((TextBox)e.Item.Cells[2].Controls[0]).Text; …中间省略… string zf = ((TextBox)e.Item.Cells[8].Controls[0]).Text;string strUpd = "更新语句";SqlConnection con = new SqlConnection("server=ICELEAN;database=Score;uid=sa;pwd=1982");SqlCommand com = new SqlCommand(strUpd,con); com.CommandTimeout=15; com.CommandType=CommandType.Text; try { con.Open(); com.ExecuteNonQuery(); con.Close(); } catch(Exception ex) { throw ex; }…… 建议用到的SQL语句,采用存储过程,比在程序中写SQL要好的多! 我给你写了一段:SqlConnection MyConnection=new SqlConnection();MyConnection.ConnectionString="server=ICELEAN;database=Score;uid=sa;pwd=1982";SqlDataAdapter MyAda=new SqlDataAdapter();MyAda.UpdateCommand=new SqlCommand("update score set KSBH=@KSBH,XM=@XM,ZZLL=@ZZLL,WGY=@WGY,YWK1=@YWK1,YWK2=@YWK2,ZF=@ZF where 关键字=@关键字",MyConnection);MyConnection.Open();MyAda.UpdateCommand.Parameters.Add("@KSBH", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[2].Controls[0]).Text );MyAda.UpdateCommand.Parameters.Add("@XM", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[3].Controls[0]).Text );MyAda.UpdateCommand.Parameters.Add("@ZZLL", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[4].Controls[0]).Text );MyAda.UpdateCommand.Parameters.Add("@WGY", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[5].Controls[0]).Text );MyAda.UpdateCommand.Parameters.Add("@YWK1", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[6].Controls[0]).Text );MyAda.UpdateCommand.Parameters.Add("@YWK2", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[7].Controls[0]).Text );MyAda.UpdateCommand.Parameters.Add("@ZF", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[8].Controls[0]).Text );MyAda.UpdateCommand.Parameters.Add("@关键字", SqlDbType.VarChar, 50,??你自已去设置);int Result=MyAda.UpdateCommand.ExecuteNonQuery(); //Result为影响的行数MyAda.SelectCommand=new SqlCommand("select * from score",MyConnection); DataSet MyDataSet=new DataSet(); MyAda.Fill(MyDataSet,"score");DataGrid1.DataSource =MyDataSet;DataGrid1.DataBind(); 谢谢大家的帮忙,等我调完会把分给各位热心帮忙的人我不太会用存储过程,另外问一下,怎样用DataRow 来定义一个与指定数据库表有关的newRow呢? 我用过 foreach(DataRow newRow7 in MyDataSet.Tables["score"].Rows),但这样会把所有的数据都更新了,怎样只更新一行呢?to lightnings我利用你的方法,但提示说(@KSBH varchar(50),@XM varchar(50),@ZZLL varchar(50),@WGY varcha' 需要参数 @KSBH,但未提供该参数。 你把它更改为:MyAda.UpdateCommand.Parameters.Add("@KSBH", SqlDbType.VarChar, 50).value=(TextBox)(e.Item.Cells[2].Controls[0]).Text ;其它的也是 找MSDN上有实例,或者找一本书,都讲的有 怎样用DataRow 来定义一个与指定数据库表有关的newRow呢?数据集对象名(不是实例名!).表的名称.NewRow() 有网络推广经验的高手请指教。。。 随机色? 小小问题? C# 快速排序法 导出为Excel文件的时候,怎么控制某一行或者某一列合并 寻求一方法,路过的来指点下! SQL Server2003SQL Server2003SQL Server2003SQL Server2003 Componentone 中Reports表格问题。 c# 如何让textbox 只能输入十六进制? 《C#高级编程简体中文版》那里有下载? 在客户端导出报表怎么不行呢???高分求助!!高手帮忙啊!! (无分求助)关于树的底层节点的URL导航事件(无分求助)
SqlDataAdapter实例的UpdateCommand的用法是: 获取或设置一个 Transact-SQL 语句或存储过程,用于更新数据源中的记录。
你上面的写法有一个严重的问题:当两个人同时访问你的程序时,更新时就要错位了,你仔细考虑一下是不是?
建议你利用存储过程或SQL语句来更新,利用一个或几个关键字来更新
我利用你的那句话,倒是更新了数据,可是数据库中的其它数据也都变成同样的数据了,这是为什么呢?
to lightnings:
我试过好几种方法了,有一种代码如下,但提示索引出错,请予以指教 SqlConnection MyConnection=new SqlConnection();
MyConnection.ConnectionString="server=ICELEAN;database=Score;uid=sa;pwd=1982";
SqlDataAdapter MyAda=new SqlDataAdapter("select * from score",MyConnection);
DataSet MyDataSet=new DataSet();
MyAda.Fill(MyDataSet,"score");
DataGrid1.DataSource =MyDataSet;
DataGrid1.DataSource=dataSet11;
DataGrid1.DataKeyField=ID;
string ksbh,xm, zzll,wgy,ywk1,ywk2,zf;
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
TextBox tb; tb = (TextBox)(e.Item.Cells[2].Controls[0]);
ksbh= tb.Text;
tb = (TextBox)(e.Item.Cells[3].Controls[0]);
xm = tb.Text;
tb = (TextBox)(e.Item.Cells[4].Controls[0]);
zzll = tb.Text; tb = (TextBox)(e.Item.Cells[5].Controls[0]);
wgy= tb.Text;
tb = (TextBox)(e.Item.Cells[6].Controls[0]);
ywk1 = tb.Text;
tb = (TextBox)(e.Item.Cells[7].Controls[0]);
ywk2= tb.Text;
tb = (TextBox)(e.Item.Cells[8].Controls[0]);
zf= tb.Text;
DataSet1.scoreRow r;
r = dataSet11.score.FindByID(int.Parse(key));
r.KSBH = ksbh;
r.XM = xm;
r.ZZLL=System.Convert.ToInt32(zzll);
r.WGY=System.Convert.ToInt32(wgy);
r.YWK1=System.Convert.ToInt32(ywk1);
r.YWK2=System.Convert.ToInt32(ywk2);
r.ZF=System.Convert.ToInt32(zf);
sqlDataAdapter1.Update(dataSet11);
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
string id(你自己数据中的ID) = DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim();
string ksbn = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
…中间省略…
string zf = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
string strUpd = "更新语句";
SqlConnection con = new SqlConnection("server=ICELEAN;database=Score;uid=sa;pwd=1982");
SqlCommand com = new SqlCommand(strUpd,con); com.CommandTimeout=15;
com.CommandType=CommandType.Text; try
{
con.Open();
com.ExecuteNonQuery();
con.Close();
}
catch(Exception ex)
{
throw ex;
}
……
SqlConnection MyConnection=new SqlConnection();
MyConnection.ConnectionString="server=ICELEAN;database=Score;uid=sa;pwd=1982";
SqlDataAdapter MyAda=new SqlDataAdapter();
MyAda.UpdateCommand=new SqlCommand("update score set KSBH=@KSBH,XM=@XM,ZZLL=@ZZLL,WGY=@WGY,YWK1=@YWK1,YWK2=@YWK2,ZF=@ZF where 关键字=@关键字",MyConnection);
MyConnection.Open();
MyAda.UpdateCommand.Parameters.Add("@KSBH", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[2].Controls[0]).Text );
MyAda.UpdateCommand.Parameters.Add("@XM", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[3].Controls[0]).Text );
MyAda.UpdateCommand.Parameters.Add("@ZZLL", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[4].Controls[0]).Text );
MyAda.UpdateCommand.Parameters.Add("@WGY", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[5].Controls[0]).Text );
MyAda.UpdateCommand.Parameters.Add("@YWK1", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[6].Controls[0]).Text );
MyAda.UpdateCommand.Parameters.Add("@YWK2", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[7].Controls[0]).Text );
MyAda.UpdateCommand.Parameters.Add("@ZF", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[8].Controls[0]).Text );
MyAda.UpdateCommand.Parameters.Add("@关键字", SqlDbType.VarChar, 50,??你自已去设置);
int Result=MyAda.UpdateCommand.ExecuteNonQuery(); //Result为影响的行数
MyAda.SelectCommand=new SqlCommand("select * from score",MyConnection);
DataSet MyDataSet=new DataSet();
MyAda.Fill(MyDataSet,"score");
DataGrid1.DataSource =MyDataSet;
DataGrid1.DataBind();
我用过 foreach(DataRow newRow7 in MyDataSet.Tables["score"].Rows),但这样会把所有的数据都更新了,怎样只更新一行呢?to lightnings
我利用你的方法,但提示说(@KSBH varchar(50),@XM varchar(50),@ZZLL varchar(50),@WGY varcha' 需要参数 @KSBH,但未提供该参数。
MyAda.UpdateCommand.Parameters.Add("@KSBH", SqlDbType.VarChar, 50).value=(TextBox)(e.Item.Cells[2].Controls[0]).Text ;
其它的也是