你能解释我为什么会收到这个错误吗?
ExecuteReader:连接属性尚未初始化
Protected Sub btnLogin_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btnLogin.Click
Dim name = txtUserName.Text.Trim
Dim Pass = txtPassword.Text.Trim
Try
Dim conn As SqlConnection
conn = DataBaseFunc.OpenConnection()
Dim sqlSt As String = "select * FROM AdminLog WHERE AdminName = '" & name & "' AND AdminPassword = '" & Pass & "'"
Dim SelCmd As SqlCommand = New SqlCommand(sqlSt, conn)
Dim RrecSet As SqlDataReader
RrecSet = SelCmd.ExecuteReader()
If RrecSet.Read() Then
Session("Admin") = RrecSet("AdminName")
Response.Redirect("~/AdminPages/AdminHome.aspx")
Else
lblMsg.Text = "<p>Rong Username or Password</p>"
txtUserName.Text = ""
txtPassword.Text = ""
txtUserName.Focus()
End If
Catch ex As Exception
lblMsg.Text = ex.Message
End Try
End Sub
数据库函数
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic
Public Class DataBaseFunc
Public Shared Sub EnsureOpenConnection(ByRef conn As SqlConnection)
Try
If conn.State <> Data.ConnectionState.Open Then
conn.Open()
End If
Catch ex As Exception
End Try
End Sub
Public Shared Function OpenConnection() As SqlConnection
Try
Dim ConnString As String = "server =.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BloodDb.mdf;Integrated Security=True;User Instance=True"
Dim conn As New SqlConnection(ConnString)
If conn.State <> Data.ConnectionState.Open Then
conn.Open()
End If
Return conn
Catch ex As Exception
Return Nothing
End Try
End Function
End Class