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