0

Hi,
I'm looking for a way to get a single data value from a table by an SQL statement and store it in a variable (i'm using C#).

example:

table: customers(customer_ID, name, age)

string variable = SELECT name FROM customers where customer_ID = 25

It's probably not that hard, but I can't find the right code.

4
Contributors
4
Replies
11
Views
7 Years
Discussion Span
Last Post by arunkumars
1

Hi,
I'm looking for a way to get a single data value from a table by an SQL statement and store it in a variable (i'm using C#).

example:

table: customers(customer_ID, name, age)

string variable = SELECT name FROM customers where customer_ID = 25

It's probably not that hard, but I can't find the right code.

//

Hi try this one

SqlCommand scmd = new SqlCommand(" SELECT name FROM customers where customer_ID = 25", Conn);
Conn.Open();
string str = scmd.ExecuteScalar().ToString();
Conn.Close();

Votes + Comments
works
0

SqlCommand scmd = new SqlCommand(" SELECT DOB FROM customers where customer_ID = 25", Conn);
Conn.Open();
DateTime dt = Convert.ToDateTime(scmd.ExecuteScalar().ToString());
Conn.Close();

This should work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.