C#系统登录功能实现窗体
C#系统登录功能实现窗体:
一般的登陆界面,都是利用用户名和密码在数据库的匹配关系,来实现登陆的跳转功能。
首先介绍用户数据表的设计。
SQL数据表:
其中ID列需要设置好增量标识,随着用户的增加,ID的值递增,避免重复。
C#中对数据库的操作类
class DataBase
{
public static string Login_ID = "";
public static string Login_Name = "";
public static SqlConnection My_Conn;
public static string openConnstr = @"Data Source=服务器名;Database=数据库名;User id=数据库用户名;PWD=密码";
public static SqlConnection getcon()
{
My_Conn = new SqlConnection(openConnstr);
My_Conn.Open();
return My_Conn;
}
public void con_open()
{
getcon();
}
public void conn_close()
{
if (My_Conn.State == ConnectionState.Open)
{
My_Conn.Close();
My_Conn.Dispose();
}
}
public SqlDataReader getsdr(string sqlstr)
{
getcon();
SqlCommand My_com = My_Conn.CreateCommand();
My_com.CommandText = sqlstr;
SqlDataReader My_Reader = My_com.ExecuteReader();
return My_Reader;
}
public void dosqlcom(string sqlstr)
{
getcon();
SqlCommand sqlcom = new SqlCommand(sqlstr, My_Conn);
sqlcom.ExecuteNonQuery();
sqlcom.Dispose();
conn_close();
}
public DataSet getDs(string sqlstr, string tableName)
{
getcon();
SqlDataAdapter sqlda = new SqlDataAdapter(sqlstr, My_Conn);
DataSet My_DataSet = new DataSet();
sqlda.Fill(My_DataSet, tableName);
conn_close();
return My_DataSet;
}
}
注意:密码不是本机登录密码
登陆窗体的代码实现:
public partial class Form_Login : Form
{
DataBase userDB = new DataBase();
public Form_Login()
{
InitializeComponent();
}
private void btn_Login_Click(object sender, EventArgs e)
{
if (tb_UserName.Text != "" & tb_UserPwd.Text != "")
{
SqlDataReader temdr = userDB.getsdr("select * from tb_UserList where UserName='" + tb_UserName.Text.Trim() + "' and UserPwd='" + tb_UserPwd.Text.Trim() + "'");
bool ifcom = temdr.Read();
if (ifcom)
{
Form_Main frmMain = new Form_Main();
frmMain.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误!");
tb_UserName.Text = "";
tb_UserPwd.Text = "";
}
userDB.conn_close();
}
else
MessageBox.Show("请填写用户名和密码!");
}
private void Form_Login_Load(object sender, EventArgs e)
{
try
{
userDB.con_open();
userDB.conn_close();
tb_UserName.Text = "";
tb_UserPwd.Text = "";
}
catch
{
MessageBox.Show("数据库连接错误!");
Application.Exit();
}
}
private void Form_Login_Activated(object sender, EventArgs e)
{
tb_UserName.Focus();
}
private void btn_Quit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
希望可用帮到你,喜欢记得点赞+购买我的网络专栏