c# 调用 存储过程

来源:互联网 发布:网络诈骗的七大特点 编辑:程序博客网 时间:2024/05/22 14:41

SQL

-------------------------------------------------------------

alter proc fact

 @a int,
 @b int output
as
declare @tem nvarchar(10)
begin
if exists(select empName from employee where empName='郭金亮' )
begin
  insert into employee values('1234567891','有了',1,'32020002')
  set @tem='有了'
end
else
begin
 insert into employee values('9876543211','郭金亮',0,'32020002')
 set @tem='郭金亮'
end
 select @b=ID from employee where empName=@tem

end


------------------

C#

------------------------------------------------------

            SqlConnection con = new SqlConnection("server=.;database=hospital;uid=sa;pwd=123;");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            int a=0;
            cmd.CommandText = "fact";
            cmd.Parameters.Add("@a", SqlDbType.Int);
            cmd.Parameters["@a"].Value = 10;
            cmd.Parameters.Add("@b", SqlDbType.Int);
            cmd.Parameters["@b"].Direction = ParameterDirection.Output;
            con.Open();
            cmd.ExecuteNonQuery();
            Console.WriteLine(cmd.Parameters["@b"].Value.ToString());
            con.Close();
            Console.ReadLine();