**话不多说 直接上代码**
1:窗口1的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 登录窗口
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
MessageBox.Show("账号不能为空");
return;
}
if (string.IsNullOrWhiteSpace(textBox2.Text))
{
MessageBox.Show("密码不能为空");
return;
}
string str =string.Format("select * from logon where username='{0}' and password='{1}'",textBox1.Text,textBox2.Text) ;
MessageBox.Show(str);
SqlCommand sqlcom = new SqlCommand(str,sql.sqlcon);
SqlDataReader dr = sqlcom.ExecuteReader();
if (!dr.HasRows)
{
MessageBox.Show("账号不存在,请先注册");
textBox1.Clear();
textBox1.Focus();
textBox2.Clear();
dr.Close();
return;
}
else
{
MessageBox.Show("登录成功");
dr.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
sql.sqlcon.Open();
textBox3.AppendText("数据库已经打开");
}
private void button3_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.Show();
}
}
}
2:窗口2的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;
namespace 登录窗口
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox1.Text.Trim()))
{
MessageBox.Show("账号不能为空");
return;
}
if (string.IsNullOrWhiteSpace(textBox2.Text.Trim()))
{
MessageBox.Show("账号不能为空");
return;
}
if (string.IsNullOrWhiteSpace(textBox3.Text.Trim()))
{
MessageBox.Show("账号不能为空");
return;
}
if (textBox2.Text.Trim() != textBox3.Text.Trim())
{
MessageBox.Show("两次输入的密码不一样,请确认一样");
return;
}
string str = string.Format("select * from logon where username='{0}' ", textBox1.Text);
string str1= string.Format("insert into logon values ('{0}','{1}')", textBox1.Text,textBox2.Text);
MessageBox.Show(str1);
MessageBox.Show(str);
SqlCommand sqlcom = new SqlCommand(str, sql.sqlcon);
SqlDataReader dr = sqlcom.ExecuteReader();
if (dr.HasRows)
{
dr.Close();
MessageBox.Show("此账号已经注册。不允许重复注册");
}
else
{
dr.Close();
Thread.Sleep(50);
SqlCommand sqlcom1 = new SqlCommand(str1,sql.sqlcon);
sqlcom1.ExecuteNonQuery();
MessageBox.Show("注册成功");
}
}
}
}