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 StudentInfoSystem
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
public static string userName ;//記錄登錄用戶名字//主表單中使用
public static string userRight;//記錄登錄用戶的權限//主表單中使用
SqlConnection conn;
SqlCommand com;
SqlDataAdapter da;
SqlDataReader dr;
private void frmLogin_Load(object sender, EventArgs e)
{
try
{
conn = new SqlConnection("server=(local)\\sqlexpress;integrated security=true;database=StudentBC");
DataSet ds = new DataSet();
da = new SqlDataAdapter("select 用戶名稱 from 用戶表", conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
da.Fill(ds, "用戶表");
conn.Close();
cbxUserName.DataSource = ds.Tables["用戶表"];
cbxUserName.DisplayMember = "用戶名稱";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void cbxUserName_SelectedIndexChanged(object sender, EventArgs e)//查詢權限值
{
try
{
com = new SqlCommand("select 用戶名稱,用戶權限 from 用戶表 where 用戶名稱='" + cbxUserName.Text + "'", conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
dr = com.ExecuteReader();
if (dr.Read())
{
lblUserRight1.Text = dr["用戶權限"].ToString();
userRight = lblUserRight1.Text;
}
dr.Close();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void btnLogin_Click(object sender, EventArgs e)//可以用讀取器類也可以用資料集類實作
{
//用資料集類實作
try
{
da = new SqlDataAdapter("select * from 用戶表 where 用戶名稱='" + cbxUserName.Text.Trim() + "' and 用戶密碼='" + txtUserPwd.Text.Trim() + "'", conn);
DataSet ds = new DataSet();
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
da.Fill(ds, "用戶表");
conn.Close();
if (ds.Tables["用戶表"].Rows.Count > 0)
{
userName = cbxUserName.Text;
frmMain frmmain = new frmMain();
this.Hide();
frmmain.Show();
}
else
{
MessageBox.Show("用戶名或密碼錯誤!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtUserPwd.Text = "";
cbxUserName.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
//用讀取器類實作
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void frmLogin_Load_1(object sender, EventArgs e)
{
}
}
}

uj5u.com熱心網友回復:
這不是c+嗎轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/62314.html
標籤:其他開發語言
上一篇:入門小白求助
