国产凌凌漆李香琴对白:用C#写一个简单的Login窗口加载数据库的改进
来源:百度文库 编辑:偶看新闻 时间:2024/04/27 22:18:59
用C#写一个简单的Login窗口加载数据库的改进
分类: C# 20052007-10-05 09:36398人阅读评论(0)收藏举报数据库结构如下:
userId userName userPassWord
0001 admin admin
0002 jingang 123456
0003 sys sys
0004 0000
0005 0005 123456
10001 jingang jingang
10002 admin admin
10003 sys sys
10004 0000 0000
frmlogin.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 login
{
public partial class frmlogin : Form
{
private int nLoginCount = 0;
private const int MAX_LOGIN_COUNT = 3;
public bool islogin = false;
private UserInfo uiLogin;
public frmlogin(ref UserInfo ui)
{
InitializeComponent();
uiLogin = ui;
}
private void btnOK_Click(object sender, EventArgs e)
{
if (!System.IO.File.Exists("local.ini"))
{
MessageBox.Show("配置文件不存在,请先检查配置文件!");
return;
}
globeSet.DBConnectString = System.IO.File.ReadAllText("local.ini");
SqlConnection conn = new SqlConnection(globeSet.DBConnectString);
try
{
//连接
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show("数据连接失败!,请先检查服务器或配置文件!" + ex.Message);
return;
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select count(*) from users where userName='" + txtUserName.Text + "'" + " and userPassWord='" + txtPassword.Text + "'";
int n = Convert.ToInt32(cmd.ExecuteScalar());
//关闭连接
conn.Close();
if (n > 0)
{
globeSet.txtUserName = txtUserName.Text;
islogin = true;
this.DialogResult = DialogResult.OK;
}
else
{
// 错误的用户名称或密码
nLoginCount++;
if (nLoginCount == MAX_LOGIN_COUNT)
// 超过3次
this.DialogResult = DialogResult.Cancel;
else
{
MessageBox.Show("用户名或密码错误");
txtUserName.Focus();
}
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void frmlogin_FormClosing(object sender, FormClosingEventArgs e)
{
// Check whether form is closed with dialog result
if (this.DialogResult != DialogResult.Cancel &&
this.DialogResult != DialogResult.OK)
e.Cancel = true;
}
private void frmlogin_Load(object sender, EventArgs e)
{
this.AcceptButton = this.btnOK;
this.CancelButton = this.btnCancel;
this.txtUserName.Focus();//刷新用户名框光标定位在此
}
}
}
globeSet.cs类的代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace login
{
class globeSet
{
static public string DBConnectString = "";
static public string txtUserName = "";
}
}
using System;
using System.Collections.Generic;
using System.Text;
UserInfo类的代码:
namespace login
{
public class UserInfo
{
private string strUserName;
private string strPassword;
public string UserName
{
get { return strUserName; }
set { strUserName = value; }
}
public string Password
{
get { return strPassword; }
set { strPassword = value; }
}
public UserInfo()
{
strUserName = "";
strPassword = "";
}
}
}
Program.cs的代码:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace login
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
UserInfo ui = new UserInfo();
frmlogin myLogin = new frmlogin(ref ui);
if (myLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new frmMain());
}
}
}
}