private void button1_Click(object sender, EventArgs e) { string username = textBox1.Text.Trim(); string constr = "Data Source=STUDENT-PC\\SQLEXPRESS;Initial Catalog=RemoteUser;Persist Security Info=True;User ID=sa;Password=123456;"; SqlConnection mycon = new SqlConnection(constr); mycon.Open(); SqlCommand checkCmd = mycon.CreateCommand(); string s = "select username from users where username='******'"; checkCmd.CommandText = s; SqlDataAdapter check = new SqlDataAdapter(); check.SelectCommand = checkCmd; DataSet checkData = new DataSet(); int n = check.Fill(checkData, "users"); if (n != 0) { MessageBox.Show("用户名存在"); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } else if (textBox2.Text != textBox3.Text) { MessageBox.Show("两次密码不一致"); textBox2.Text = ""; textBox3.Text = ""; } else { string s1 = "insert into users(username,password) values ('" + textBox1.Text + "','" + textBox2.Text + "')"; SqlCommand mycom = new SqlCommand(s1, mycon); mycom.ExecuteNonQuery(); mycon.Close(); mycom = null; mycon.Dispose(); MessageBox.Show("注册成功"); 主界面 form = new 主界面(); form.ShowDialog(); } }
void start() { 主界面 form = new 主界面(); form.ShowDialog(); }