MYSQL C#.net
hi,
In sql,
insert into TABLENAME (COLUMNS..) values(1,2...);select scope_identity.
above query gives primary key of inseted row.
\how this in MYsql query.?
help me pls
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SreekanthPosted Nov 15, 2009, 10:55 PM
niki i fnd sum easy method for this.
insert into TABLE () values(); select last_inset_id() ;
execute with executescalar , i gt d inserted primary key..
Cmd commnd text;
int pkey= Cmd.ExecuteScalar();
tanks 4 ur ,replay
Nilanka DharmadasaPosted Nov 10, 2009, 1:22 AM
Using the following C# code you will be able to connect to MYSQL DB and execute your queries.
Please make sure to set the connection string and queries properly.
If my answer helps you please do not forget to accept my answer.
string connString = "SERVER=" + server + ";" +
"DATABASE=mydatabase;" +
"UID=testuser;" +
"PASSWORD=testpassword;";
//create your mySQL connection
MySqlConnection cnMySQL = new MySqlConnection(connString);
//create your mySql command object
MySqlCommand cmdMySQL = cnMySQL.CreateCommand();
//set the command text (query) of the
//mySQL command object
cmdMySQL.CommandText = "insert into
//open the mySQL connection
cnMySQL.Open();
//execute the insert query
cmdMySQL.ExecuteNonQuery();
//create the command for the second query
cmdMySQL = cnMySQL.CreateCommand();
//set the select query
cmdMySQL.CommandText = "select
//create your mySQL reeader object
MySqlDataReader reader;
//execute the reader, thus retrieving the data
reader = cmdMySQL.ExecuteReader();
//while theres data keep reading
string columnvalue = "";
while (reader.Read())
{
//The value in the column is assigned to the variable.
columnvalue = reader.GetValue(0).ToString();
}
cnMySQL.Close();