CREATE PROC InsertCategory 
@CategoryName nchar(15)
AS
INSERT INTO Categories(CategoryName) Values(@CategoryName)
Return scope_identity()
go SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=;database=Northwind");
SqlCommand cmd = new SqlCommand("InsertCategory", cn);
cmd.CommandType = CommandType.StoredProcedure;
//存储过程返回值的参数名随便取,这里我取名为@ReturnValue
cmd.Parameters.Add("@ReturnValue", SqlDbType.Int);
//指明参数的方向是存储过程的返回值
cmd.Parameters["@ReturnValue"].Direction = ParameterDirection.ReturnValue;
cmd.Parameters.AddWithValue("@CategoryName", TextBox1.Text);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
//得到返回值
Label1.Text = cmd.Parameters["@ReturnValue"].Value.ToString();

解决方案 »

  1.   

    和执行SQL语句是一样的,只是要要声明SqlCommand sqlcom=new SqlCommand();
                              sqlcom.CommandType = CommandType.StoredProcedure ;
    其他没什么区别的。
    当然执行的SQL语句改成"exec 存储过程名称 '"+参数1+"','"+参数2+"'..."
      

  2.   

    我是在外面将 其组织好了的 
    string sql = "exec UpdateSingleProblem " + courseid + " ," + userid; int result = DBHelper.ExecuteCommand(sql); 
    然后调用 //执行不带参数的非查询语句 
    public static int ExecuteCommand(string sql) 

    SqlCommand scmd = new SqlCommand(sql, Connection); 
    ?= scmd.ExecuteNonQuery(); 
    return ?; 
    }
    zj2050      2008年11月02日 19点35分29秒 说:
    以下是存储过程 CREATE PROCEDURE [dbo].[UpdateSingleProblem] 
    @tmid int, 
    @compid int, 
    @courseid int 
    AS 
    DECLARE @retid int if exists (select 1 from TB_SingleProblem where ID=@tmid) 
    UPDATE [DBBaby].[dbo].[TB_SingleProblem] 
    SET [CompID] = @compid 
    ,[CourseID] = @courseid 
    ,[Title] = @title 
    WHERE (ID=@tmid) 
    else 
    INSERT INTO [DBBaby].[dbo].[TB_SingleProblem] 
    ([CompID] 
    ,[CourseID]) 
    VALUES 
    (@compid, 
    @courseid ) SET @retid = SCOPE_IDENTITY() 
    Return @retid
      

  3.   

    在BU层调用就可以了 跟写SQL语句一样的 
      

  4.   

    在BU层调用就可以了 跟写SQL语句一样的 
      

  5.   

    string sql = "exec UpdateSingleProblem " + courseid + " ," + userid; 
    ==
    这不是标准的调用存储过程的方式,请套用1楼的案例,将courseid和userid作为参数