Recommended way to execute a query and store in a DataSet

K

K Viltersten

Today, i run the code below and while it
works, i can't stop wondering if it can
be performed in a better way. Especially,
i'd like to know if the declaration of
the adapter is neccessary.

SqlCommand command =
new SqlCommand("MyStorProc", Connection);
command.CommandType = CommandType.StoredProcedure;
foreach (String key in form.Keys)
command.Parameters.AddWithValue("@" + key, form[key]);
Connection.Open();
command.ExecuteNonQuery();
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet data = new DataSet();
adapter.Fill(data);
Connection.Close();

Can i pull the data directly from command
and if so, how?
 
D

Duggi

Today, i run the code below and while it
works, i can't stop wondering if it can
be performed in a better way. Especially,
i'd like to know if the declaration of
the adapter is neccessary.

SqlCommand command =
  new SqlCommand("MyStorProc", Connection);
command.CommandType = CommandType.StoredProcedure;
foreach (String key in form.Keys)
  command.Parameters.AddWithValue("@" + key, form[key]);
Connection.Open();
command.ExecuteNonQuery();
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet data = new DataSet();
adapter.Fill(data);
Connection.Close();

Can i pull the data directly from command
and if so, how?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.

There are two mode of connecting to a database in ADO.Net

I think connected model does not required a dataAdapter, however a
disconnected model requires one..

ADO.Net architecture is best explained at

http://msdn.microsoft.com/en-us/library/27y4ybxw(VS.71).aspx


-Cnu
 
D

Duggi

Today, i run the code below and while it
works, i can't stop wondering if it can
be performed in a better way. Especially,
i'd like to know if the declaration of
the adapter is neccessary.

SqlCommand command =
  new SqlCommand("MyStorProc", Connection);
command.CommandType = CommandType.StoredProcedure;
foreach (String key in form.Keys)
  command.Parameters.AddWithValue("@" + key, form[key]);
Connection.Open();
command.ExecuteNonQuery();
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet data = new DataSet();
adapter.Fill(data);
Connection.Close();

Can i pull the data directly from command
and if so, how?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.

perticularly when you do a ExecuteNonQuery(), it is connected model as
it retrives only a scalar value from the database.

-Cnu
 
R

Ralph

Today, i run the code below and while it
works, i can't stop wondering if it can
be performed in a better way. Especially,
i'd like to know if the declaration of
the adapter is neccessary.

SqlCommand command =
  new SqlCommand("MyStorProc", Connection);
command.CommandType = CommandType.StoredProcedure;
foreach (String key in form.Keys)
  command.Parameters.AddWithValue("@" + key, form[key]);
Connection.Open();
command.ExecuteNonQuery();
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet data = new DataSet();
adapter.Fill(data);
Connection.Close();

Can i pull the data directly from command
and if so, how?

You need some way to retrieve the data so either the Sqldataadapter,
or a Sqldatareader.
Maybe from the command object you can to execute reader
Unless you are returning all your data through out parameters.
I don't think you need command.ExecuteNonQuery(); either for this.
 
K

K Viltersten

You need some way to retrieve the data so
either the Sqldataadapter, or a Sqldatareader.
Maybe from the command object you can to
execute reader Unless you are returning all
your data through out parameters.I don't think
you need command.ExecuteNonQuery(); either for
this.

Right. And i noticed also that i didn't need
to open or close the connetion either. That
was a surprise, though...
 

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