C#我用Visual Studio 2008编了一个Windows登陆窗体,建了一个账号密码的数据库,当点登陆时怎么连接数据库
有意者 请加QQ461236623 成为同行网友 本人万分感激 ! 展开
在此假设你用的数据库为SQL Server数据库,数据库名为“Test”,用户表名为“Users”,表结构碧塌为Users(UserName,Password),代码如下:
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 LoginTest
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
//创建Connection对象
SqlConnection con = new SqlConnection("server=.;database=Test;uid=sa;pwd=");
//创建Command对象
SqlCommand cmd = new SqlCommand(string.Format("SELECT COUNT(*) FROM Users WHERE UserName='{0}' AND Password='{1}'",
this.txtUserName.Text.Trim(), this.txtPwd.Text.Trim()), con);
try
{
//打开数据库连接
con.Open();
//如果执行cmd,返回值为1,提示用户登录成功,否则提示登录失败
if ((int)cmd.ExecuteScalar() == 悔槐圆1)
{
MessageBox.Show("登录成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
明散 {
MessageBox.Show("登录失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
//如果捕获到异常,提示用户数据库操作失败
MessageBox.Show("数据库操作失败!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
//关闭数据库连接
con.Close();
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)//登录按钮
{
string username = txtname.Text.Trim();
string pwd = txtpwd.Text.Trim();
string sql = "select * from Admin where Username='" + username + "' and Pwd='" + pwd + "' and Type=0";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LinXuan"].ConnectionString);
con.Open();//打开连接
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();//执行命令
if (dr.Read())
{
Session["myadmin"] = username;//保存登录名
Response.Redirect("manage.aspx", true);//登亏信颂录成功跳转页面
}
else
{
Response.Write("<script language=javascript>alert('用户名或者密码不正确!坦嫌请销郑重新输入!');</script>");
}
dr.Close();
con.Close();
}
}
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void btnlogin_Click(object sender, EventArgs e)
{//获取用户输入
string username = txtname.Text.ToString();
string password = txtpwd.Text.ToString();
//判断用户是否输入用户名和密码
if (username == "" | password == "")
{
lblerr.Visible = true;
lblerr.Text = "请输入启皮用户名和密码";
}
else
{//从配置文件前竖中读取字符串慧旁大
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["MySqlConnection"]);
//测试字符串是否正确
SqlConnection myconn = new SqlConnection(settings);
//打开数据库连接
myconn.Open();
//创建sql语句
string mysql = "select * from Employee where empName=" + "'"
+ username + "'" + "and empPwd=" + "'" + password + "'";
//创建命令对象
SqlCommand mycmd = new SqlCommand(mysql, myconn);
//执行命令,结果传递给适配器
SqlDataReader mydr = mycmd.ExecuteReader();
try
{
if (mydr.Read())
{//身份验证成功
lblerr.Visible = true;
lblerr.Text = "登录成功!";
}
else
{//身份验证失败
lblerr.Visible = true;
lblerr.Text = "登录失败!";
}
}
finally
{ //关闭连接
mydr.Close();
myconn.Close();
}
}
}
protected void btnclear_Click(object sender, EventArgs e)
{
txtname.Text = "";
txtpwd .Text= "";
}
protected void Page_Load(object sender, EventArgs e)
{
lblerr.Visible = false;
}
}