Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Retrieve value from table

Author  Topic 

Apples
Posting Yak Master

146 Posts

Posted - 2008-01-17 : 16:04:07
Here's my table:

tblOrders
----------------
OrderID | Total
----------------

I'd like to get the Total value out of the table if I'm given the OrderID, and store it in a variable. I'm using C# and ASP.NET 2.0.

It's probably really simple, and I think I need to use a stored procedure and ExecuteScalar(), but I'm not sure how to do it.

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-01-17 : 16:15:56
[code]SqlCommand sCMD = new SqlCommand("SELECT [Total] FROM tblOrders where OrderID = 1234", mConnection);

Int32 Total = (Int32)sCMD.ExecuteScalar();[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Apples
Posting Yak Master

146 Posts

Posted - 2008-01-17 : 16:55:19
OK, now I've got another problem. Say I have a bunch of different Totals for one OrderID, like this:

OrderID | Total
---------------
3 | 8.50
3 | 9.00
3 | 9.50

How can I store all the Total values in a single string, delimited by a newline?
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-01-17 : 17:00:04
call SqlCommand.ExecuteReader() from your code and loop over the results. Don't try to format the string in SQL Server.


elsasoft.org
Go to Top of Page

Apples
Posting Yak Master

146 Posts

Posted - 2008-01-17 : 17:02:31
quote:
Originally posted by jezemine

call SqlCommand.ExecuteReader() from your code and loop over the results. Don't try to format the string in SQL Server.


elsasoft.org



Ya, I'm doing it in C#. How can I loop through the results?
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-01-17 : 17:16:46
[code]
using (SqlDataReader r = myCommand.ExecuteReader())
{
while (r.Read())
{
// do stuff
}
}
[/code]


elsasoft.org
Go to Top of Page
   

- Advertisement -