首页 > 学院 > 开发设计 > 正文

DataGrid中数据的增删改向数据库提交

2019-11-08 03:17:31
字体:
来源:转载
供稿:网友

PRivate void button1_Click(object sender, System.EventArgs e)  {   //添加   string insStr = "Insert into test values(@vID, @vMC, @vDH, @vCZ, @vDZ)";   sda.InsertCommand = new SqlCommand(insStr, con);

   sPara = sda.InsertCommand.Parameters.Add("@vID", SqlDbType.VarChar);   sPara.SourceColumn = "id";   sPara.SourceVersion = DataRowVersion.Current;

   sPara = sda.InsertCommand.Parameters.Add("@vMC", SqlDbType.VarChar);   sPara.SourceColumn = "mingcheng";   sPara.SourceVersion = DataRowVersion.Current;

   sPara = sda.InsertCommand.Parameters.Add("@vDH", SqlDbType.VarChar);   sPara.SourceColumn = "dianhua";   sPara.SourceVersion = DataRowVersion.Current;

   sPara = sda.InsertCommand.Parameters.Add("@vCZ", SqlDbType.VarChar);   sPara.SourceColumn = "chuanzhen";   sPara.SourceVersion = DataRowVersion.Current;

   sPara = sda.InsertCommand.Parameters.Add("@vDZ", SqlDbType.VarChar);   sPara.SourceColumn = "dizhi";   sPara.SourceVersion = DataRowVersion.Current;

      if (ds.HasChanges())   {    sda.Update(ds, "test");    MessageBox.Show("插入记录OK!");        }  }

  private void button2_Click(object sender, System.EventArgs e)  {   //修改更新   string updStr = "Update test set id = @vIDnew, MingCheng = @vMC, DianHua = @vDH, ChuanZhen = @vCZ, DiZhi = @vCZ  where id = @vIDold";   sda.UpdateCommand = new SqlCommand(updStr, con);

   sPara = sda.UpdateCommand.Parameters.Add("@vIDnew", SqlDbType.VarChar);   sPara.SourceColumn = "id";   sPara.SourceVersion = DataRowVersion.Current;

   sPara = sda.UpdateCommand.Parameters.Add("@vMC", SqlDbType.VarChar);   sPara.SourceColumn = "mingcheng";   sPara.SourceVersion = DataRowVersion.Current;

   sPara = sda.UpdateCommand.Parameters.Add("@vDH", SqlDbType.VarChar);   sPara.SourceColumn = "dianhua";   sPara.SourceVersion = DataRowVersion.Current;

   sPara = sda.UpdateCommand.Parameters.Add("@vCZ", SqlDbType.VarChar);   sPara.SourceColumn = "chuanzhen";   sPara.SourceVersion = DataRowVersion.Current;

   sPara = sda.UpdateCommand.Parameters.Add("@vDZ", SqlDbType.VarChar);   sPara.SourceColumn = "dizhi";   sPara.SourceVersion = DataRowVersion.Current;

   sPara = sda.UpdateCommand.Parameters.Add("@vIDold", SqlDbType.VarChar);   sPara.SourceColumn = "id";   sPara.SourceVersion = DataRowVersion.Original;

   if (ds.HasChanges())   {         sda.Update(ds, "test");          MessageBox.Show("数据更新OK!");   }  }

  private void button3_Click(object sender, System.EventArgs e)  {   //删除   string delStr = "Delete from test where id = @vID";   sda.DeleteCommand = new SqlCommand(delStr, con);

   sPara = sda.DeleteCommand.Parameters.Add("@vID", SqlDbType.VarChar);   sPara.SourceColumn = "id";   sPara.SourceVersion = DataRowVersion.Original;

   if (MessageBox.Show("确实要删除该纪录吗?","系统提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2,0)==DialogResult.Yes)   {       //ds.Tables[0].Rows[DataGrid_khxx.CurrentRowIndex].Delete();    sda.Update(ds, "test");    MessageBox.Show("删除记录OK!");   }  }

  private void button4_Click(object sender, System.EventArgs e)  {   Close();//关闭  }

  private void Form1_Load(object sender, System.EventArgs e)  {   //窗体载入事件   con = new SqlConnection("Server =.; Database = lesson; uid = sa; pwd = ");   con.Open();       sda = new SqlDataAdapter("select * from test", con);      sda.Fill(ds, "test");   con.Close();   DataGrid_khxx.DataSource = ds.Tables["test"];  }

===============================================

 public Cam(OleDbDataAdapter param_adapter, DataTable param_table)        {            g_adapter = param_adapter;            g_table = param_table;            string _strPath = AppDomain.CurrentDomain.BaseDirectory + "users.mdb";            string _strCon = "provider=microsoft.jet.oledb.4.0;data source="+_strPath;            g_con = new OleDbConnection(_strCon);                               }        public void upt(DataRow param_row)        {            string _strValue = "正常卡";            g_adapter.UpdateCommand = new OleDbCommand("update accountPersonInfoTable set cardState='" + _strValue + "' where num=@numValue", g_con);            g_adapter.UpdateCommand.Parameters.Add(_strValue, OleDbType.VarChar, 50, "cardState");            OleDbParameter _parameter= g_adapter.UpdateCommand.Parameters.Add("@numValue",OleDbType.VarChar);            _parameter.SourceColumn = "num";            _parameter.SourceVersion = DataRowVersion.Original;            param_row["cardState"] = _strValue;        }        public void cot()        {            g_adapter.Update(g_table);            g_table.AcceptChanges();        }

=====================

public class CDBOperation    {        private OleDbConnection g_con = null;        OleDbDataAdapter g_adapter = null;        public CDBoperation()        {            string _strPath = AppDomain.CurrentDomain.BaseDirectory + "users.mdb";            string _strCon = "provider=microsoft.jet.oledb.4.0;data source=" + _strPath;            g_con = new OleDbConnection(_strCon);            g_adapter = new OleDbDataAdapter();        }        public void update(DataTable param_newTable)        {            foreach (DataRow item in param_newTable.Rows)            {                if (item.RowState == DataRowState.Added)                {                    g_adapter.InsertCommand = new OleDbCommand("insert into machineSetTable (machineNum1,position1,communicateMode1,category1,ip1,port1,com1) values (@machineNum11,@position11,@communicateMode11,@category11,@ip11,@port11,@com11)",g_con);                    OleDbParameter _machineNumParam= g_adapter.InsertCommand.Parameters.Add("@machineNum11",OleDbType.VarChar);                    _machineNumParam.SourceColumn = "machineNum1";                    _machineNumParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _positionParam= g_adapter.InsertCommand.Parameters.Add("@position11",OleDbType.VarChar);                    _positionParam.SourceColumn = "position1";                    _positionParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _communicateModeParam= g_adapter.InsertCommand.Parameters.Add("@communicateMode11",OleDbType.VarChar);                    _communicateModeParam.SourceColumn = "communicateMode1";                    _communicateModeParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _categoryParam= g_adapter.InsertCommand.Parameters.Add("@category11",OleDbType.VarChar);                    _categoryParam.SourceColumn = "category1";                    _categoryParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _ipParam= g_adapter.InsertCommand.Parameters.Add("@ip11",OleDbType.VarChar);                    _ipParam.SourceColumn = "ip1";                    _ipParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _portParam= g_adapter.InsertCommand.Parameters.Add("@port11",OleDbType.VarChar);                    _portParam.SourceColumn = "port1";                    _portParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _comParam= g_adapter.InsertCommand.Parameters.Add("@com11",OleDbType.VarChar);                    _comParam.SourceColumn = "com1";                    _comParam.SourceVersion = DataRowVersion.Current;                }                else if (item.RowState == DataRowState.Modified)                {                    g_adapter.UpdateCommand = new OleDbCommand("update machineSetTable set machineNum1=@machineNum,position1=@position,communicateMode1=@communicate,category1=@category,ip1=@ip,port1=@port,com1=@com where id=@id1", g_con);                    OleDbParameter _machineNumParam = g_adapter.UpdateCommand.Parameters.Add("@machineNum", OleDbType.VarChar);                    _machineNumParam.SourceColumn = "machineNum1";                    _machineNumParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _positionParam= g_adapter.UpdateCommand.Parameters.Add("@position",OleDbType.VarChar);                    _positionParam.SourceColumn = "position1";                    _positionParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _communicateParam= g_adapter.UpdateCommand.Parameters.Add("@communicate",OleDbType.VarChar);                    _communicateParam.SourceColumn = "communicateMode1";                    _communicateParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _categoryParam= g_adapter.UpdateCommand.Parameters.Add("@category",OleDbType.VarChar);                    _categoryParam.SourceColumn = "category1";                    _categoryParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _ipParam= g_adapter.UpdateCommand.Parameters.Add("@ip",OleDbType.VarChar);                    _ipParam.SourceColumn = "ip1";                    _ipParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _portParam= g_adapter.UpdateCommand.Parameters.Add("@port",OleDbType.VarChar);                    _portParam.SourceColumn = "port1";                    _portParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _comParam= g_adapter.UpdateCommand.Parameters.Add("@com",OleDbType.VarChar);                    _comParam.SourceColumn = "com1";                    _comParam.SourceVersion = DataRowVersion.Current;                    OleDbParameter _idParam = g_adapter.UpdateCommand.Parameters.Add("@id1", OleDbType.Integer);                    _idParam.SourceColumn = "id";                    _idParam.SourceVersion = DataRowVersion.Original;                }                else if (item.RowState == DataRowState.Deleted)                {                    g_adapter.DeleteCommand = new OleDbCommand("delete from machineSetTable where id=@id",g_con);                    OleDbParameter _idParam= g_adapter.DeleteCommand.Parameters.Add("@id",OleDbType.Integer);                    _idParam.SourceColumn = "id";                    _idParam.SourceVersion = DataRowVersion.Original;                }            }            g_adapter.Update(param_newTable);            param_newTable.AcceptChanges();        }    }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表