/*****************************
*
*Done By dayfrom klete ;
*
********************************/
Sql语句 :
UPDATE tbl_Products SET pName = [@pName], pIntro = [@pIntro], pMarketPrice = [@pMarketPrice], pPreferentialPrice = [@pPreferentialPrice]
WHERE pId=[@pId];
产品更新函数
public bool ProductUpdate(dProduct one)
{
OleDbCommand sqlCmd = new OleDbCommand();
AddParamToSQLCmd(sqlCmd, "@pName", OleDbType.VarChar, 50, ParameterDirection.Input, one.PName);
AddParamToSQLCmd(sqlCmd, "@pIntro", OleDbType.LongVarWChar,one.PIntroduction.Length , ParameterDirection.Input, one.PIntroduction);
AddParamToSQLCmd(sqlCmd, "@pMarketPrice", OleDbType.Single, 8, ParameterDirection.Input,one.PMarketPrice);
AddParamToSQLCmd(sqlCmd, "@pPreferentialPrice", OleDbType.Single, 8, ParameterDirection.Input, one.PPreferentialPrice);
AddParamToSQLCmd(sqlCmd, "@pId", OleDbType.Integer, 0, ParameterDirection.Input, one.PId);
this.SetCommandType(sqlCmd,CommandType.StoredProcedure,SP_PRODUCTS_PRODUCTS_UPDATE);
/*
AddParamToSQLCmd(sqlCmd, "@pId", OleDbType.Integer, 0, ParameterDirection.Input, one.PId);
AddParamToSQLCmd(sqlCmd, "@pName", OleDbType.VarChar, 50, ParameterDirection.Input, one.PName);
AddParamToSQLCmd(sqlCmd, "@pIntro", OleDbType.LongVarWChar,one.PIntroduction.Length , ParameterDirection.Input, one.PIntroduction);
AddParamToSQLCmd(sqlCmd, "@pMarketPrice", OleDbType.Single, 8, ParameterDirection.Input,one.PMarketPrice);
AddParamToSQLCmd(sqlCmd, "@pPreferentialPrice", OleDbType.Single, 8, ParameterDirection.Input, one.PPreferentialPrice);
this.SetCommandType(sqlCmd,CommandType.StoredProcedure,SP_PRODUCTS_PRODUCTS_UPDATE);
*/
return (this.ExecuteNonQuery(sqlCmd) == 1 );
}
测试结果
如果更新函数是绿色部分,将无法对产品进行更新,主要原因是
初步认为OleDbParameter[]的参数使用顺序与sql语句里的参数使用顺序必须是对应的,在access里面。。
但如果是sql2000里使用(SqlParameter[]),参数顺序则不会有影响。。
附上:AddParamToSQLCmd方法,不知是不是这个方法有问题
private void AddParamToSQLCmd(OleDbCommand sqlCmd, string paramId, OleDbType sqlType, int paramSize, ParameterDirection paramDirection, object paramvalue)
{
// Validate Parameter Properties
if (sqlCmd == null)
throw (new ArgumentNullException("sqlCmd"));
if (paramId == string.Empty)
throw (new ArgumentOutOfRangeException("paramId"));
// Add Parameter
OleDbParameter newSqlParam = new OleDbParameter();
newSqlParam.ParameterName = paramId;
newSqlParam.OleDbType = sqlType;
newSqlParam.Direction = paramDirection;
if (paramSize > 0)
newSqlParam.Size = paramSize;
if (paramvalue != null)
newSqlParam.Value = paramvalue;
sqlCmd.Parameters.Add(newSqlParam);
}