winform datagridview 数据保存
假设MaterielRegiser_Number,MaterielRegiser_Name,MaterielRegiser_Model,MaterielRegiser_Unit是数据库的几个字段,我如何将datagridview 里用户填的数据保存进数据库!
------解决方案--------------------
------解决方案--------------------如果没有结构上的太多改动的话,可以用dataAdapter和selectCommand一起,然后用update进行更新,比较快一些:
/// <summary>
/// 根据传入的sql语句以及对应生成的数据表对数据进行更新
/// </summary>
/// <param name="dt">修改后的datatable</param>
/// <param name="strSql">生成的sql</param>
/// <returns></returns>
public bool UpdateDb(DataTable dt,string strSql)
{
Open();
if (!IsOpen) return false;
DataTable dtTemp = new DataTable();
lock (this)
{
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strSql, m_OleCon);
adapter.SelectCommand = new OleDbCommand(strSql, m_OleCon);
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
m_Mutex.WaitOne();
// DataSet ds = new DataSet();
// SqlDataAdapter sda;
//
// SqlCommandBuilder scb = new SqlCommandBuilder(sda);
// sda.Update(ds);
// this.dataGridView1.DataSource = ds.Tables[0];
try
{
adapter.Fill(dtTemp);
dtTemp = dt;
adapter.Update(dt);
}
catch (Exception Ex)
{
OnError("COleDbOperate.GetDataSet(" + strSql + ")", Ex);
return false;
}
finally
{
adapter.Dispose();
}
m_Mutex.ReleaseMutex();
return true;
}