PetShop4.0 源代码分析 C# 克隆 ICloneable使用
[align=left]
//Create a hashtable for the parameter cached private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
public static OracleParameter[] GetCachedParameters(string cacheKey) { OracleParameter[] cachedParms = (OracleParameter[])parmCache[cacheKey]; if (cachedParms == null) return null; // If the parameters are in the cache OracleParameter[] clonedParms = new OracleParameter[cachedParms.Length]; // return a copy of the parameters for (int i = 0, j = cachedParms.Length; i < j; i++) clonedParms[i] = (OracleParameter)((ICloneable)cachedParms[i]).Clone(); return clonedParms; }
private static OracleParameter[] GetOrderParameters() { OracleParameter[] parms = OracleHelper.GetCachedParameters(SQL_INSERT_ORDER); if (parms == null) { parms = new OracleParameter[] { new OracleParameter(PARM_ORDER_ID, OracleType.Number, 10), new OracleParameter(PARM_USER_ID, OracleType.VarChar, 80), new OracleParameter(PARM_DATE, OracleType.DateTime), new OracleParameter(PARM_SHIP_ADDRESS1, OracleType.VarChar, 80), new OracleParameter(PARM_SHIP_ADDRESS2, OracleType.VarChar, 80), new OracleParameter(PARM_SHIP_CITY, OracleType.VarChar, 80), new OracleParameter(PARM_SHIP_STATE, OracleType.VarChar, 80), new OracleParameter(PARM_SHIP_ZIP, OracleType.VarChar, 50), new OracleParameter(PARM_SHIP_COUNTRY, OracleType.VarChar, 50), new OracleParameter(PARM_BILL_ADDRESS1, OracleType.VarChar, 80), new OracleParameter(PARM_BILL_ADDRESS2, OracleType.VarChar, 80), new OracleParameter(PARM_BILL_CITY, OracleType.VarChar, 80), new OracleParameter(PARM_BILL_STATE, OracleType.VarChar, 80), new OracleParameter(PARM_BILL_ZIP, OracleType.VarChar, 50), new OracleParameter(PARM_BILL_COUNTRY, OracleType.VarChar, 50), new OracleParameter(PARM_TOTAL, OracleType.Number), new OracleParameter(PARM_BILL_FIRST_NAME, OracleType.VarChar, 80), new OracleParameter(PARM_BILL_LAST_NAME, OracleType.VarChar, 80), new OracleParameter(PARM_SHIP_FIRST_NAME, OracleType.VarChar, 80), new OracleParameter(PARM_SHIP_LAST_NAME, OracleType.VarChar, 80), new OracleParameter(PARM_AUTHORIZATION_NUMBER, OracleType.Int32)}; OracleHelper.CacheParameters(SQL_INSERT_ORDER, parms); } return parms; } }
[解决办法]
OracleParameter[] Pars=new .....
如果执行:
A.EX(Sql1,Pars)
A.EX(Sql2,Pars)
就会报错(第二句):Pars参数已属于其他对象(如同 二楼所说)
但Clone()后就不会发生这种情况.
在实际中这种情况非常多,因为相同的参数名以及参数值,可能被多个语句引用.
否则你得这样(就算是一样的参数名与参数值):
OracleParameter[] Pars=new .....
A.EX(Sql1,Pars)
Pars=new .....
A.EX(Sql2,Pars)