问题描述
C#datagirdview中如何将修改后的数据通过点击button保存到数据库中,要代码......
解决方案
解决方案二:
/*在使用SqlCommandBuilder和SqlDataAdapter更新数据,必须保存数据库里有主键,否则不会自动生成deleteCommand和updateCommand也就是只能增加不能修改和删除!*/privatevoidbutton1_Click(objectsender,EventArgse){SqlConnectionconn=newSqlConnection(connString);SqlDataAdapterada=newSqlDataAdapter(sql,conn);SqlCommandBuildercb=newSqlCommandBuilder(ada);ada.Update(ds);dataGridView1.Update();}
解决方案三:
准备工作,声明窗体级全局变量:privateDataSetds=newDataSet();privateSqlDataAdaptersda=newSqlDataAdapter();privateSqlConnectionmyConn=newSqlConnection();1、利用SqlCommandBuilderSqlCommandBuildersb=newSqlCommandBuilder(sda);sda.Update(ds.Tables[0]);ds.Tables[0].AcceptChanges();2、指定UpdateCommandSqlParameterparam=newSqlParameter();stringstrSql="updateGood_PrtyInfosetmaxstcs=@maxstcs,minstcs=@minstcswhere1=1andGoodID=@GoodID";sda.UpdateCommand=newSqlCommand(strSql,myConn);param=sda.UpdateCommand.Parameters.Add("@maxstcs",SqlDbType.VarChar,50,"maxstcs");param.SourceVersion=DataRowVersion.Current;param=sda.UpdateCommand.Parameters.Add("@minstcs",SqlDbType.VarChar,50,"minstcs");param.SourceVersion=DataRowVersion.Current;//param=myDA.UpdateCommand.Parameters.Add("@DeptID",SqlDbType.VarChar,50,"DeptID");//param.SourceVersion=DataRowVersion.Current;param=sda.UpdateCommand.Parameters.Add("@GoodID",SqlDbType.VarChar,50,"GoodID");param.SourceVersion=DataRowVersion.Original;sda.UpdateCommand.UpdatedRowSource=UpdateRowSource.None;sda.UpdateBatchSize=10;//SqlCommandBuildersb=newSqlCommandBuilder(sda);sda.Update(ds.Tables[0]);ds.Tables[0].AcceptChanges();本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/emailqjc/archive/2010/05/18/5605107.aspx
解决方案四:
1楼的很明了