一、声明窗体级变量:
SqlDataAdapter myDaGoodRepPric = new SqlDataAdapter();
DataSet myDsGoodRepPric = new DataSet();
SqlParameter param = new SqlParameter();
SqlConnection myConnection;
MMS.Public.DbAccLibry.DbAccess myDbacc;
二、窗体构造代码:
myDbacc = new MMS.Public.DbAccLibry.DbAccess();
myDbacc.DatabaseInint(MMS.Public.Common.SysCurOperInfo.CurDataBaseType, MMS.Public.Common.SysCurOperInfo.CurDataBaseConnectionStr);
myConnection =(SqlConnection ) myDbacc.GetCurConnection();
三、窗体加载代码:
using (myDaGoodRepPric = new SqlDataAdapter("select * from dbo.Sys_BcInfo ", myConnection))
{
myDaGoodRepPric.Fill(myDsGoodRepPric);
dataGridView1.DataSource = myDsGoodRepPric.Tables[0];
}
四、保存代码:
myDaGoodRepPric.UpdateCommand = new SqlCommand("update Sys_BcInfo set bcName=@v_bcName where bcid=@v_bcid ", myConnection);
param = myDaGoodRepPric.UpdateCommand.Parameters.Add("@v_bcName", SqlDbType.VarChar, 10, "bcName");
param.SourceVersion = DataRowVersion.Current;
param = myDaGoodRepPric.UpdateCommand.Parameters.Add("@v_bcid", SqlDbType.VarChar, 10, "bcid");
param.SourceVersion = DataRowVersion.Original;
myDaGoodRepPric.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
myDaGoodRepPric.UpdateBatchSize = 10;
if (myDsGoodRepPric.HasChanges())
{
MessageBox.Show("保存成功!");
myDaGoodRepPric.Update(myDsGoodRepPric.Tables[0]); //位置A
myDsGoodRepPric.AcceptChanges(); //位置B
}
注意:如果位置A与位置B颠倒则不能成功执行