473,414 Members | 1,626 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,414 software developers and data experts.

How to shorten this code for SQL-query

I'm using the code below. I know for sure
that the data obtained will be a single
value. Hence, it feels somewhat like nuking
a fly to use the adapter etc. Is it
possible to shorten the code for this
specific, special case? How?

string commandString
= "select sth from smwhr where id = 5";
SqlConnection connection
= new SqlConnection(conStr);
SqlCommand command
= new SqlCommand(commandString, connection);
connection.Open();
command.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
connection.Close();
return ds.Tables[0].Rows[0][0].ToString();
--
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.
Aug 19 '08 #1
7 1685
K Viltersten wrote:
I'm using the code below. I know for sure
that the data obtained will be a single
value. Hence, it feels somewhat like nuking
a fly to use the adapter etc. Is it
possible to shorten the code for this
specific, special case? How?
SqlCommand.ExecuteScalar().

--
J.
Aug 19 '08 #2

"K Viltersten" <tm**@viltersten.comwrote in message
news:6h************@mid.individual.net...
I'm using the code below. I know for sure
that the data obtained will be a single
value. Hence, it feels somewhat like nuking
a fly to use the adapter etc. Is it
possible to shorten the code for this
specific, special case? How?

string commandString
= "select sth from smwhr where id = 5";
SqlConnection connection
= new SqlConnection(conStr);
SqlCommand command
= new SqlCommand(commandString, connection);
connection.Open();
command.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
connection.Close();
return ds.Tables[0].Rows[0][0].ToString();
If you are only getting a single value, use ExecuteScalar:

string commandString
= "select sth from smwhr where id = 5";
SqlConnection connection
= new SqlConnection(conStr);
SqlCommand command
= new SqlCommand(commandString, connection);
connection.Open();
object result = command.ExecuteScalar();
connection.Close();
return result.ToString();
Aug 19 '08 #3
K Viltersten wrote:
I'm using the code below. I know for sure
that the data obtained will be a single
value. Hence, it feels somewhat like nuking
a fly to use the adapter etc. Is it
possible to shorten the code for this
specific, special case? How?
Actually, you are nuking the fly twice...
string commandString
= "select sth from smwhr where id = 5";
SqlConnection connection
= new SqlConnection(conStr);
SqlCommand command
= new SqlCommand(commandString, connection);
connection.Open();
Here you are executing the query and throwing away the result:
command.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
Here you are executing the query again:
da.Fill(ds);
connection.Close();
return ds.Tables[0].Rows[0][0].ToString();
All you need is:

string commandString = "select sth from smwhr where id = 5";
string value;
using (SqlConnection connection = new SqlConnection(conStr)) {
using (SqlCommand command = new SqlCommand(commandString, connection)) {
value = (string)command.ExecuteScalar();
}
}
return value;

--
Göran Andersson
_____
http://www.guffa.com
Aug 19 '08 #4
>I'm using the code below. I know for sure
>that the data obtained will be a single
value. Hence, it feels somewhat like nuking
a fly to use the adapter etc. Is it
possible to shorten the code for this
specific, special case? How?

Actually, you are nuking the fly twice...
>string commandString
= "select sth from smwhr where id = 5";
SqlConnection connection
= new SqlConnection(conStr);
SqlCommand command
= new SqlCommand(commandString, connection);
connection.Open();

Here you are executing the query and throwing away the result:
>command.ExecuteNonQuery();
>SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();

Here you are executing the query again:
>da.Fill(ds);
>connection.Close();
return ds.Tables[0].Rows[0][0].ToString();

All you need is:

string commandString = "select sth from smwhr where id = 5";
string value;
using (SqlConnection connection = new SqlConnection(conStr)) {
using (SqlCommand command = new SqlCommand(commandString, connection))
{
value = (string)command.ExecuteScalar();
}
}
return value;
Where do you open/close the connection? Is it
performed automagically, perhaps? I'm affraid
that it might not work on my computer as i'm
still on v2 of C#. Does it matter in this case?

--
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.
Aug 19 '08 #5
"K Viltersten" <tm**@viltersten.comwrote in message
news:6h************@mid.individual.net...
[...]
Where do you open/close the connection? Is it
performed automagically, perhaps?
No, it's not automatic. He forgot to add connection.Open() inside the
"using" block.
>I'm affraid
that it might not work on my computer as i'm
still on v2 of C#. Does it matter in this case?
No problem. There's nothing in that code that requires a newer version.
In fact, if I recall correctly, it should even work in C# 1.0.

Aug 19 '08 #6
K Viltersten wrote:
>>I'm using the code below. I know for sure
that the data obtained will be a single
value. Hence, it feels somewhat like nuking
a fly to use the adapter etc. Is it
possible to shorten the code for this
specific, special case? How?
Actually, you are nuking the fly twice...
>>string commandString
= "select sth from smwhr where id = 5";
SqlConnection connection
= new SqlConnection(conStr);
SqlCommand command
= new SqlCommand(commandString, connection);
connection.Open();
Here you are executing the query and throwing away the result:
>>command.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
Here you are executing the query again:
>>da.Fill(ds);
connection.Close();
return ds.Tables[0].Rows[0][0].ToString();
All you need is:

string commandString = "select sth from smwhr where id = 5";
string value;
using (SqlConnection connection = new SqlConnection(conStr)) {
using (SqlCommand command = new SqlCommand(commandString, connection))
{
value = (string)command.ExecuteScalar();
}
}
return value;

Where do you open/close the connection? Is it
performed automagically, perhaps? I'm affraid
that it might not work on my computer as i'm
still on v2 of C#. Does it matter in this case?
As Alberto pointed out, I forgot to open the connection.

Closing the connection, however, is done automatically. When you exit
the using block, the Dispose method of the object is called, which
closes the database connection.

The using block also uses a try...finally, so the connection is always
closed, even if the code inside the block would cause an exception.

--
Göran Andersson
_____
http://www.guffa.com
Aug 19 '08 #7
<snip>
>Where do you open/close the connection? Is it
performed automagically, perhaps? I'm affraid
that it might not work on my computer as i'm
still on v2 of C#. Does it matter in this case?

As Alberto pointed out, I forgot to open the connection. Closing the
connection, however, is done automatically. When you exit the using block,
the Dispose method of the object is called, which closes the database
connection.
Thanks. Jättebra, if i may take a guess based on
your name and the contents of your webpage. :)

--
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.
Aug 19 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Francois | last post by:
Hi, This is probably a very easy question...but I just don't know the answer... I want to shorten this script: document.formulier.form_o_stoel1.checked = false;...
4
by: B.J. | last post by:
Hi, Is there any way how I can shorten following code ? try {...} catch(FormatException) {SAME CODE IN ALL CATCH BLOCKS} catch(OverflowException) {SAME CODE IN ALL CATCH BLOCKS} E.g. to...
15
by: Benjamin Rutt | last post by:
Are there any C tools that can find redundant #includes in a project, so as to shorten compile time? Of course, eliminating single #includes by hand and determining if the recompile fails is one...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
24
by: John Smith | last post by:
I want to shorten a string by replacing the last character by '\0'. The following code displays the string. It works fine. It's in a loop and different strings are displayed without problems....
1
by: Kenneth Keeley | last post by:
Hi, I am creating a news and events page for my web site and I have a data grid for the summary of news items that is using a TemplateColumn I can layout the data in the format I wish. My only...
1
by: Web Response Time | last post by:
I found a surprising problem. I sent a zero length's request in my asp.net program to another asp.net program on the local machine. until I received the zero length's response data, it only took...
3
by: neoswf | last post by:
hi. i want to shorten getElementById() string at my files. ive writen this function: function gebi(el){ document.getElementById(el) } and when i try to call an ID, i try to call him like this:...
3
by: UKuser | last post by:
Hi, Is there a way to shorten this line if ((ereg(".jpg",$filename))||(ereg(".eps",$filename))||(ereg(".tif", $filename))||(ereg(".tiff",$filename))||(ereg(".jpeg",$filename))||...
1
by: treeguy | last post by:
I am not a developer but my site has long URL strings that have problem getting crawled. I understand Google now will crawl past the + but does not like the long strings. I tried URL rewrite and it...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.