问题描述
我的代码如下:///<summary>///sql事务处理///</summary>///<paramname="sqlString">sql查询字符串数组A</param>///<paramname="sqlparam">对应A的相应的SqlParameter[]的ArrayList对象</param>///<returns>返回的字符串为该方法出现异常的描述,当它为空则表示执行成功</returns>publicstaticstringExecuteSqlTransaction(string[]sqlString,ArrayListsqlparam){using(SqlConnectionconnection=newSqlConnection(_sqlConStr)){stringexStr="";connection.Open();System.Data.SqlClient.SqlCommandcommand=connection.CreateCommand();System.Data.SqlClient.SqlTransactiontransaction;//Startalocaltransaction.transaction=connection.BeginTransaction("myTransaction");//Mustassignbothtransactionobjectandconnection//toCommandobjectforapendinglocaltransactioncommand.Connection=connection;command.Transaction=transaction;try{for(inti=0;i<sqlString.Length;i++){//得到sql的查询字符串command.CommandText=sqlString[i].ToString();//得到对应该查询字符串的SqlParameter[]SqlParameter[]_param=(SqlParameter[])sqlparam[i];for(intj=0;j<_param.Length;j++){command.Parameters.Add(_param[j]);}command.ExecuteNonQuery();}//Attempttocommitthetransaction.transaction.Commit();//Console.WriteLine("Bothrecordsarewrittentodatabase.");}catch(Exceptionex){//Console.WriteLine("CommitExceptionType:{0}",ex.GetType());//Console.WriteLine("Message:{0}",ex.Message);//Attempttorollbackthetransaction.exStr=ex.ToString();try{transaction.Rollback();}catch(Exceptionex2){exStr=ex2.ToString();//Thiscatchblockwillhandleanyerrorsthatmayhaveoccurred//ontheserverthatwouldcausetherollbacktofail,suchas//aclosedconnection.//Console.WriteLine("RollbackExceptionType:{0}",ex2.GetType());//Console.WriteLine("Message:{0}",ex2.Message);}}returnexStr;}//插入定单,并插入指定数量的货物(事物处理)publicstringINSERT_TransportOrdersAndGoods(TransportOrdersInfotOrdersInfo,ArrayListgoodsParam){intgoodsCounts=goodsParam.Count;//得到查询字符串的数组string[]_sqlQueryArry=newstring[goodsCounts+1];_sqlQueryArry[0]=SQL_INSERT_TransportOrders;for(inti=0;i<goodsCounts;i++)_sqlQueryArry[i+1]=SQL_INSERT_Goods;//得到查询字符串的数组对应的参数ArrayList_sqlparam=newArrayList();_sqlparam.Clear();_sqlparam.Add(GetINSERTParam(tOrdersInfo));GoodsInfo_myGoodsInfo;for(inti=0;i<goodsCounts;i++){_myGoodsInfo=(GoodsInfo)goodsParam[i];_sqlparam.Add(Get_GoodsExceptIDParam(_myGoodsInfo.GoodsName,_myGoodsInfo.IsPackete,_myGoodsInfo.Amount,_myGoodsInfo.Volume,_myGoodsInfo.Weight,_myGoodsInfo.Premium,_myGoodsInfo.TransportOrdersID));}//执行事务string_exceptionStr=Utility.ExecuteSqlTransaction(_sqlQueryArry,_sqlparam);return_exceptionStr;}这是参数定义:privateSqlParameter[]Get_GoodsExceptIDParam(stringgoodsName,boolisPackete,intamount,stringvolume,stringweight,decimalpremium,stringtransportOrdersID){SqlParameter[]_param=newSqlParameter[]{newSqlParameter(PARAM_GoodsName,SqlDbType.NVarChar),newSqlParameter(PARAM_IsPacket,SqlDbType.Bit),newSqlParameter(PARAM_Amount,SqlDbType.Int),newSqlParameter(PARAM_Volume,SqlDbType.NVarChar),newSqlParameter(PARAM_Weight,SqlDbType.NVarChar),newSqlParameter(PARAM_Premium,SqlDbType.Money),newSqlParameter(PARAM_TransportOrdersID,SqlDbType.NVarChar)};_param[0].Value=goodsName;_param[1].Value=isPackete;_param[2].Value=amount;_param[3].Value=volume;_param[4].Value=weight;_param[5].Value=premium;_param[6].Value=transportOrdersID;return_param;}//插入货物记录privateconststringSQL_INSERT_Goods="insertintogoodsInfo(goodsName,isPacket,amount,volume,weight,premium,transportOrdersID)values(@goodsName,@isPacket,@amount,@volume,@weight,@premium,@transportOrdersID)";现象:当只插入一个货物时,它正常工作,当插入2行时就报错:{"Thevariablename'@goodsName'hasalreadybeendeclared.Variablenamesmustbeuniquewithinaquerybatchorstoredprocedure."}如何来解决它啊!
解决方案
解决方案二:
ding~~~~~~~~~~~~
解决方案三:
高手帮忙啊!
解决方案四:
参数清空:cmd.Parameters.Clear()