473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Recommended way to execute a query and store in a DataSet

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
4 1643
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tony Lee | last post by:
Some time a ago, on this newsgroup the following comments were made in recommending good references for Access (2003) >I used to recommend Dr. Rick Dobson's, "Programming Access <version>" for...
1
by: rawCoder | last post by:
Hi All Is there a way to Execute a query (possibly including two tables and joins) on a totally disconnected dataset. By 'Totally Disconnted' , I mean there is no DB and there is no DBConnection...
2
by: Lacka | last post by:
Hi, If I have this sequence: Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(ConfigurationSettings.AppSettings("DB")) Dim dbCommand As...
5
by: moondaddy | last post by:
I'm caching a dataset in an asp.net session variable to hold a user's data. one data item I need to store is an image the user uploaded. My problem is that I don't know how to get the image into...
1
by: Simon1234 | last post by:
Below you will see my code to read data from a database and store it into a dataset. The first, commented code uses an SQLDataReader and the second, uncommented code uses the Fill method of the...
2
by: Wizard | last post by:
Hi, I have a database with controls on a form: Student >> Class >> Date >> Grade at Present >>> Predicted >>> DOB The Grade and the Predict often change, how do i get it so the I could add a...
2
by: Rudy | last post by:
Hello all! I have a store proceudre that when it runs, it turns a 0 into a 1. Just one row, one value. I'm kinda of using it a check point. So the procedure returns no value at all. I want to...
4
by: Dikkuuhh | last post by:
Hi, i have a function that can execute stored procedures, but i don`t know how i can give it back to the other classes. I did it in an ArrayList now, but how can i do it better, i can`t make it...
9
by: sohan | last post by:
Hi, I want to know how to connect and execute a db2 query from inside a UNIX shell script. Details: We have a unix shell script. We need to execute multiple db2 sql queries from this shell...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.