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();
}
}
你倒是说一下具体出什么问题啊,另外看起来
sql1
里面的单引号应该改为双引号才是你的本意,但改了后需加上必要的包裹单引号。首先 请不要拼接sql,其次用sqlparameter
sql拼接错了