How to pass stored procedure as string to another method?

A

Assimalyst

Hi,

I have two methods. In one i would like to create a string from a
stored procedure to pass to the second which populates a datagrid.

private void method()
{
string sqlString = "spDataForDatagrid"; // the stored procedure
BindDataGrid(sqlString);
}

private void BindDataGrid(string sqlString)
{
// Bind data to DataGrid
}

This obviously passes the string in it's literal "spDataForDatagrid"
form. How do I reference the the actual stored procedure so the string
passed is "SELECT ..... FROM ...... etc"?

Many thanks.
 
M

Marc Gravell

Well, you can't (robustly); the SP contents belong to the SQL-server, not
clients like C# (via ADO.Net); callers should just know it's interface
(name, params, etc); you /might/ be able to issue an sp_helptext (or similar
for other vendors), but this isn't guaranteed to return the text (encrypted,
etc).

Are you really talking about an SP? Or just a named entry e.g. in a resource
file? If you mean an SP, the normal usage is just to create a command with
the command-text of the sp-name, and command-type of sp.

Marc
 
M

Marc Gravell

...to the SQL-server...

Sorry, I should have said "to the database"... principle applies to all
backends.

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Not sure what you want. At somepoint you will have to instantiate a
SqlCommand, where you do this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top