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. 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.
"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();
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
>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.
"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.
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
<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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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:...
|
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))||...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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: 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,...
| |