通过在页面输入账号密码,实现从数据库查询数据并返回,验证成功后登录,打开主界面。
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.Threading;
using System.Data.SqlClient;
using System.Net;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public static Form1 pForm1 = null;
private SqlConnection master;//定义数据连接对象
private SqlCommand cmd;//定义数据命令对象
private SqlDataAdapter sqlada;//定义数据适配器对象
private SqlCommandBuilder sqlbuid;//定义CommandBuilder对象
private DataSet ds;//定义数据集对象
public Form1()
{
pForm1 = this;
InitializeComponent();
}
private void label7_Click(object sender, EventArgs e)
{
}
private void txtCardNum_TextChanged(object sender, EventArgs e)
{
}
private void btnOpenPort_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
String textzhanghao = zhanghao.Text;
String textmima = mima.Text;
string zhanghao1;
string mima1;
string connString = "server=DESKTOP-=\\SQLEXPRESS; database=master;integrated security=SSPI";
string sqlString = "select sno,sname from student where sno='" + textzhanghao + "'and sname='" + textmima + "' ";
master = new SqlConnection(connString);
master.Open();//打开连接
SqlCommand sqlCommand = new SqlCommand(sqlString, master);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)//满足用户名与密码一致,进入下一个界面
{
frm2.Show();
this.Hide();
}
else
MessageBox.Show("账号密码不正确");
}
}
}
跳转界面
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 WindowsFormsApp2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}