SqlCommand 关于Parameters的问题

hackztx 2009-05-26 08:09:47
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;
}
}
...全文
179 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangwenfengksyc 2009-05-31
  • 打赏
  • 举报
回复
学习
阿非 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Sandy945 的回复:]
C# code
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;
}
}

[/Quote]

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;
}
}

阿非 2009-05-26
  • 打赏
  • 举报
回复

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;
}
}

cmd.Parameters.Clear();
yanfei_519 2009-05-26
  • 打赏
  • 举报
回复
路过,学习。。
薪水 2009-05-26
  • 打赏
  • 举报
回复
up

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧