首先我定义了一个DBHelper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace XiaoCiLang
{
class DBHelper
{
//创建连接字符串
public static string connString = "server=.;Database=XiaoCiLang;uid=sa;pwd=123456";
public static SqlConnection Conn = null;
public static void InitConnection()
{
if (Conn == null)
Conn = new SqlConnection(connString);
if (Conn.State == ConnectionState.Closed)
Conn.Open();
if (Conn.State == ConnectionState.Broken)
Conn.Close();
Conn.Open();
}
//定义断开式查询方法
public static DataTable GetDataTable(string sql)
{
InitConnection();
DataTable table = new DataTable();
SqlDataAdapter dap = new SqlDataAdapter(sql, Conn);
dap.Fill(table);
Conn.Close();
return table;
}
//定义增删改方法
public static bool ExcuteNonQuery(string sql)
{
InitConnection();
SqlCommand cmd = new SqlCommand(sql, Conn);
int result = cmd.ExecuteNonQuery();
return result > 0;
}
}
}
第一个窗体
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;
namespace XiaoCiLang
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//退出程序
this.Close();
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//创建logon窗体对象
logon lo = new logon();
//调用Show()方法,显示窗体
Hide();//当前隐藏窗体
lo.ShowDialog();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
}
private void yonghu_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string Name = this.textBox2.Text;
string Word = this.textBox3.Text;
//验证非空
if (Name==""||Word=="")
{
MessageBox.Show("用户名和密码不能为空!");
}
else
{
//验证用户名和密码是否匹配
if (Name=="admin" && Word=="admin" )
{//登录成功
MessageBox.Show("登录成功");
}
else
{
//登陆失败
MessageBox.Show("登陆失败");
}
}
}
private void login_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
}
}
第二个窗体
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;
namespace XiaoCiLang
{
public partial class logon : Form
{
public logon()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)//注册的放回登录按钮
{
login lg = new login();
Hide();
lg.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)//注册的确定按钮
{
//定义添加SQL语句
string sql = string.Format("insert into users(usersname,phone,pwd) values ('{0}','{1}','{2}')", textBox1.Text, textBox2.Text,
textBox3.Text);
if (textBox1.Text == "")
{
MessageBox.Show("请输入昵称!");
}
else if (textBox2.Text == "")
{
MessageBox.Show("请输入账号!");
}
else if (textBox3.Text == "")
{
MessageBox.Show("请输入密码!");
}
else
{
bool result = DBHelper.ExcuteNonQuery(sql);//调用方法执行SQL语句,返回受影响的行数
if (result == true)
{
MessageBox.Show("成功添加数据!");
this.Close();
}
else
{
MessageBox.Show("数据添加失败!");
}
}
}
private void logon_FormClosing(object sender, FormClosingEventArgs e)
{
//Application.Exit();
}
private void logon_Load(object sender, EventArgs e)
{
}
}
}