如何从存储过程调用选定的值

时间:2017-02-13 04:47:16

标签: c# sql-server select stored-procedures

如果我有以下存储过程,如何在代码中调用所选值,即CourseId,以便将其设置为变量?

CREATE PROCEDURE [dbo].GetCourseIDFromCourseNumber(
 @courseNo int,
 @termId int,
 @courseId int OUTPUT
)
AS
BEGIN

SELECT @courseId = CourseId
FROM Course
Where (CourseNumber = @courseNo AND TermId = @termId)

END

C#代码

   objCommand.CommandType = CommandType.StoredProcedure;
            objCommand.CommandText = "GetCourseIDFromCourseNumber";

            objCommand.Parameters.AddWithValue("@courseNo", courseNumber);
            objCommand.Parameters.AddWithValue("@termId", term);
            objCommand.Parameters.AddWithValue("@section", section);

            //Create an Output parameter to store value from stored procedure
            SqlParameter courseIdParam = new SqlParameter("@courseId", SqlDbType.Int);
            courseIdParam.Direction = ParameterDirection.Output;
            objCommand.Parameters.Add(courseIdParam);

            //execute stored procedure
            DBConnect objDB = new DBConnect();
            SqlConnection connect = objDB.GetConnection();
            connect.Open();                                                   
            objCommand.ExecuteNonQuery();
            connect.Close();

            //read parameter value
            if (courseIdParam.Value != null)
            {
                var courseId = (int)courseIdParam.Value;
                Session["Course"] = courseId;
            } 

GetDataSet方法

public DataSet GetDataSetUsingCmdObj(SqlCommand theCommand)
{
        // Used for stored procedures (Select only) with parameters
        try
        {
            theCommand.Connection = myConnectionSql;
            SqlDataAdapter myDataAdapter = new SqlDataAdapter(theCommand);

            DataSet myDataSet = new DataSet();
            myDataAdapter.Fill(myDataSet);

            ds = myDataSet;
        }
        catch (Exception e)
        {
        }
        finally
        {
            myConnectionSql.Close();
        }

        return ds;
    }

3 个答案:

答案 0 :(得分:1)

创建FUNCTION而不是存储过程:

CREATE FUNCTION [dbo].GetCourseIDFromCourseNumber(
@courseNo int,
@termId int
)
RETURNS TABLE
AS RETURN
(
    SELECT CourseId
    FROM Course
    Where (CourseNumber = @courseNo AND TermId = @termId)
)

您现在可以在任何查询中使用输出,例如:

SELECT CourseId
FROM dbo.GetCourseIDFromCourseNumber(123,1);

SELECT t.*
FROM myTable t
CROSS APPLY dbo.GetCourseIDFromCourseNumber(123,1) c
WHERE t.CourseId = c.CourseId

答案 1 :(得分:0)

我个人喜欢将存储过程中返回的值存储到DataTable而不是DataSet中;然而,在这种情况下,这完全是个人偏好。然后,您需要遍历DataTable行中的数据。

请参阅以下代码:

string courseId;

DataTable myDataTable = new DataTable();
myDataAdapter.Fill(myDataTable);

foreach (DataRow dr in dt.Rows)
{
     courseId = dr["CourseId"].ToString();
}

答案 2 :(得分:0)

由于海报添加了相关的C#代码,因此很明显存储过程可能是合适的。

如果存储过程只返回一个值(而不是数据集),那么最好将该值作为OUTPUT参数返回:

CREATE PROCEDURE [dbo].GetCourseIDFromCourseNumber(
@courseNo int,
@termId int,
@courseId int OUTPUT
)
AS
BEGIN

    SELECT @courseId = CourseId
    FROM Course
    Where (CourseNumber = @courseNo AND TermId = @termId)

END

现在在C#中:

SqlCommand objCommand = new SqlCommand();
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.CommandText = "GetCourseIDFromCourseNumber";

objCommand.Parameters.AddWithValue("@courseNo", courseNumber);
objCommand.Parameters.AddWithValue("@termId", term);

// Create an OUTPUT parameter to store value from stored procedure
SqlParameter courseIdParam = new SqlParameter("@courseId", SqlDbType.Int);
courseIdParam.Direction = ParameterDirection.Output;
objCommand.Parameters.Add(courseIdParam);

// Execute stored procedure
objCommand.ExecuteNonQuery();

// Read parameter value
if(courseIdParam.Value != null)
{
    var courseId = (int)courseIdParam.Value;
}