DbCommand Extensions
public
static
DbCommand AddInParameter(
this
DbCommand cmd,
string
parameterName,
object
value)
{
DbParameter para = cmd.CreateParameter();
para.ParameterName = parameterName;
para.Value = value ?? DBNull.Value;
cmd.Parameters.Add(para);
return cmd;
}
public static DbCommand AddInParameter( this DbCommand cmd, string parameterName, DbType type, int size, object value)
{
DbParameter para = cmd.CreateParameter();
para.ParameterName = parameterName;
para.DbType = type;
para.Size = size;
para.Value = value ?? DBNull.Value;
cmd.Parameters.Add(para);
return cmd;
}
public static DbCommand AddOutParameter( this DbCommand cmd, string parameterName)
{
DbParameter para = cmd.CreateParameter();
para.ParameterName = parameterName;
para.Direction = ParameterDirection.Output;
cmd.Parameters.Add(para);
return cmd;
}
public static DbCommand SetReturnParameter( this DbCommand cmd)
{
return cmd.SetReturnParameter(DbType.Int32);
}
public static DbCommand SetReturnParameter( this DbCommand cmd, DbType type)
{
if ( ! cmd.Parameters.Contains(DbBase.ReturnParameterName))
{
DbParameter para = cmd.CreateParameter();
para.ParameterName = DbBase.ReturnParameterName;
para.DbType = type;
para.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(para);
}
return cmd;
}
public static DbParameter GetReturnParameter( this DbCommand cmd)
{
int index = cmd.Parameters.IndexOf(DbBase.ReturnParameterName);
if (index != - 1 )
{
DbParameter returnParameter = cmd.Parameters[index];
if (returnParameter.Direction == ParameterDirection.ReturnValue)
{
return returnParameter;
}
}
return null ;
}
{
DbParameter para = cmd.CreateParameter();
para.ParameterName = parameterName;
para.Value = value ?? DBNull.Value;
cmd.Parameters.Add(para);
return cmd;
}
public static DbCommand AddInParameter( this DbCommand cmd, string parameterName, DbType type, int size, object value)
{
DbParameter para = cmd.CreateParameter();
para.ParameterName = parameterName;
para.DbType = type;
para.Size = size;
para.Value = value ?? DBNull.Value;
cmd.Parameters.Add(para);
return cmd;
}
public static DbCommand AddOutParameter( this DbCommand cmd, string parameterName)
{
DbParameter para = cmd.CreateParameter();
para.ParameterName = parameterName;
para.Direction = ParameterDirection.Output;
cmd.Parameters.Add(para);
return cmd;
}
public static DbCommand SetReturnParameter( this DbCommand cmd)
{
return cmd.SetReturnParameter(DbType.Int32);
}
public static DbCommand SetReturnParameter( this DbCommand cmd, DbType type)
{
if ( ! cmd.Parameters.Contains(DbBase.ReturnParameterName))
{
DbParameter para = cmd.CreateParameter();
para.ParameterName = DbBase.ReturnParameterName;
para.DbType = type;
para.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(para);
}
return cmd;
}
public static DbParameter GetReturnParameter( this DbCommand cmd)
{
int index = cmd.Parameters.IndexOf(DbBase.ReturnParameterName);
if (index != - 1 )
{
DbParameter returnParameter = cmd.Parameters[index];
if (returnParameter.Direction == ParameterDirection.ReturnValue)
{
return returnParameter;
}
}
return null ;
}