mysql - C# always has this problem when connecting to the database int i = cmd.ExecuteNonQuery();
高洛峰
高洛峰 2017-05-18 10:46:30
0
3
682

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();
        }
    }
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
黄舟

Could you please tell me what the specific problem is? In addition, it seems that the single quotes in sql1 should be changed to double quotes, which is your original intention, but after changing it, you need to add the necessary wrapping single quotes.

左手右手慢动作

First of all, please do not splice sql, and secondly, use sqlparameter

習慣沉默

SQL splicing is wrong

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template