C#执行存储过程问题

xiaodezhuo xiaodezhuo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我用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;
}

}
}

...全文
评论 2
收藏 已收藏
分享
返回顶部
xiaodezhuo
C语言 70,039 成员 243,237 内容