封装类
class Class1
{
public static SqlConnection con;
public static DataTable dt;
//数据库连接
public static SqlConnection getcon()
{
string str = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Txl;Data Source=DESKTOP-3DTIOB8"
con = new SqlConnection(str);
con.Open();
return con;
}
//查询,获取数据表
public static DataTable getdatable(string sql)
{
getcon();
SqlDataAdapter da = new SqlDataAdapter(sql,con);
dt = new DataTable();
da.Fill(dt);
con.Close();
return dt;
}
//增删改
public static int geExecuteNonQuery(string sql)
{
con=getcon();
SqlCommand cmd = new SqlCommand(sql,con);
int i = cmd.ExecuteNonQuery();
return i;
}
//查询
public static int geExecuteScalar(string sql)
{
getcon();
SqlCommand cmd = new SqlCommand(sql,con);
int i = Convert.ToInt32(cmd.ExecuteScalar());
return i;
}
}
登录模块
代码
public partial class Login : Form
{
public static string user = null;//用户名,获取当前登录的用户名
public Login()
{
InitializeComponent();
}
//登录页面布局
private void Form1_Load(object sender, EventArgs e)
{
}
//登录按钮
private void button1_Click(object sender, EventArgs e)
{
string sql = "select count(*) from Login where Users='"+textBox1.Text.Trim()+"' and Password='"+textBox2.Text.Trim()+"'";
int i = Class1.geExecuteScalar(sql);//调用封装类的查询方法
if (i > 0)//说明数据表有与输入的用户名和密码匹配
{
user = textBox1.Text.Trim();//获取用户名
Manager f = new Manager();
f.Show();//打开manager页面
this.Hide();//隐藏当前页面
}
else
{
MessageBox.Show("用户名或密码有误!请重新输入");
}
}
}
主界面模块
代码
public partial class Manager : Form
{
public Manager()
{
InitializeComponent();
}
private void Manager_Load(object sender, EventArgs e)
{
string sql = "select * from Connection where Users='" + Login.user + "'";
dataGridView1.DataSource = Class1