mysql - C#連接數據庫時一直這一句出問題int i = cmd.ExecuteNonQuery();
問題描述
private void button1_Click(object sender, EventArgs e)
{string MyConnectionString = 'server=localhost;user=root;database=yangbo;port=3306;password=yangbo6510;';MySqlConnection connection = new MySqlConnection(MyConnectionString);if (textBox_username.Text.Trim() == '' && textBox_password.Text.Trim() == ''){ MessageBox.Show('請輸入用戶名和密碼進行注冊');}else{ connection.Open();//連接到數據庫 string sql = 'select * from usernp where username=’' + textBox_username.Text.Trim() + '’ ;'; MySqlCommand cmd = new MySqlCommand(sql, connection); cmd.CommandType = CommandType.Text; MySqlDataReader sdr; sdr = cmd.ExecuteReader(); if (sdr.Read()) {MessageBox.Show('用戶名重復,請重新輸入');textBox_username.Clear();textBox_password.Clear(); } else {string sql1 = 'insert into usernp (username,userpassword) values(’ + textBox_username.Text.Trim() + ’,’ + textBox_password.Text.Trim() +’)';cmd = new MySqlCommand(sql1, connection);int i = cmd.ExecuteNonQuery();if (i> 0){ MessageBox.Show('注冊成功'); textBox_username.Clear(); textBox_password.Clear();}else{ MessageBox.Show('注冊不成功'); textBox_username.Clear(); textBox_password.Clear();} } connection.Close();} }
問題解答
回答1:你倒是說一下具體出什么問題啊,另外看起來sql1里面的單引號應該改為雙引號才是你的本意,但改了后需加上必要的包裹單引號。
回答2:首先 請不要拼接sql,其次用sqlparameter
回答3:sql拼接錯了
相關文章:
1. docker 下面創建的IMAGE 他們的 ID 一樣?這個是怎么回事????2. 在應用配置文件 app.php 中找不到’route_check_cache’配置項3. html按鍵開關如何提交我想需要的值到數據庫4. css - width設置為100%之后列表無法居中5. ios - vue-cli開發項目webstrom會在stylus樣式報錯,飆紅,請大神幫忙6. css3 - 怎么感覺用 rem 開發的不多啊7. python - 在pyqt中做微信的機器人,要在表格中顯示微信好友的名字,卻顯示不出來,怎么解決?8. html5 - 用Egret寫的小游戲,怎么分享到微信呢?9. javascript - 一個頁面有四個圖片,翻頁的時候想固定住某個圖片然后翻頁,如何實現呢?10. objective-c - 自定義導航條為類似美團的搜索欄樣式
