调用存储过程,返回参数
2005-11-04 14:39 Bluetooth 阅读(687) 评论(0) 编辑 收藏 举报
首先声明一个带有返回参数的存储过程,在Pubs里面
1.声明连接字符窜、连接命令
2.添加对象变量
3.打开连接
4.执行
5.关闭连接。
与执行SQL语句不同的是:存储过程必须先标明此SQL类型为存储过程 : comm.CommandType=System.Data.CommandType.StoredProcedure ;另外一点,comm.CommandText=str,本来我以为像写CommandText="select * from pubs.dbo.userm*"一样,使用在SQLServer编辑使用的CommandText="execute procedure para1,para2……",编辑一直出现错误。原来它需要使用的是CommandText="存储过程名",而不需后加参数,参数部分则由Parameters包含其中。
一下代码就OK了。呵呵
if exists (select name from sysobjects where(name='ma_CheckUser' and type='P'))
drop procedure ma_CheckUser
go
create procedure ma_CheckUser
@paraID varchar(10),@paraName varchar(10),@paraResult varchar(10)=null output,@paraLevel varchar(10)=null output
as --下面是SQL
if exists (select [id],[name] from usermanage where([id]=@paraID and [name]=@paraName))
begin
set @paraResult='YES'
select @paraLevel=(select level from usermanage where(id=@paraID and name=@paraName))
end
else
set @paraResult='NO'
go
这样,我们在控制台程序里面来调用这个存储过程。步骤不外乎如下:drop procedure ma_CheckUser
go
create procedure ma_CheckUser
@paraID varchar(10),@paraName varchar(10),@paraResult varchar(10)=null output,@paraLevel varchar(10)=null output
as --下面是SQL
if exists (select [id],[name] from usermanage where([id]=@paraID and [name]=@paraName))
begin
set @paraResult='YES'
select @paraLevel=(select level from usermanage where(id=@paraID and name=@paraName))
end
else
set @paraResult='NO'
go
1.声明连接字符窜、连接命令
2.添加对象变量
3.打开连接
4.执行
5.关闭连接。
与执行SQL语句不同的是:存储过程必须先标明此SQL类型为存储过程 : comm.CommandType=System.Data.CommandType.StoredProcedure ;另外一点,comm.CommandText=str,本来我以为像写CommandText="select * from pubs.dbo.userm*"一样,使用在SQLServer编辑使用的CommandText="execute procedure para1,para2……",编辑一直出现错误。原来它需要使用的是CommandText="存储过程名",而不需后加参数,参数部分则由Parameters包含其中。
一下代码就OK了。呵呵
System.Console.WriteLine("显示存储过程");
System.Data.SqlClient.SqlConnection con=new System.Data.SqlClient.SqlConnection("user id=sa;Initial Catalog=pubs;data source=HONOR10\\wincc");
System.Data.SqlClient.SqlCommand comm=new System.Data.SqlClient.SqlCommand();
comm.Connection=con;
comm.CommandType=System.Data.CommandType.StoredProcedure;
comm.Parameters.Add("@paraID",System.Data.SqlDbType.VarChar,10);
comm.Parameters.Add("@paraName",System.Data.SqlDbType.VarChar,10);
comm.Parameters.Add("@paraResult",System.Data.SqlDbType.VarChar,10);
comm.Parameters.Add("@paraLevel",System.Data.SqlDbType.VarChar,10);
comm.Parameters[0].Direction=System.Data.ParameterDirection.Input;
comm.Parameters[1].Direction=System.Data.ParameterDirection.Input;
comm.Parameters[2].Direction=System.Data.ParameterDirection.Output;
comm.Parameters[3].Direction=System.Data.ParameterDirection.Output;
comm.Parameters[0].Value="1";
comm.Parameters[1].Value="5";
comm.CommandText="[pubs].[dbo].[ma_CheckUser]";
comm.Connection.Open();
try
{
comm.ExecuteReader();
Console.WriteLine(comm.Parameters[2].Value.ToString()+"\n"+comm.Parameters[3].Value.ToString());
}
catch(System.Data.SqlClient.SqlException E)
{
Console.WriteLine(E.Message);
comm.Connection.Close();
}
comm.Connection.Close();
goto tt;
System.Data.SqlClient.SqlConnection con=new System.Data.SqlClient.SqlConnection("user id=sa;Initial Catalog=pubs;data source=HONOR10\\wincc");
System.Data.SqlClient.SqlCommand comm=new System.Data.SqlClient.SqlCommand();
comm.Connection=con;
comm.CommandType=System.Data.CommandType.StoredProcedure;
comm.Parameters.Add("@paraID",System.Data.SqlDbType.VarChar,10);
comm.Parameters.Add("@paraName",System.Data.SqlDbType.VarChar,10);
comm.Parameters.Add("@paraResult",System.Data.SqlDbType.VarChar,10);
comm.Parameters.Add("@paraLevel",System.Data.SqlDbType.VarChar,10);
comm.Parameters[0].Direction=System.Data.ParameterDirection.Input;
comm.Parameters[1].Direction=System.Data.ParameterDirection.Input;
comm.Parameters[2].Direction=System.Data.ParameterDirection.Output;
comm.Parameters[3].Direction=System.Data.ParameterDirection.Output;
comm.Parameters[0].Value="1";
comm.Parameters[1].Value="5";
comm.CommandText="[pubs].[dbo].[ma_CheckUser]";
comm.Connection.Open();
try
{
comm.ExecuteReader();
Console.WriteLine(comm.Parameters[2].Value.ToString()+"\n"+comm.Parameters[3].Value.ToString());
}
catch(System.Data.SqlClient.SqlException E)
{
Console.WriteLine(E.Message);
comm.Connection.Close();
}
comm.Connection.Close();
goto tt;