日期:2014-05-17 浏览次数:20410 次
using System;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
string name = this.txtName.Text; //得到登陆帐号
string pwd = this.txtpwd.Text; //得到登陆密码
SqlConnection con = new SqlConnection(); //连接数据库对象
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True";
// 创建命令(SqlCommand)对象(cmd)
SqlCommand cmd = new SqlCommand();
cmd.Connection = con; // 指示命令发给哪个数据库
cmd.CommandType = CommandType.Text; // 指示是什么类型的命令
cmd.CommandText = "SELECT username,password FROM user WHERE username='" + txtName.Text + "' and password='" + txtpwd.Text + "'";
con.Open(); // 打开连接,准备接受命令
SqlDataReader reader = cmd.ExecuteReader();
//// Read方法开始读取数据,读到记录,则返回true,并指向下一条记录,否则返回false
if (reader.Read())
{
con.Close();
Response.Redirect("good.aspx");
}
else
{
con.Close();
Response.Write("<script>alert('登录失败')</script>");
}
}