A C# SQL question
-
Hi, When I want to specify an SQL query I am using the below code: mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = 'Brown'"; But how can I do this if I don't want to write down the surname(use a variable instead of that)? I used below code but couldn't get a result: string buffer_surname = textBox1.Text; mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = '@buffer_surname'"; ??? Thank you, Cem Louis
-
Hi, When I want to specify an SQL query I am using the below code: mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = 'Brown'"; But how can I do this if I don't want to write down the surname(use a variable instead of that)? I used below code but couldn't get a result: string buffer_surname = textBox1.Text; mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = '@buffer_surname'"; ??? Thank you, Cem Louis
string buffer_surname = textBox1.Text; mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = '" + buffer_surname.ToString() + "'"; You also should probably do a replace and replace any apostrophes with double apostrophes in buffer_surname, just in case. Hope this helps.
-
string buffer_surname = textBox1.Text; mySqlCommand.CommandText = "SELECT name, surname FROM people WHERE name = 'James' AND surname = '" + buffer_surname.ToString() + "'"; You also should probably do a replace and replace any apostrophes with double apostrophes in buffer_surname, just in case. Hope this helps.
-
Look a few posts down at Heath's response about an SQL statement. While it is very humorous, he is also VERY correct. You have the same issue.
Hi, I used the below code to EMPTY a table which is named people but it is not working???:
SqlConnection mySqlConnection4 = new SqlConnection("Initial Catalog=mydb;Data Source=" + comboBox1.SelectedItem.ToString() + ";Integrated Security=SSPI;"); mySqlConnection4.Open(); SqlCommand mySqlCommand4 = mySqlConnection4.CreateCommand(); SqlTransaction myTrans; myTrans = mySqlConnection4.BeginTransaction(IsolationLevel.ReadCommitted,"SampleTransaction"); mySqlCommand4.Connection = mySqlConnection4; mySqlCommand4.Transaction = myTrans; mySqlCommand4.CommandText = "EMPTY people" ; //mySqlCommand4.ExecuteNonQuery(); myTrans.Commit(); mySqlConnection4.Close();
Anyway What is the problem??? Thank you by the way... Cem Louis