日期:2014-05-18 浏览次数:20978 次
SqlConnection conn = new SqlConnection(connString); cmd = conn.CreateCommand(); cmd.CommandText = "select * from Test_Table"; SqlDataAdapter da = new SqlDataAdapter(cmd); SqlCommandBuilder cb = new SqlCommandBuilder(da); da.Update(ds);
------解决方案--------------------
给你俩方法吧,看不懂就真没办法了!
public void OpenSqlConnection() { filePath = "Data Source=172.24.20.5;Persist Security Info=True;User ID=sa;Password=fc.erp;Initial Catalog=GDIS"; constr = filePath; if (sqlConn.State != ConnectionState.Open) { sqlConn.Close(); sqlConn.ConnectionString = constr; myCmd.Connection = sqlConn; try { sqlConn.Open(); } catch (Exception e) { throw new Exception(e.Message); } } } public int UpData(DataSet MyDs) { int iReturn = 0; try { OpenSqlConnection(); for (int i = 0; i < MyDs.Tables.Count; i++) { DataTable dt = MyDs.Tables[i]; if (dt != null) { SqlDataAdapter da = GetDataAdapter("SELECT * FROM " + dt.TableName); iReturn = da.Update(dt); } else throw new Exception(); } } catch (Exception ex) { try { CloseSqlConnection(); } catch (Exception err) { throw new Exception(err.Message); } throw ex; } finally { CloseSqlConnection(); } return iReturn; }
------解决方案--------------------
public SqlDataAdapter GetDataAdapter(string strSQL) { SqlDataAdapter da = null; try { da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(strSQL, sqlConn); SqlCommandBuilder custCB = new SqlCommandBuilder(da); if (sqlConn != null) { if (da.SelectCommand != null) da.SelectCommand.Transaction = trans; if (da.DeleteCommand != null) da.DeleteCommand.Transaction = trans; if (da.InsertCommand != null) da.InsertCommand.Transaction = trans; if (da.UpdateCommand != null) da.UpdateCommand.Transaction = trans; } } catch (Exception ex) { throw ex; } return da; }