基于数据库的C#用户登录程序
2015-10-12 14:18
465 查看
这篇文章只是部分代码,是将一个项目拆开了的,避免放在一块儿太长,不便阅读。
原文在这儿,看的话可以从这儿开始:
源代码可从这儿下载:http://download.csdn.net/detail/tingzhiyi/9173473
效果图:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ADO
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
int n = 0;
private void button1_Click(object sender, EventArgs e)
{
if (tb_username.Text=="") MessageBox.Show("请输入用户名");
else if (tb_userpwd.Text=="") MessageBox.Show("请输入登录密码");
else
{
string constring = "server=Flz;database=product;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(constring))
{
string sql="select count(*) from login where username=@name and userpwd=@pwd";
SqlCommand comm = new SqlCommand(sql,conn);
comm.Parameters.AddWithValue("@name",tb_username.Text);
comm.Parameters.AddWithValue("@pwd",tb_userpwd.Text);
conn.Open();
int count=Convert.ToInt32(comm.ExecuteScalar());
#region 登录成功
if (count > 0)
{
operation f1 = new operation();
f1.Show();
this.Hide();
}
#endregion
#region 登录失败
else
{
MessageBox.Show("用户名或密码输入错误,请重新登录!");
tb_userpwd.Text = string.Empty;
tb_userpwd.Focus();
n++;
if (n>=3)
{
MessageBox.Show("密码输入次数太多,程序即将退出!");
Application.Exit();
}
}
#endregion
}
}
}
//启动程序光标跳到用户名框
private void login_Load(object sender, EventArgs e)
{
tb_username.Focus();
}
//重置用户名和密码
private void button2_Click(object sender, EventArgs e)
{
tb_username.Text = string.Empty;
tb_userpwd.Text = string.Empty;
tb_username.Focus();
}
//回车光标跳到密码框
private void tb_username_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar==13)//回车键的ASC码是13
{
tb_userpwd.Focus();
}
}
//回车登录
private void tb_userpwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar==13)
{
button1_Click(sender,e);
}
}
}
}
原文在这儿,看的话可以从这儿开始:
源代码可从这儿下载:http://download.csdn.net/detail/tingzhiyi/9173473
效果图:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ADO
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
int n = 0;
private void button1_Click(object sender, EventArgs e)
{
if (tb_username.Text=="") MessageBox.Show("请输入用户名");
else if (tb_userpwd.Text=="") MessageBox.Show("请输入登录密码");
else
{
string constring = "server=Flz;database=product;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(constring))
{
string sql="select count(*) from login where username=@name and userpwd=@pwd";
SqlCommand comm = new SqlCommand(sql,conn);
comm.Parameters.AddWithValue("@name",tb_username.Text);
comm.Parameters.AddWithValue("@pwd",tb_userpwd.Text);
conn.Open();
int count=Convert.ToInt32(comm.ExecuteScalar());
#region 登录成功
if (count > 0)
{
operation f1 = new operation();
f1.Show();
this.Hide();
}
#endregion
#region 登录失败
else
{
MessageBox.Show("用户名或密码输入错误,请重新登录!");
tb_userpwd.Text = string.Empty;
tb_userpwd.Focus();
n++;
if (n>=3)
{
MessageBox.Show("密码输入次数太多,程序即将退出!");
Application.Exit();
}
}
#endregion
}
}
}
//启动程序光标跳到用户名框
private void login_Load(object sender, EventArgs e)
{
tb_username.Focus();
}
//重置用户名和密码
private void button2_Click(object sender, EventArgs e)
{
tb_username.Text = string.Empty;
tb_userpwd.Text = string.Empty;
tb_username.Focus();
}
//回车光标跳到密码框
private void tb_username_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar==13)//回车键的ASC码是13
{
tb_userpwd.Focus();
}
}
//回车登录
private void tb_userpwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar==13)
{
button1_Click(sender,e);
}
}
}
}
相关文章推荐
- oracle wm_concat(column)函数的使用
- 基于数据库的C#产品信息管理系统,用户登录程序
- mysql主从复制
- 数据库性能之索引
- MongoDB——第一天 基础入门
- Oracle 11G 单机asm安装
- redis操作记录
- SQL UNION 和 UNION ALL 操作符
- MongoDB demo
- VS2013 编译 MySql Connector C 6.1.6
- MySQL索引类型
- Could not load oracle/sql/converter_xcharset/lx20354.glb.
- Redis 的安装配置
- Redis demo
- mac下安装redis
- MYSQL常用操作示例
- SQLite的简单介绍
- mysql yearweek修改开始日期
- Oracle spatial创建空间数据的字段详解
- Oracle数据库新创建实例会遇到的有关监听的问题