📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _7计应的班级管理系统
{
public partial class Form1 : Form
{
SqlConnection dbconn;//定义一连接集
SqlDataAdapter dbda;//定义一个试配器
DataSet dbrs;//定义一数据集相当于存储于用于存储于一张虚拟表
//int judge = 0;
private string nowid;
public string Nowid
{
get
{
return nowid;
}
set
{
nowid = value;
}
}
private string nowpassword;
public string Nowpassword
{
get
{
return nowpassword;
}
set
{
nowpassword = value;
}
}
private static string nowpower;
public string Nowpower
{
get
{
return nowpower;
}
set
{
nowpower = value;
}
}
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
dbda = new SqlDataAdapter("select * from login where 用户名 ='" + textBox1.Text + "'", dbconn);//用试配器检索到dbconn的数据库然后用dbconn的查询分析器进行查询"select * from ituser where username ='" + textBox1.Text + "'"把查询出来的行数返回给fill()的填充方法
dbrs = new DataSet();
dbda.Fill(dbrs);//然后把检索到的textbox1.text的表的行"select * from login where username ='" + textBox1.Text + "'返回fill()方法填充到的数据集里面相当存储在数据集里的内存当中
if (dbrs.Tables[0].Rows.Count == 0)//检验数据集里是否有一张表
{
MessageBox.Show("用户不存在!", "杨俊辉提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
// MessageBox.Show("用户不存在!", "信息提示", MessageBoxButtons.OK);//如果没表显示用户不存在
}
else
{
if (dbrs.Tables[0].Rows[0].ItemArray[1].Equals(textBox2.Text))//若用户名通过则验证密码第一张表的第一行的第二列是否等于textbox2.text//为什么row[0]方括号里的为0不为1或2或3呢,因为用试配器查询的数据只有一行,然后把这行赋值给数据集里而数据集里只有一行数据所以么row[0]方括号里的只能为0
{
MessageBox.Show("成功登录!", "杨俊辉提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
// MessageBox.Show("成功", "信息提示", MessageBoxButtons.OK);
Form2 student = new Form2();
student.Show();
this.Hide();
}
else
{
MessageBox.Show("密码错误!", "杨俊辉提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
//MessageBox.Show("密码错误", "信息提示");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
dbconn = new SqlConnection();
dbconn.ConnectionString = "user id =sa;data source=localhost;initial catalog=学生数据库1";// 相当于连接数据库地址
dbconn.Open();//连接打开
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -