常用代码:
1、检查Cookies是否打开:
if (Request.Cookies["CheckCode"] == null)
{
Response.Write("<script language=javascript>alert('您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。')</script>");
return;
}
2、对字符串作比较:
String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text.ToUpper(), true) != 0
3、连接字符串
请在使用这段之前添加引用:using System.Data.SqlClient; ///引入命名空间。
SqlConnection Con = new SqlConnection(ConfigurationManager.AppSettings["ConSQL"]);
Con.Open();
SqlCommand Com = new SqlCommand("select * from tb_User where UserName='" + this.txtName.Text + "' and PassWord='" + this.txtPwd.Text + "' and Popedom='0'", Con);
SqlDataReader dr = Com.ExecuteReader();
dr.Read();
插入一条记录:
string cmdtxt = "INSERT INTO tb_LoginLog(UserName,LoginTime,LoginIP)";
cmdtxt += " VALUES('" + dr["UserName"].ToString() + "','" + DateTime.Now + "','" + Request.UserHostAddress + "')";
SqlData da = new SqlData();
da.ExceSQL(cmdtxt);
4、判断是否有记录
dr.HasRows
5、获取记录集中的某个字段
dr["UserName"],可以调用一个公共方法ToString()进行类型转换。
6、session的使用
Session["Name"] = ""注意这里是[ ]中括号。
附:一个完整的登录代码
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 IndexFrame_Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOK_Click(object sender, EventArgs e)
{
if (Request.Cookies["CheckCode"] == null)
{
Response.Write("<script language=javascript>alert('您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。')</script>");
return;
}
if (this.CheckBox1.Checked)
{
if (String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text.ToUpper(), true) != 0)
{
Response.Write("<script language=javascript>alert('验证码错误,请重新输入!')</script>");
return;
}
else
{
SqlConnection Con = new SqlConnection(ConfigurationManager.AppSettings["ConSQL"]);
Con.Open();
SqlCommand Com = new SqlCommand("select * from tb_User where UserName='" + this.txtName.Text + "' and PassWord='" + this.txtPwd.Text + "' and Popedom='0'", Con);
SqlDataReader dr = Com.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
Session["UserName"] = dr["UserName"].ToString();
Session["Name"] = dr["Name"].ToString();
Session["PassWord"] = dr["PassWord"].ToString();
Session["Popedom"] = dr["Popedom"].ToString();
Session.Timeout = 100;
string cmdtxt = "INSERT INTO tb_LoginLog(UserName,LoginTime,LoginIP)";
cmdtxt += " VALUES('" + dr["UserName"].ToString() + "','" + DateTime.Now + "','" + Request.UserHostAddress + "')";
SqlData da = new SqlData();
da.ExceSQL(cmdtxt);
Response.Redirect("Module/Index.aspx");
}
else
{
Response.Write("<script language=javascript>alert('用户名或密码不正确!');</script>");
}
dr.Close();
}
}
else
{
if (String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text.ToUpper(), true) != 0)
{
Response.Write("<script language=javascript>alert('验证码错误,请重新输入!')</script>");
return;
}
else
{
SqlConnection Con = new SqlConnection(ConfigurationManager.AppSettings["ConSQL"]);
Con.Open();
SqlCommand Com = new SqlCommand("select * from tb_User where UserName='" + this.txtName.Text + "' and PassWord='" + this.txtPwd.Text + "' and Popedom='1'", Con);
SqlDataReader dr = Com.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
Session["UserName"] = dr["UserName"].ToString();
Session["Name"] = dr["Name"].ToString();
Session["PassWord"] = dr["PassWord"].ToString();
Session["Popedom"] = dr["Popedom"].ToString();
string cmdtxt = "INSERT INTO tb_LoginLog(UserName,LoginTime,LoginIP)";
cmdtxt += " VALUES('" + dr["UserName"].ToString() + "','" + DateTime.Now + "','" + Request.UserHostAddress + "')";
SqlData da = new SqlData();
da.ExceSQL(cmdtxt);
Response.Redirect("Module/Index.aspx");
}
else
{
Response.Write("<script language=javascript>alert('用户名或密码不正确!');</script>");
}
dr.Close();
}
}
}
}