PDA

View Full Version : Extract variables from a database


andreasberglind
08-28-03, 06:48 AM
I have a MS SQL database with som tables in it. what Iīd like to do is get an ID value from one of the tables to another table. I guess I should be able to do this with ASP.Net(C#), but I canīt get it there.

This is the SQL stmt:
select ActorID from Actor where ActorName = 'Name'

then I want the value of this, which is an int, into an int variable, such as:
int actor_ID;

how do i get that?

Shane
08-28-03, 10:17 AM
SqlConnection conn = new SqlConnection(CONNECTION_STRING);

string strSQL = "select ActorID from Actor where ActorName = 'Name'";

SqlCommand cmd = new SqlCommand(strSQL,conn);

int intNumber;

conn.Open();

intNumber = (int) cmd.ExecuteScalar();

conn.Close();



That should be it. Remember to import your System.Data.SqlClient namespace. :)