string strcon = "server=.;database=Myschool;uid=sa;pwd=123456"; SqlConnection sqlconn = new SqlConnection(strcon); SqlCommand sqlcomm = new SqlCommand("GetUser"); sqlcomm.Connection = sqlconn; sqlcomm.CommandType = CommandType.StoredProcedure; SqlParameter[] sqlps = new SqlParameter[2]; sqlps[0] = new SqlParameter("@name", "1"); sqlps[1] = new SqlParameter("@age", SqlDbType.Int,4); sqlps[1].Direction = ParameterDirection.Output; sqlcomm.Parameters.AddRange ( sqlps); sqlcomm.Parameters["@age"].Direction = ParameterDirection.Output; sqlcomm.Parameters.Add(new SqlParameter("@return",SqlDbType.Int)); sqlcomm.Parameters["@return"].Direction = ParameterDirection.ReturnValue; sqlconn.Open(); SqlDataAdapter sda = new SqlDataAdapter(sqlcomm); DataSet ds = new DataSet(); // sqlcomm.ExecuteNonQuery(); sda.Fill(ds); string str = sqlcomm.Parameters["@age"].Value.ToString(); string returns = sqlcomm.Parameters["@return"].Value.ToString();
create Proc GetUser @name varchar(50), @age int output as if(@name='1') begin select @age =COUNT(1) from student; set @age=2; select * from student; return 999; end else begin set @age=10 return 888; end
declare @id int exec GetUser '1',@id out print @id