问题描述
publicstaticOleDbTypeget_SqlDbType(stringColumnType){OleDbTyperGet=OleDbType.Integer;switch(ColumnType){case"int":rGet=OleDbType.Integer;break;case"intidentity":rGet=OleDbType.Integer;break;case"decimal":rGet=OleDbType.Decimal;break;case"char":rGet=OleDbType.VarChar;break;case"nchar":rGet=OleDbType.VarChar;break;case"bit":rGet=OleDbType.Integer;break;case"float":rGet=OleDbType.VarNumeric;break;case"money":rGet=OleDbType.VarNumeric;break;case"datetime":rGet=OleDbType.DBDate;break;case"image"://rGet=OleDbType.break;case"text":rGet=OleDbType.VarChar;break;case"ntext":rGet=OleDbType.VarChar;break;case"numeric":rGet=OleDbType.Decimal;break;case"varchar":rGet=OleDbType.VarChar;break;case"nvarchar":rGet=OleDbType.VarChar;break;}returnrGet;}///多表用适配器更新,使用事务成功则提交,失败则回滚。///在调用该方法外面申明事务、连接,再循环调用此方法更新///</summary>///<paramname="trans">事务</param>///<paramname="Connection">数据库连接</param>///<paramname="da">适配器,多表中必须在调用方法前申明</param>///<paramname="dataSet">修改的数据集</param>///<paramname="tableName">更新的表名</param>///<paramname="ID_Name">唯一关键字</param>///<paramname="constring">连接字符串</param>publicstaticvoidUpdateDataset(OleDbConnectionConnection,DataSetdataSet,stringtableName,stringID_Name){stringsValue="";stringsColName="";stringsUpdate="";intcolWidth=10;OleDbTypecolType;colType=OleDbType.VarChar;DataSetds=newDataSet();ds=rtnds("execsp_columns'"+tableName+"'",Connection);//SqlHelper.FillDataset(constring,CommandType.Text,"execsp_columns'"+tableName+"'",ds,newstring[]{"ArrayCol"});foreach(DataRowsRowinds.Tables[0].Rows){if(sRow["type_name"].ToString()!="intidentity"){if(sRow["column_def"].ToString().Trim()!="(getdate())"){colType=get_SqlDbType(sRow["type_name"].ToString());stringColumn_name=sRow["Column_Name"].ToString();sValue+=(sValue==""?"":",")+"@"+Column_name;sColName+=(sColName==""?"":",")+Column_name;sUpdate+=(sUpdate==""?"":",")+Column_name+"=@"+Column_name;}}}stringsInsert="INSERTinto"+tableName+"("+sColName+")values("+sValue+")";stringsaUpdate="update"+tableName+"set"+sUpdate+"where"+ID_Name+"=@id_name";stringsDelete="Delete"+tableName+"where"+ID_Name+"=@id_name";OleDbCommandinsertCommand=newOleDbCommand(sInsert,Connection);OleDbCommandupdateCommand=newOleDbCommand(saUpdate,Connection);OleDbCommanddeleteCommand=newOleDbCommand(sDelete,Connection);//OleDbTransactiontrans=Connection.BeginTransaction();//if(trans!=null)//{//if(trans.Connection==null)thrownewArgumentException("Thetransactionwasrollbackedorcommited,pleaseprovideanopentransaction.","transaction");//insertCommand.Transaction=trans;//updateCommand.Transaction=trans;//deleteCommand.Transaction=trans;//}insertCommand.CommandType=CommandType.Text;updateCommand.CommandType=CommandType.Text;deleteCommand.CommandType=CommandType.Text;foreach(DataRowsRowinds.Tables[0].Rows){if(sRow["type_name"].ToString()!="intidentity"){if(sRow["column_def"].ToString().Trim()!="(getdate())"){colType=get_SqlDbType(sRow["type_name"].ToString());colWidth=(int)sRow["length"];stringColumn_name=sRow["Column_Name"].ToString();updateCommand.Parameters.Add("@"+Column_name,colType,colWidth,Column_name);if(Column_name==ID_Name){OleDbParameterupdateparm=updateCommand.Parameters.Add("@id_name",colType,colWidth,ID_Name);updateparm.SourceVersion=DataRowVersion.Original;OleDbParameterdeleteparm=deleteCommand.Parameters.Add("@id_name",colType,colWidth,ID_Name);deleteparm.SourceVersion=DataRowVersion.Original;}insertCommand.Parameters.Add("@"+Column_name,colType,colWidth,Column_name);}}}OleDbDataAdapterdataAdapter=newOleDbDataAdapter();dataAdapter.InsertCommand=insertCommand;dataAdapter.UpdateCommand=updateCommand;dataAdapter.DeleteCommand=deleteCommand;try{dataAdapter.Update(dataSet);}catch(System.ArgumentNullExceptione1){thrownewArgumentNullException(e1.Message);}catch(System.Data.SqlClient.SqlExceptione2){thrownewException(e2.Message);}}调用方法按按钮事件ds对应的数据集为 stringls_sql="selectep01NEW.E_NO,ep01NEW.e_name,EP70.E70_CLASS_NAMEfromep01NEW,EP70";ls_sql+="whereep01NEW.E_NO='ay0005'ANDEP70.E70_KIND='A'ANDep01NEW.E_DEPT=EP70.E70_KIND_NAME";oleconn=ConnData.Conntodatabase();ConnData.UpdateDataset(oleconn,ds,"ep01NEW","e_no");一上午头都晕了都不知道
解决方案
解决方案二:
try{dataAdapter.Update(dataSet);}这里出错,提示必须宣告变数'