我試圖用一個文本框的文本在mysql數據庫中選擇一行。 但是,當我使用下面的代碼時,我得到一個錯誤。C#的Mysql文本框文本選擇
MySqlCommand command = connection.CreateCommand(); //we create a command
command.CommandText = "SELECT * FROM info where id=" + textBox1.Text ; //in commandtext, we write the Query
MySqlDataReader reader = command.ExecuteReader(); //execute the SELECT command, which returns the data into the reader
while (reader.Read()) //while there is data to read
{
MessageBox.Show(reader["info"].ToString());
}
它工作正常,用字母,但是當我嘗試使用問號或類似的東西,我得到以下錯誤:‘?’
「參數必須定義。「在這種情況下
你可以張貼例外? –
MySql.Data.MySqlClient.MySqlException未處理 HResult = -2147467259 消息=參數'?'必須定義。 Source = MySql.Data ErrorCode = -2147467259 – user3540727
由於目前的情況,您的代碼很容易受到SQL注入攻擊。我強烈建議你考慮使用下面答案中給出的參數。 – mcy