mysql - C#連接數(shù)據(jù)庫(kù)時(shí)一直這一句出問(wèn)題int i = cmd.ExecuteNonQuery();
問(wèn)題描述
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('請(qǐng)輸入用戶名和密碼進(jìn)行注冊(cè)');}else{ connection.Open();//連接到數(shù)據(jù)庫(kù) 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('用戶名重復(fù),請(qǐng)重新輸入');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('注冊(cè)成功'); textBox_username.Clear(); textBox_password.Clear();}else{ MessageBox.Show('注冊(cè)不成功'); textBox_username.Clear(); textBox_password.Clear();} } connection.Close();} }
問(wèn)題解答
回答1:你倒是說(shuō)一下具體出什么問(wèn)題啊,另外看起來(lái)sql1里面的單引號(hào)應(yīng)該改為雙引號(hào)才是你的本意,但改了后需加上必要的包裹單引號(hào)。
回答2:首先 請(qǐng)不要拼接sql,其次用sqlparameter
回答3:sql拼接錯(cuò)了
相關(guān)文章:
1. docker api 開發(fā)的端口怎么獲取?2. docker-compose 為何找不到配置文件?3. docker gitlab 如何git clone?4. MySQL數(shù)據(jù)庫(kù)中文亂碼的原因5. angular.js - 關(guān)于$apply()6. angular.js - angularjs的自定義過(guò)濾器如何給文字加顏色?7. mysql - 新浪微博中的關(guān)注功能是如何設(shè)計(jì)表結(jié)構(gòu)的?8. 在windows下安裝docker Toolbox 啟動(dòng)Docker Quickstart Terminal 失敗!9. dockerfile - 我用docker build的時(shí)候出現(xiàn)下邊問(wèn)題 麻煩幫我看一下10. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失敗???
