Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myConnection As New SqlConnection()
myConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) If username.Text = "" Or password.Text = "" Then
msg.text = "帐号和密码不能为空"
Else
Dim InsertCmd As String = "select * from admin where username='" + username.Text + "'"
Dim MyCommand As New SqlCommand(InsertCmd, MyConnection)
Dim dr As SqlDataReader
MyCommand.Connection.Open()
dr = MyCommand.ExecuteReader()
If dr.Read() Then
If dr("pwd").ToString() = password.Text Then
Dim cookie As HttpCookie
cookie = New HttpCookie("username", username.Text)
Response.AppendCookie(cookie)
Dim MyCookie As New HttpCookie("username")
MyCookie.Value = dr("username").ToString()
MyCookie.Expires = DateTime.Now.AddHours(1)
Response.Cookies.Add(MyCookie)
Response.Redirect("default.aspx")
Else
msg.text = "密码错误"
End If
Else
msg.text = "帐号错误"
End If
MyCommand.Connection.Close()
End If
End Sub
错误提示
C:\Inetpub\wwwroot\ks\login.aspx.vb(29): 未定义类型“SqlConnection”。
C:\Inetpub\wwwroot\ks\login.aspx.vb(30): 未定义类型“SqlConnection”。
C:\Inetpub\wwwroot\ks\login.aspx.vb(36): 未定义类型“SqlCommand”。
C:\Inetpub\wwwroot\ks\login.aspx.vb(37): 未定义类型“SqlDataReader”。如何解决?
Dim myConnection As New SqlConnection()
myConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) If username.Text = "" Or password.Text = "" Then
msg.text = "帐号和密码不能为空"
Else
Dim InsertCmd As String = "select * from admin where username='" + username.Text + "'"
Dim MyCommand As New SqlCommand(InsertCmd, MyConnection)
Dim dr As SqlDataReader
MyCommand.Connection.Open()
dr = MyCommand.ExecuteReader()
If dr.Read() Then
If dr("pwd").ToString() = password.Text Then
Dim cookie As HttpCookie
cookie = New HttpCookie("username", username.Text)
Response.AppendCookie(cookie)
Dim MyCookie As New HttpCookie("username")
MyCookie.Value = dr("username").ToString()
MyCookie.Expires = DateTime.Now.AddHours(1)
Response.Cookies.Add(MyCookie)
Response.Redirect("default.aspx")
Else
msg.text = "密码错误"
End If
Else
msg.text = "帐号错误"
End If
MyCommand.Connection.Close()
End If
End Sub
错误提示
C:\Inetpub\wwwroot\ks\login.aspx.vb(29): 未定义类型“SqlConnection”。
C:\Inetpub\wwwroot\ks\login.aspx.vb(30): 未定义类型“SqlConnection”。
C:\Inetpub\wwwroot\ks\login.aspx.vb(36): 未定义类型“SqlCommand”。
C:\Inetpub\wwwroot\ks\login.aspx.vb(37): 未定义类型“SqlDataReader”。如何解决?
using System.Data.SqlClient
Imports System.Data.SqlClient
using System.Data.SqlClient
Imports System.Data.SqlClient
加入这两句,是因为你没有导入命名空间,你可以通过.net帮助文档是查看这个类属于哪一个命名空间。
Imports System.Data.SqlClient
加到哪个文件 login.aspx 还是 login.aspx.vb ??
Imports System.Data.SqlClient
是 login.aspx.vb
引用命名空间