PDA

Click to See Complete Forum and Search --> : sp_executeSQL and dataadapter


Techno
Dec 13th, 2004, 11:48 AM
hi there

I was wondering, in C#, is there a way to obtain a datatable or dataset of results when using sp_executeSQL?

I can use normal SQL queries fine, but when using the stored proc sp_executeSQL, i do not know how to obtain the entire collection of results into a datatable or dataset.

any ideas?

pvb
Dec 15th, 2004, 11:26 PM
using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

public class MyClass
{
public static void Main()
{
DataTable authors = new DataTable();
string connString="user id=userid;password=password;server=(local);database=pubs;";
using( SqlConnection cn = new SqlConnection( connString ) )
using( SqlCommand cmd = new SqlCommand( "sp_executesql", cn ) )
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add( "@stmt", "select * from authors" );
using( SqlDataAdapter da = new SqlDataAdapter( cmd ) )
{
da.Fill( authors );
}
}
Debug.Assert( authors.Rows.Count > 0, "authors datatable contains rows" );
}
}

eh eh look familiar? (http://www.panteravb.com/photos/showphoto.aspx?photoid=360)(the place,not me) I'm a BIG fan of the Royal Mile (http://www.panteravb.com/photos/showphoto.aspx?photoid=412).

HOASTONGE
Feb 12th, 2012, 02:54 PM
I had the same situations
And in my store proc. I had a PRINT command just before sp_executesql

PRINT @sql

exec sp_executesql @sql

And I just remove my PRINT command in my store procedure
and it's working fine

Weird ! but it's work for me

Techno
Feb 12th, 2012, 05:28 PM
this is a thread dated 2004 :)

techgnome
Feb 12th, 2012, 08:33 PM
I had the same situations
And in my store proc. I had a PRINT command just before sp_executesql

PRINT @sql

exec sp_executesql @sql

And I just remove my PRINT command in my store procedure
and it's working fine

Weird ! but it's work for me
Not all that weird actually... ANYthing returned back by SQL Server is considered return output. So the PRINT becomes the first resultset, you have to move to the next resultset in order to get past it.

-tg