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容器呢SSH為什么連不通呢?2. docker start -a dockername 老是卡住,什么情況?3. docker - 各位電腦上有多少個容器啊?容器一多,自己都搞混了,咋辦呢?4. Android TextView 或 ListView 加載過渡效果5. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””6. service mysql.server start 啟動失敗7. python - thrift 返回 TSocket read 0 bytes 求助!!!!8. 【python小白】 問關于之初始化一次對象的問題9. python - 為什么我直接用requests爬網頁可以,但用scrapy不行?10. python3.x - python中import theano出錯
