17,740
社区成员
发帖
与我相关
我的任务
分享
static readonly SqlParameter[] sps;
static Brand()
{
sps = new SqlParameter[4];
sps[0] = new SqlParameter("@PKID", SqlDbType.TinyInt, 2);
sps[0].Direction = ParameterDirection.Output;
sps[1] = new SqlParameter("@Name", SqlDbType.VarChar, 50);
sps[2] = new SqlParameter("@Picture", SqlDbType.VarChar, 120);
sps[3] = new SqlParameter("@Url", SqlDbType.VarChar, 120);
}
private static SqlCommand SetCmd(string sProcedure,SqlParameter[]sps)
{
SqlCommand cmd = new SqlCommand(sProcedure,new SqlConnection(sConnectionString));
cmd.CommandType = CommandType.StoredProcedure;
if (sps != null)
{
cmd.Parameters.AddRange(sps);
}
return cmd;
}
public static void Save(Camera.OL.Brand brand)
{
sps[1].Value = brand.Name;
sps[2].Value = brand.Picture;
sps[3].Value = brand.Url;
object[]obj;
DBHelper.Execute("Camera_Brand_Save", sps, new string[] { "@PKID" }, out obj);//当第二遍执行的时候 System.ArgumentException: 另一个 SqlParameterCollection //中已包含 SqlParameter。
brand.PKID = Convert.ToInt32(obj[0]);
list.Add(brand);
}
public static void Execute(string sProcedure, SqlParameter[] sps, string[] sSource, out object[] obj)
{
try
{
SqlCommand cmd = SetCmd(sProcedure, sps);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
obj = new object[sSource.Length];
for (int i = 0; i < sSource.Length; i++)
{
obj[i] = cmd.Parameters[i].Value;
}
}
catch (Exception ex)
{
throw ex;
}
}
public static void Execute(string sProcedure, SqlParameter[] sps, string[] sSource, out object[] obj)
{
try
{
SqlCommand cmd = SetCmd(sProcedure, sps);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
obj = new object[sSource.Length];
for (int i = 0; i < sSource.Length; i++)
{
obj[i] = cmd.Parameters[i].Value;
}
cmd.Parameters.Clear();
cmd.Connection.Close();
}
catch (Exception ex)
{
throw ex;
}
}
public static void Execute(string sProcedure, SqlParameter[] sps, string[] sSource, out object[] obj)
{
try
{
SqlCommand cmd = SetCmd(sProcedure, sps);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.Connection.Close();
obj = new object[sSource.Length];
for (int i = 0; i < sSource.Length; i++)
{
obj[i] = cmd.Parameters[i].Value;
}
}
catch (Exception ex)
{
throw ex;
}
}