C#执行存储过程问题

xiaodezhuo 2010-10-07 10:59:05
我用C#写了一个类如下代码和一个存储过程,请问如何用以下代码中的函数执行我的这个存储过程(请详细一点):
//创建的存储过程update_user
create procedure update_user
(@username varchar(30),@usernichen varchar(30),@xingming varchar(30),
@sex varchar (2)
AS
update userinfo set usernichen=@usernichen,xingming=@xingming,
sex=@sex
where username=@username

GO

以下是C#写的类CpublicDB:
class CDBstoreProc
{
public string strCon = File.ReadAllText(Application.StartupPath + @"\DB.txt");
public SqlConnection con;

public void OpenCon()
{
con = new SqlConnection(strCon);
con.Open();
}

public void CloseCon()
{
if (con != null)
{
con.Close();
con.Dispose();
}
}

private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
if (parameter != null)
{
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
command.Parameters.Add(parameter);
}
}
return command;
}

public string RunProc(string storedProcName, IDataParameter[] parameters)
{
string StrValue;
OpenCon();
SqlCommand cmd;
cmd = BuildQueryCommand(con, storedProcName, parameters);
StrValue = cmd.ExecuteScalar().ToString();
CloseCon();
return StrValue;
}

}
}

...全文
38 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2010-10-09
  • 打赏
  • 举报
回复
C#问题请自己转贴子到C#版,这样提问会比较有效率。
「已注销」 2010-10-09
  • 打赏
  • 举报
回复
public string RunProc(string storedProcName, IDataParameter[] parameters)
{
string StrValue;
OpenCon();
SqlCommand cmd;
cmd = BuildQueryCommand(con, storedProcName, parameters);
StrValue = cmd.ExecuteScalar().ToString();
CloseCon();
return StrValue;
}
这个函数的参数名字不是表达得很清楚吗。。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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