类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
我要验证用户然后登陆,循环读出数据库中用户名和密码,下面是我的代码
dim constr as string = "192.168.0.1;database=netconfer;password=zsjyc;user id=maverick;"
dim sqlstr as string = "select * from stuinfos"
dim objcon as new sqlconnection(constr)
dim objdataset as new dataset
dim objAdapter as new sqldataAdapter(sqlstr,objconn)
objAdapter.fill(objconn,"stuinfos")
dim username,password as string
username = textbox1.text
password = textbox2.text
下面的代码怎样写呢?谢谢各位。
网友回答:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim var_user, var_pass, selectcommand As String
Dim var_count As Integer
Dim SelectCmd_checkuser As SqlCommand
Dim dtrResults As SqlDataReader
var_user = textuser.Text
var_pass = TextBox2.Text
selectcommand = "select count(*) from clerk where sn=@sn and password=@password"
If var_user <> "" Then
If var_pass <> "" Then
If IsPostBack Then
myconnect.Open()
SelectCmd_checkuser = New SqlCommand(selectcommand, myconnect)
SelectCmd_checkuser.Parameters.Add(New SqlParameter("@sn", var_user))
SelectCmd_checkuser.Parameters.Add(New SqlParameter("@password", var_pass))
Label1.Text = SelectCmd_checkuser.ExecuteScalar
var_count = dtrResults(0)
If CInt(SelectCmd_checkuser.ExecuteScalar) > 0 Then
Session("passed") = True
Session("person_id") = var_user
myconnect.Close()
Response.Write("<script>parent.location.href=information/main.aspx,newinformation,toolbar=no,resizable=yes,directories=no,menubar=no,location=no;</script>")
Response.Redirect("information/main.aspx")
Else
myconnect.Close()
Label1.Text = "username or password is not correct!"
End If
myconnect.Close()
End If
Else
Label1.Text = "Please input password!"
End If
Else
Label1.Text = "Please input username!"
End If
End Sub
OleDbConnection myconnection=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ Server.MapPath("../luntang.mdb"));
OleDbCommand mycommand=new OleDbCommand("select * from bbsuser where username="+FormatString(username.Text)+" and userpassword="+FormatString(password.Text)+"",myconnection);
mycommand.Connection.Open();
OleDbDataReader dr=mycommand.ExecuteReader();
if(dr.HasRows)
{
dr.Read();
Session["username"]=dr["username"];
Session["password"]=dr["userpassword"];
dr.Close();
mycommand.Connection.Close();
Response.Redirect("bbs1.aspx?leibie="+Session["leibie"]+"");
}
else
{
//错误用户名或密码
dr.Close();
mycommand.Connection.Close();
}
(验证用户然后登陆)应该这样好点:
string sSql="Select * from stuinfos where username="+textbox1.text+" and password="+"";
SqlCommand cmd=new SqlCommand(sSql,objcon);
int iRet=cmd.ExecuteNonQuery();
if(iRet!=0)
//这里说明已经验证成功了。
else
//这里说明已经验证不成功。
select * from stuinfos where name=username and password2=password
OleDbCommand mycmd =new OleDbCommand(Sql,mycnn);
OleDbDataReader myDr =mycmd.ExecuteReader();
if(myDr.Read())
{
}
else
{
}
dim dr as datarow
dr = objdataset.stuinfos.Select("username=" & TextBox1.Text.Trim & " and password=" & Textbox2.Text.Trim & ")
If dr.Length = 0 Then
Me.Label1.Text = "输入不正确,请重输"
Else
Me.Label1.Text = "输入正确"
Your Code
End If
Response.Redirect("../careersystem/protocol/stu-login.aspx?student_no="& textbox1.text)
dim a as string
a=Request.QueryString("student_no")
你设定一个session值,到每个页面都可以用了,如果这个session值为空,证明没有登录,跳出login.aspx页重新登录