使用OdbcConnection登录Vb.net

玛丽

我正在为数据库使用Postgresql,我想创建一个应用程序,用户在该应用程序中登录他们的帐户,我有此逻辑,并且遇到此错误System.NullReferenceException:'对象引用未设置为对象的实例。' 为什么我收到此错误?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim Username = Username_TextBox1.Text
            Dim Password = Password_TextBox2.Text
            Dim dt As New DataTable
            Dim ds = New DataSet
            Dim i As Integer
            Try
                Using MyCon As New Odbc.OdbcConnection("Driver={PostgreSQL ANSI};database=YouthRecord;server=localhost;port=5432;uid=postgres;sslmode=disable;readonly=0;protocol=7.4;User ID=*****;password=*****;"),
                    cmd As New Odbc.OdbcCommand("SELECT * FROM YouthApp_tableuser WHERE `username` " & "='" & Username & "' and " & "password='" & Password & "'", MyCon)

                    If ds.Tables("YouthApp_tableuser").Rows.Count > 0 Then
                        For i = 0 To ds.Tables("YouthApp_tableuser").Rows.Count - 1
                            Username = ds.Tables("YouthApp_tableuser").Rows(i).Item(1).ToString
                            Password = ds.Tables("YouthApp_tableuser").Rows(i).Item(2).ToString
                        Next
                        If (Username = Username_TextBox1.Text And Password = Password_TextBox2.Text) Then
                            Username = ""
                            Password = ""

                            MainForm.Show()
                            Me.Hide()
                        Else
                            MsgBox("Invalid User! Please Enter Correct User Name and Password.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Invalid!")
                            Username = ""
                            Password = ""
                        End If
                    End If

                    MyCon.Open()
                    dt.Load(cmd.ExecuteReader)
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Catch ex As Exception

        End Try

这是错误

在此处输入图片说明

更新

Try
   Using MyCon As New Odbc.OdbcConnection("Driver={PostgreSQL ANSI};database=YouthRecord;server=localhost;port=5432;uid=******;sslmode=disable;readonly=0;protocol=7.4;User ID=*****;password=*****;"),
   cmd As New Odbc.OdbcCommand("SELECT id, username, password FROM ""YouthApp_tableuser"" where username='" & Username & "' and password='" & Password & "' ", MyCon)
   MyCon.Open()
   dt.Load(cmd.ExecuteReader)
       If ds.Tables("YouthApp_tableuser").Rows.Count > 0 Then
             If (Username = Username_TextBox1.Text And Password = Password_TextBox2.Text) Then
                 Username = ""
                 Password = ""

                 MainForm.Show()
                 Me.Hide()
               Else
                 MsgBox("Invalid User! Please Enter Correct User Name and Password.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Invalid!")
                  Username = ""
                  Password = ""
                End If
       End If

   End Using
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

我犯了同样的错误

在此处输入图片说明

马文
 Try
                Using MyCon As New Odbc.OdbcConnection("Driver={PostgreSQL ANSI};database=YouthRecord;server=localhost;port=5432;uid=*****;sslmode=disable;readonly=0;protocol=7.4;User ID=*****;password=*****;"),
                    cmd As New Odbc.OdbcCommand("SELECT id, username, password FROM ""YouthApp_tableuser"" where username='" & Username & "' and password='" & Password & "' ", MyCon)
                    MyCon.Open()
                    dt.Load(cmd.ExecuteReader)

                    If dt.Rows.Count > 0 Then
                        If (Username = Username_TextBox1.Text And Password = Password_TextBox2.Text) Then
                            Username = ""
                            Password = ""

                            MainForm.Show()
                            Me.Hide()

                        End If
                    Else
                        MsgBox("Invalid User! Please Enter Correct User Name and Password.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Invalid!")
                        Username = ""
                        Password = ""
                    End If


                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章