日期:2014-05-18  浏览次数:20833 次

datareader在那关闭啊?
C# code
   Sub enter_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim conn As New SqlConnection("server=localhost;database=education;uid=sa;pwd=")
        Dim strsql1 As String
        strsql1 = "select * from login where userid='" & userid.text & "'"
        Dim cmd As New SqlCommand(strsql1, conn)
        conn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        If dr.Read() Then
            message.Text = "帐号已经注册过"
            conn.Close()
        Else
            Dim strsql2 As String
            strsql2 = "insert into login(userid,password,sex,birthday,email,telephone,name) values ('" & userid.Text & "','" & password.Text & "','" & sex.SelectedItem.Text & "','" & birthday.Text & "','" & email.Text & "','" & telephone.Text & "','" & name.Text & "')"
            Dim cmdr As New SqlCommand(strsql2, conn)
            'Try
            cmdr.ExecuteNonQuery()
            conn.Close()
            Response.Redirect("index.aspx")
            '    Catch ex As Exception
            '        message.Text = "没有注册成功"
            '    End Try
        End If
    End Sub


------解决方案--------------------
If dr.Read() Then
message.Text = "帐号已经注册过"
dr.Close()
conn.Close()
Else
dr.Close()
Dim strsql2 As String
strsql2 = "insert into login(userid,password,sex,birthday,email,telephone,name) values ('" & userid.Text & "','" & password.Text & "','" & sex.SelectedItem.Text & "','" & birthday.Text & "','" & email.Text & "','" & telephone.Text & "','" & name.Text & "')"
Dim cmdr As New SqlCommand(strsql2, conn)
'Try
cmdr.ExecuteNonQuery()
conn.Close()
Response.Redirect("index.aspx")
' Catch ex As Exception
' message.Text = "没有注册成功"
' End Try
End If