By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

Recommended way to execute a query and store in a DataSet

P: n/a
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.
Oct 8 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Oct 8, 10:21*am, "K Viltersten" <t...@viltersten.comwrote:
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/libr...xw(VS.71).aspx
-Cnu
Oct 8 '08 #2

P: n/a
On Oct 8, 10:21*am, "K Viltersten" <t...@viltersten.comwrote:
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
Oct 8 '08 #3

P: n/a
On Oct 8, 1:21*pm, "K Viltersten" <t...@viltersten.comwrote:
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
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.
Oct 8 '08 #4

P: n/a
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...

--
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.


Oct 8 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.