我用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=@usernameGO以下是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;
} }
}
//创建的存储过程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=@usernameGO以下是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;
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货