一、用vs2013创建一个窗体
工具箱在:视图---工具箱
从工具箱中找出 2个文本标签Lable,2个文本框textbox,2个单击按钮button,下面以label做示例,其他控件一样。(属性对话框在哪里找:视图------属性窗口)
二、在sqlserver中创建一个表(用来保存登录注册的数据)
表已创建完毕,编写代码
三、单击单选按钮"登录",在button1_Click下写入
if (textBox1.Text == "" || textBox2.Text == "")
MessageBox.Show("请输入用户名和密码!", "警告");
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=xj;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from denglu where name='" + textBox1.Text.Trim() + "' and password='" + textBox2.Text.Trim() + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (!sdr.HasRows)
MessageBox.Show("未注册,请注册!", "提示");
else
{
if (sdr.HasRows)
MessageBox.Show("登录成功!", "提示");
else
MessageBox.Show("提示,用户名或密码错误!", "警告");
conn.Close();
}
单击单选按钮"注册",在button2_Click下写入
if (textBox1.Text == "" || textBox2.Text == "")
MessageBox.Show("请输入用户名和密码!", "警告");
else
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=xj;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from denglu where name='" + textBox1.Text.Trim() + "' and password='" + textBox2.Text.Trim() + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
MessageBox.Show("该用户已注册,请使用其他用户名", "提示");
else
{
sdr.Close();
string myinsert = "insert into denglu(name,password) values('" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand mycom = new SqlCommand(myinsert, conn);
mycom.ExecuteNonQuery();
conn.Close();
conn.Dispose();
MessageBox.Show("您已注册成功!");
}
}