C#应用程序登陆界面如何连接到SQL数据库验证用户名和用户密码,我要详细代码和详细注释……
2013-10-22
展开全部
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 MyKtv
{
public partial class FrmLogin : Form
{
public static string UserName;
public static string UserPwd;
public FrmLogin()
{
InitializeComponent();
}
private void btnclear_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否要退出??", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
this.Close();
}
}
private void btnlogin_Click(object sender, EventArgs e)
{
CheckUser();
}
public void CheckUser()
{
UserName = txtname.Text;
UserPwd = txtpwd.Text;
if (UserName.Trim() == "")
{
MessageBox.Show("请输入用户名");
this.txtname.Focus();
}
else if (UserPwd.Trim() == "")
{
MessageBox.Show("请输入密码");
this.txtname.Focus();
}
else
{
string sql = string.Format("select count(*) from admin_info where admin_name='{0}' and admin_pwd='{1}'", UserName, UserPwd);
try
{
DBHepler.con.Open();
SqlCommand com = new SqlCommand(sql, DBHepler.con);
int i = (int)com.ExecuteScalar();
if (i > 0)
{
FrmAdmin admin = new FrmAdmin();
admin.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
DBHepler.con.Close();
}
}
}
private void FrmLogin_Load(object sender, EventArgs e)
{
}
}
}
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 MyKtv
{
public partial class FrmLogin : Form
{
public static string UserName;
public static string UserPwd;
public FrmLogin()
{
InitializeComponent();
}
private void btnclear_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否要退出??", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
this.Close();
}
}
private void btnlogin_Click(object sender, EventArgs e)
{
CheckUser();
}
public void CheckUser()
{
UserName = txtname.Text;
UserPwd = txtpwd.Text;
if (UserName.Trim() == "")
{
MessageBox.Show("请输入用户名");
this.txtname.Focus();
}
else if (UserPwd.Trim() == "")
{
MessageBox.Show("请输入密码");
this.txtname.Focus();
}
else
{
string sql = string.Format("select count(*) from admin_info where admin_name='{0}' and admin_pwd='{1}'", UserName, UserPwd);
try
{
DBHepler.con.Open();
SqlCommand com = new SqlCommand(sql, DBHepler.con);
int i = (int)com.ExecuteScalar();
if (i > 0)
{
FrmAdmin admin = new FrmAdmin();
admin.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误!请重新输入!");
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
DBHepler.con.Close();
}
}
}
private void FrmLogin_Load(object sender, EventArgs e)
{
}
}
}
2013-10-22
展开全部
有比这个更简单的么?我的思路跟上面的一样,执行完查询语句后,返回一个标识,判断如果>0就登录成功,否则登录失败!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询