简体   繁体   中英

How to read the stored procedure query from Sql Server 2005 in C#?

I have a simple stored procedure

CREATE PROCEDURE [dbo].[simple]

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @sql NVARCHAR(MAX)

    SELECT @sql = '
            SELECT TOP(5) * FROM aTable'
        PRINT @sql

    EXEC sp_executesql 
        @sql
END

And now, in C#, I want to get, if is possible, the @sql value from stored procedure (after it was executed).

I use Sql Server 2005.

How to do that in C# ?

You will need to return it explicitly from your SPROC.

You have at least 3 2 options here

  1. An OUT parameter ( CREATE PROCEDURE [dbo].[simple] @sql NVARCHAR(MAX) OUT )
  2. a RETURN value ( RETURN @sql )
  3. Just SELECT @sql as the last line of your SPROC

You will need to then bind this in your appropriate technology in your C# code

EDIT Return won't work - Integer expressions only

Re : How do I do this in C#?

Assuming you are using ADO.NET SqlClient:

If you use OUTPUT

var myParam = new SqlParameter("@sql", SqlDbType.VarChar);
myParam.Direction = ParameterDirection.Output;
myCmd.Parameters.Add(myParam);

If you use SELECT it will come through as an additional result set to your SPROC. Since your proc already emits one result set (by 'sp_executesql @sql'), this will be the second.

From the comments, you want access to @sql from C# code after calling the method. So:

CREATE PROCEDURE [dbo].[simple]
    @sql nvarchar(4000) = null OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    SELECT @sql = N'SELECT TOP(5) * FROM aTable'

    EXEC sp_executesql @sql
END

then simply in your ADO.NET code:

using(var cmd = connection.CreateCommand()) {
    cmd.CommandText = "dbo.simple";
    cmd.CommandType = CommandType.StoredProcedure;
    var sqlParam = cmd.Parameters.Add("sql", SqlDbType.NVarChar, 4000);
    sqlParam.Direction = ParameterDirection.Output;
    // TODO here: ExecuteNonQuery, ExecuteReader, etc, i.e. your existing code
    string sql = (string)sqlParam.Value;
}

Why are you not using this, what is the need of @sql variable:

CREATE PROCEDURE [dbo].[simple]
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SELECT TOP(5) * FROM aTable
END

C# code:

    SqlConnection sqlConnection = new SqlConnection("ConnectionString"); 
    SqlCommand cmd = new SqlCommand("simple", sqlConnection);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adp.Fill(ds);

The SqlCommand class has the property CommandText which you can use to read the procedure, "Gets or sets the Transact-SQL statement, table name or stored procedure to execute at the data source." You can find some examples here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM