mysql - C#連接數(shù)據(jù)庫時(shí)一直這一句出問題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('請輸入用戶名和密碼進(jìn)行注冊');}else{ connection.Open();//連接到數(shù)據(jù)庫 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ù),請重新輸入');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里面的單引號應(yīng)該改為雙引號才是你的本意,但改了后需加上必要的包裹單引號。
回答2:首先 請不要拼接sql,其次用sqlparameter
回答3:sql拼接錯(cuò)了
相關(guān)文章:
1. mysql 查詢身份證號字段值有效的數(shù)據(jù)2. python bottle跑起來以后,定時(shí)執(zhí)行的任務(wù)為什么每次都重復(fù)(多)執(zhí)行一次?3. 視頻文件不能播放,怎么辦?4. html5 - HTML代碼中的文字亂碼是怎么回事?5. python - 爬蟲模擬登錄后,爬取csdn后臺文章列表遇到的問題6. visual-studio - Python OpenCV: 奇怪的自動(dòng)補(bǔ)全問題7. mysql - 分庫分表、分區(qū)、讀寫分離 這些都是用在什么場景下 ,會(huì)帶來哪些效率或者其他方面的好處8. javascript - 彈出一個(gè)子窗口,操作之后關(guān)閉,主窗口會(huì)得到相應(yīng)的響應(yīng),例如網(wǎng)站的某些登錄界面,django后臺的管理等,這是怎么實(shí)現(xiàn)的呢?9. javascript - ios返回不執(zhí)行js怎么解決?10. android - 分享到微信,如何快速轉(zhuǎn)換成字節(jié)數(shù)組
