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,
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();

commented: works +0

Thx, works perfect!

it's working fine for string what about date??

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.