C#存储过程Output返回参数 方法调用类
2009-04-30 09:00
603 查看
存储过程:
create proc proc_GetUserID
@UserName nvarchar(50),@ID int output
as
begin
set @ID = (select ID from UserAccount where UserName = @UserName)
end
C#代码:
private void GetUserID(string userName)
{
SqlParameter[] paras = new SqlParameter[2];
paras[0] = new SqlParameter("@UserName", userName);
paras[1] = new SqlParameter("@ID",SqlDbType.Int);
paras[1].Direction = ParameterDirection.Output;
object o = DataAccess.ExcuteNonQuery_Proc_Output("proc_GetUserID", paras, "@ID");
if (o == null || o.ToString() == "")
{
this.Label1.Text = "没有这个用户名";
}
else
{
this.Label1.Text = o.ToString();
}
}
public class DataAccess
{
public static object ExcuteNonQuery_Proc_Output(string procName, SqlParameter[] parameters,string outName)
{
SqlConnection conn = GetConnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procName;
for (int i = 0; i < parameters.Length; i++)
{
cmd.Parameters.Add(parameters[i]);
}
conn.Open();
int n = cmd.ExecuteNonQuery();
object o = cmd.Parameters[outName].Value;
conn.Close();
return o;
}
}
create proc proc_GetUserID
@UserName nvarchar(50),@ID int output
as
begin
set @ID = (select ID from UserAccount where UserName = @UserName)
end
C#代码:
private void GetUserID(string userName)
{
SqlParameter[] paras = new SqlParameter[2];
paras[0] = new SqlParameter("@UserName", userName);
paras[1] = new SqlParameter("@ID",SqlDbType.Int);
paras[1].Direction = ParameterDirection.Output;
object o = DataAccess.ExcuteNonQuery_Proc_Output("proc_GetUserID", paras, "@ID");
if (o == null || o.ToString() == "")
{
this.Label1.Text = "没有这个用户名";
}
else
{
this.Label1.Text = o.ToString();
}
}
public class DataAccess
{
public static object ExcuteNonQuery_Proc_Output(string procName, SqlParameter[] parameters,string outName)
{
SqlConnection conn = GetConnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procName;
for (int i = 0; i < parameters.Length; i++)
{
cmd.Parameters.Add(parameters[i]);
}
conn.Open();
int n = cmd.ExecuteNonQuery();
object o = cmd.Parameters[outName].Value;
conn.Close();
return o;
}
}
相关文章推荐
- c#调用数据库中的带返回(output参数形式)存储过程的demo
- C#调用SQL中的存储过程中有output参数,存储过程执行过程中返回信息
- 基于C#中的类SqlCommand对象调用SQLServer存储过程时,存储过程执行成功,但是对象方法ExecuteNonQuery()返回-1
- c# 调用带有参数的存储过程方法--oracle篇
- 【Mybatis】MyBatis调用带有返回结果、output参数的存储过程上与ibatis的区别
- 存储过程输出参数、返回值、返回表及C#调用
- 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值
- c# 调用带有参数的存储过程方法--oracle【转载】
- 在存储过程中取得刚刚插入的记录的某个字段,将其输出,由C#接受OUTPUT返回参数
- C#中的类SqlCommand对象使用方法ExecuteNonQuery()调用SQLServer存储过程时,存储过程执行成功,数据发生改变,但是返回-1
- C#调用存储过程带输出参数或返回值
- 存储过程output参数_数据库调用_C#_NET调用简单CODE
- C#调用SQL存储过程以及返回值和参数的获取
- 在存储过程中取得刚刚插入的记录的某个字段,将其输出,由C#接受OUTPUT返回参数
- MyBatis调用带有返回结果、output参数的存储过程上与ibatis的区别
- C#中利用存储过程(含有参数)返回dataset
- HOW TO:使用 ADO.NET 和 Visual C# .NET 调用带参数的存储过程
- C#中调用MySQL存储过程的方法
- c# 调用存储过程方法
- MyCat1.6带返回参数存储过程调用示例