ASP.NET如何调用此存储过程

时间:2015-09-27 13:51:52

标签: c# asp.net stored-procedures

我提供了这个存储过程:

USE [DatabaseName]
GO

DECLARE@return_value int,
@L1 nchar(10),
@L2 nchar(10),
@L3 nchar(10),
@L4 nchar(10),
@LD1 nchar(15),
@LD2 nchar(15),
@LD3 nchar(15),
@LD4 nchar(15)

EXEC@return_value = [dbo].[uspCheckTagId]
            @TagId = '10001',
            @deviceId = 'testunitid',
            @L1 = @L1 OUTPUT,
            @L2 = @L2 OUTPUT,
            @L3 = @L3 OUTPUT,
            @L4 = @L4 OUTPUT,
            @LD1 = @LD1 OUTPUT,
            @LD2 = @LD2 OUTPUT,
            @LD3 = @LD3 OUTPUT,
            @LD4 = @LD4 OUTPUT

SELECT
    @L1 as N'@L1',
    @L2 as N'@L2',
    @L3 as N'@L3',
    @L4 as N'@L4',
    @LD1 as N'@LD1',
    @LD2 as N'@LD2',
    @LD3 as N'@LD3',
    @LD4 as N'@LD4'

SELECT 'Return Value' = @return_value
GO

我的问题是如何从ASP.NET调用此存储过程?

我完全从头开始,所以我过去尝试过的所有代码现在都消失了:(

请帮助。

2 个答案:

答案 0 :(得分:1)

假设这是针对 SQL Server (您应该真的为此提供正确的标记!),您应该使用类似这样的代码 - 将SqlConnectionSqlCommand等一次性内容放入正确的using(..) { ... }块中,为您的程序使用参数等。

procedure void CallStoredProcedure(int tagId)
{
    // get connection string, typically from config
    string connectionString = ConfigurationManager.ConnectionStrings["...."].ConnectionString;

    // Define stored procedure name
    string storedProcedureName = "dbo.uspCheckTagId";

    // create connection and command
    using (SqlConnection conn = new SqlConnection(connectionString))
    using (SqlCommand cmdExecProc = new SqlCommand(storedProcedureName, conn))
    {
        cmdExecProc.CommandType = CommandType.StoredProcedure;

        // build up parameters
        cmdExecProc.Parameters.Add("@TagId", SqlDbType.Int).Value = tagId;

        cmdExecProc.Parameters.Add("@L1", SqlDbType.NChar, 10).ParameterDirection = ParameterDirection.Output;
        cmdExecProc.Parameters.Add("@L2", SqlDbType.NChar, 10).ParameterDirection = ParameterDirection.Output;
        cmdExecProc.Parameters.Add("@L3", SqlDbType.NChar, 10).ParameterDirection = ParameterDirection.Output;
        cmdExecProc.Parameters.Add("@L4", SqlDbType.NChar, 10).ParameterDirection = ParameterDirection.Output;
        cmdExecProc.Parameters.Add("@LD1", SqlDbType.NChar, 15).ParameterDirection = ParameterDirection.Output;
        cmdExecProc.Parameters.Add("@LD2", SqlDbType.NChar, 15).ParameterDirection = ParameterDirection.Output;
        cmdExecProc.Parameters.Add("@LD3", SqlDbType.NChar, 15).ParameterDirection = ParameterDirection.Output;
        cmdExecProc.Parameters.Add("@LD4", SqlDbType.NChar, 15).ParameterDirection = ParameterDirection.Output;

        // open connection, execute proc, close connection
        conn.Open();
        cmdExecProc.ExecuteNonQuery();

        // read out the output parameters here, and do with them whatever you need ......
        var l1Value = cmdExecProc.Parameters("@L1").Value;        
        ....

        conn.Close();
    }
}    

答案 1 :(得分:0)

首先,我将使给定的过程成为如下存储过程:

CREATE PROC MyStoredProcedure
AS
<Code from your snipped without USE statement>
GO

然后我会从ASP.NET代码隐藏文件

中调用它
SqlConnection insSqlConnection = new SqlConnection("YOUR CONNECTION STRING HERE");
SqlCommand insSqlCommand = new SqlCommand("MyStoredProcedure", insSqlConnection);
insSqlCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter insSqlDataAdapter = new SqlDataAdapter(insSqlCommand);
DataTable insDt = new DataTable();
insSqlDataAdapter.Fill(insDt);
insSqlConnection.Close();

如果您可以为场景提供更多详细信息,则可以采用更好的执行方式。