您的位置:首页 > 数据库

基于数据库的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);
}
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: