C#做登陆窗体

[复制链接]
查看11 | 回复2 | 2008-4-3 16:37:14 | 显示全部楼层 |阅读模式
如果是做ASP.net 2.0以上的话,可以用微软写好的Login控件。只需要在写一个用于验证秘密的类,传给那个控件就可以了。如果是用SQLServer的话,只需要按.net的要求,把用户名和密码设定好,直接就可以使用了。
回复

使用道具 举报

千问 | 2008-4-3 16:37:14 | 显示全部楼层
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;using System.Data.OleDb;namespace MealRecordSystem{
public partial class Login : Form
{
string source = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + @"\\MealRecord.mdb";//连接数据库
public Login()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();//点击推出按钮关闭窗口
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (this.txtUser.Text.Trim() == "")
{
MessageBox.Show("请输入用户名!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtUser.Focus();
return;
}
if (this.txtpwd.Text.Trim() == "")
{
MessageBox.Show("请输入密码!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtpwd.Focus();
return;
}
string userName = txtUser.Text.ToString();
string pwd = txtpwd.Text.ToString();
string select = "SELECT adminPwd FROM Login WHERE adminName= '" + userName + "'";
OleDbConnection Sqlconn = new OleDbConnection(source);
try
{
Sqlconn.Open();
OleDbCommand Sqlcmd = new OleDbCommand(select, Sqlconn);
OleDbDataReader reader = Sqlcmd.ExecuteReader();
if (reader.Read())
{
if (pwd == reader["adminPwd"].ToString().Trim())
{
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("密码不正确","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
txtpwd.SelectAll();
txtpwd.Focus();
}
}
else
{
MessageBox.Show("用户名不存在", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtUser.SelectAll();
txtUser.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void txtpwd_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Return)
SendKeys.SendWait("{TAB}");
}
private void txtUser_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Return)
SendKeys.SendWait("{TAB}");
}
}}
回复

使用道具 举报

千问 | 2008-4-3 16:37:14 | 显示全部楼层
一楼的说法已经很全面了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行