sqldatareader

atareader对象提供只读单向数据的快速传递,单向:您只能依次读取下一条数据;只读:DataReader中的数据是只读的,不能修改;相对地,DataSet中的数据可以任意读取和修改. 

  它有一个很重要的方法,是Read,是个布尔值,作用是前进到下一条数据,一条条的返回数据,当布尔值为真时执行,为假时跳出。如 

while(dr.Read())
{
Response.write(dr["UserName"]
}
dr.close();
以下是用户登录判断时候合法用户的代码
SqlConnection con = DB.WebConnection();//通过类调用连接上数据库

con.Open();//打开连接

SqlCommand com = new SqlCommand();
com.CommandText = "Select * from Users where UserName='" + tbUserName.Text + "'";
com.CommandType = CommandType.Text;
com.Connection = con;
SqlDataReader reader = com.ExecuteReader();
if (reader.Read())
{
int UserID = reader.GetInt32(0);
string Password = reader["UserPassword"].ToString();
string Password0 = tbUserPassword.Text;
if (Password == Password0)
{
Session["uid"] = UserID;
Session["name"] =tbUserName.Text;
Response.Redirect("index.aspx");
}
else
{
Response.Redirect("login.aspx");
}
}
else
{
Response.Redirect("login.aspx");
}


  用这种方法不仅能判断用户名是否合法,还可以很方便地获取更多关于该用户的信息,其中,我比较喜欢用的是string Password = reader["UserPassword"].ToString();这种方法,但是这种方法似乎不能获取int类型的字段数据,比如这里的UserId,只能用int UserID = reader.GetInt32(0);这种方法获取它的值。不知道用字段名有没有方法获取到UserId的值。

转载于:https://www.cnblogs.com/Penfield/p/4290577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值