C#执行存储过程问题
我用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;
}
}
}