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;
}
}
}
//创建的存储过程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;
}
}
}
...全文
相关推荐
C#存储过程的细致使用+接收sql的返回值
一套学员量话管理 内涵数据库存储过程的详细使用 和返回值的接受 存储过程的的细致使用 给你打开思路 对每个对象有细致的划分
C#中调用存储过程的通用类
C#中调用存储过程的通用类, 共享出来来,自己的一点代码
C#存储过程简单应用
主要针对初学者,这是我初学的时候做的。希望对初学的朋友有所帮助
C#调用Oracle存储过程的方法
主要介绍了C#调用Oracle存储过程的方法,包含数据库及C#对应的调用代码,具有一定参考借鉴价值,需要的朋友可以参考下
连接SQLserver 生成访问存储过程的C#代码 - 2
连接SQLserver 生成访问存储过程的C#代码 ,第二种方式


