Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem in Filling DataSets with SQL Procedures

Status
Not open for further replies.

TechnoSpike

Programmer
Joined
Mar 23, 2005
Messages
3
Location
PT
Hi! I'm having trouble trying to fill a DataSet with the results of a procedure I wrote. The procedure has 1 parameters and is composed of a select query (select id,text from Users where id=id_passed_by_param). My problem regards using this procedure to fill a DataSet. I was trying to do this:

try{
// create and open a connection object
conn = new SqlConnection(string_conn);
conn.Open();
SqlCommand cmd = new SqlCommand
("Proc", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@id", 1));
SqlDataAdapter da = new SqlDataAdapter(cmd);
// I'm stuck here...what can I do to fill the Dataset?
// normally I would do this:
// DataSet ds = new DataSet();
// da.fill(ds,"TableName");
// but in this case, what can I do?
}
finally
{
if (conn != null){
conn.Close();
}
}

Also I'm not exactly sure how I can execute the procedure...I don't want a DataReader (so I can't use the ExecuteReader()), unless I can somehow convert the data received by the procedure to the DataSet....Any help would be great!
 
Here's a little example, hope it helps:
Code:
public DataSet MyDataSet(int MyParameter)
{

    string MyConnectionString = "Whatever the connectionstring is"
    SqlDataAdapter myDataAdapter;

    DataSet ds = new DataSet();
    myDataAdapter = new SqlDataAdapter("up_MyProcedure", MyConnectionString);
    myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
    myDataAdapter.SelectCommand.CommandTimeout = 60;
    myDataAdapter.SelectCommand.Parameters.Add("@MyParameter", MyParameter);

    myDataAdapter.Fill(ds);
    return ds;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top