【问题标题】:How to relate a row to a specific SQL column from Visual Basic?如何将一行与 Visual Basic 中的特定 SQL 列相关联?
【发布时间】:2019-11-25 21:22:06
【问题描述】:

我正在用 Visual Basic 模拟 ATM。我在 SQL 中有一个名为 Authentication 的表。该表包含两列:NUM_CARD 列和 PIN_CARD 列。在插入卡 ID 时,我需要将行 (0) 列 1、行 (1) 列 (1)、行 (2) 列 (1) 等与其他行匹配。我怎样才能做到这一点?提前致谢。

DBConnection 类如下:

Imports System
Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class clsDBConnection

'Class variables'
Public cn As SqlConnection
Public cmd As SqlCommand
Public dr As SqlDataReader

'Constructor of the Connection class that creates the connection'
Sub New()
    Try
        cn = New SqlConnection("Data Source=JOVALLES-PC\SQLSERVEREX;Initial Catalog=SigmasBank;Integrated Security=True")
        cn.Open()

    Catch ex As Exception
        MsgBox("Error connecting due to:: " + ex.ToString)
    End Try
End Sub


'Returns true or false if the record exists or not in the database'
Function validationAutentication_p1(ByVal NUM_CARD As String) As Boolean
    Dim result As Boolean = False
    Try
        cmd = New SqlCommand("Select * from Autentication where NUM_CARD='" & NUM_CARD & "'", cn)
        dr = cmd.ExecuteReader


        If dr.HasRows Then
            result = True
        End If
        dr.Close()
    Catch ex As Exception
        MsgBox("Error in the procedure: " + ex.ToString)
    End Try
    Return result
End Function

Function validationAutentication_p2(ByVal PIN_CARD As String) As Boolean
    Dim result As Boolean = False
    Try
        cmd = New SqlCommand("Select * from Autentication where PIN_CARD='" & PIN_CARD & "'", cn)
        dr = cmd.ExecuteReader

        If dr.HasRows Then
            result = True
        End If
        dr.Close()
    Catch ex As Exception
        MsgBox("Error in the procedure: " + ex.ToString)
    End Try
    Return result
End Function

End Class

插入卡 ID 表格:

Public Class FRM_InsertCardID

Public conn As New clsDBConnection

Private Sub BTN_Ok_Click(sender As Object, e As EventArgs) Handles BTN_Ok.Click


    If TXB_CardID.Text.Length = 0 Then
        MsgBox("Please fill in field.")


    ElseIf TXB_CardID.Text.Length > 0 And TXB_CardID.Text.Length < 16 Then

        MsgBox("Your Card ID must be 16 digits.")

    ElseIf conn.validationAutentication_p1(TXB_CardID.Text) = False Then


        MsgBox("The Card ID doesn't exist.")


    Else
        FRM_PIN.Show()
        Me.Hide()
        TXB_CardID.Text = ""

    End If



End Sub

插入 PIN 表单:

Public Class FRM_PIN

Public conn As New clsDBConnection


Private Sub BTN_Ok_Click(sender As Object, e As EventArgs) Handles BTN_Ok.Click


    If TXB_PIN.Text.Length = 0 Then

        MsgBox("Please fill in field.")

    ElseIf TXB_PIN.Text.Length > 0 And TXB_PIN.Text.Length < 4 Then

        MsgBox("Your PIN must be 4 digits.")

    ElseIf conn.validationAutentication_p2(TXB_PIN.Text) = False Then


        MsgBox("Incorrect PIN Please try again.")


    Else

        FRM_Transaction.Show()
        Me.Hide()
        TXB_PIN.Text = ""


    End If


End Sub

【问题讨论】:

  • 注意:始终使用参数以避免sql注入和格式错误。
  • 该示例没有提供任何见解,如果记录存在,您只会返回两个布尔值。除非已加密,否则您永远不会将这些信息存储在数据库中。这将是工作的最大份额。处理加密后,您只需从两个字段都存在记录的函数返回一个布尔值。
  • 我稍后会进行加密。感谢您的注意。我现在正在测试的是存储在数据库中的信息。问题如下:当我输入数据库中的卡ID时,输入PIN时,我可以通过输入PIN 1234或PIN 5678毫无问题地访问并且存在问题。我需要卡 ID 2222222222222222 的 PIN 码仅为 1234。问候。
  • Select * from Autentication where PIN_CARD= @param1 AND NUM_CARD=@param2 No?
  • 你在使用Imports System.Data.Sql的具体哪些类?

标签: sql-server vb.net validation


【解决方案1】:

不确定错字是否会导致问题? -- 认证

“我在 SQL 中有一个名为 Authentication 的表。” " cmd = New SqlCommand("Select * from Autentication where PIN_CARD='" & PIN_CARD & "'", cn)"

【讨论】:

  • 我翻译的不好。但问题不在于排版。
【解决方案2】:

让我们从clsDBConnection 开始。您不需要导入System。默认情况下就在那里。 System.Data.Sql 从未使用过。也摆脱它。

有人会认为这个类是关于数据库连接的。它不是。它包含用于身份验证的代码。所以重命名;类似于 DataAccess。

永远不要建立连接、命令和阅读器类级别的变量。这些数据库对象需要关闭和处置,因此类不是声明它们的地方。它们必须是局部变量,是使用它们的方法的局部变量。

在使用之前,永远不要打开连接。理想情况下,调用 .Execute... 方法之前的行。确保它也尽快关闭和处置。您的代码会打开一个连接,然后让它随风飘扬。

您可以在 DataAccess 类中执行的操作是将连接字符串设置为 Private 类级别的变量。 Private cnString as String = ...

我根本看不出您在哪里需要自定义构造函数。只需摆脱Sub New() 我已经在你的类中创建了两个方法Shared 这个数据由类的所有实例共享,你没有声明类的实例来使用这些方法。您可以通过引用类和方法的名称来调用共享方法。 conString 也是Shared,因为它被共享方法使用。

我决定 pin 号不一定是唯一的,因为它们最多只能达到 9999。这就是为什么我在第二种方法中使用了 2 个参数。

注意: 我不得不猜测 SqlParameters 的数据类型和字段大小。检查您的数据库并相应地调整代码。

Public Class FRM_InsertCardID

    Private Sub BTN_Ok_Click(sender As Object, e As EventArgs) Handles BTN_Ok.Click
        If TXB_CardID.Text.Length = 0 Then
            MsgBox("Please fill in field.")
            'Don't give the user any information on what a proper card ID consists of
            Return
        End If

        If DataAccess.validationAutentication_p1(TXB_CardID.Text) = False Then
            MsgBox("The Card ID doesn't exist.")
        Else
            FRM_PIN.Show()
            'It appears you are using the default instance of FRM_PIN
            FRM_PIM.CardID = TXB_CardID.Text
            TXB_CardID.Text = ""
            Me.Hide()
        End If
    End Sub

End Class

Public Class FRM_PIN

    Friend CardID As String

    Private Sub BTN_Ok_Click(sender As Object, e As EventArgs) Handles BTN_Ok.Click

        If TXB_PIN.Text.Length = 0 Then
            MsgBox("Please fill in field.")
            Return 'Exits the sub
        End If

        If DataAccess.validationAutentication_p2(CardID, TXB_PIN.Text) = False Then
            MsgBox("Incorrect PIN Please try again.")
        Else
            TXB_PIN.Text = ""
            FRM_Transaction.Show()
            Me.Hide()
        End If
    End Sub

End Class
Public Class DataAccess

    Private Shared conString As String = "Data Source=JOVALLES-PC\SQLSERVEREX;Initial Catalog=SigmasBank;Integrated Security=True"

    Public Shared Function validationAutentication_p1(ByVal NUM_CARD As String) As Boolean
        Dim result = False
        Using cn As New SqlConnection(conString),
                cmd As New SqlCommand("Select * from Autentication where NUM_CARD= @NumCARD;", cn)
            cmd.Parameters.Add("@NumCard", SqlDbType.VarChar, 16).Value = NUM_CARD
            cn.Open()
            Using dr = cmd.ExecuteReader
                If dr.HasRows Then
                    result = True
                End If
            End Using
        End Using
        Return result
    End Function

    Public Shared Function validationAutentication_p2(ByVal CardID As String, ByVal PIN_CARD As String) As Boolean
        Dim result = False
        Using cn As New SqlConnection(conString),
                cmd As New SqlCommand("Select * From Autentication where NUM_CARD = @NumCard AND PIN_CARD=@PinCard;", cn)
            cmd.Parameters.Add("@NumCard", SqlDbType.VarChar, 100).Value = CardID
            cmd.Parameters.Add("@PinCard", SqlDbType.VarChar, 4).Value = PIN_CARD
            cn.Open()
            Using dr = cmd.ExecuteReader()
                If dr.HasRows Then
                    result = True
                End If
            End Using
        End Using
        Return result
    End Function

End Class

【讨论】:

  • 我收到以下错误:System.Data.SqlClient.Exception: '参数化查询'(@NUM_CARD varchar (16),@PIN_CARD varchar(4)) Select * From' 需要参数' @NUM_CARD',未提供。'
  • 你使用anydesk吗?请让我知道你是否可以连接到我的电脑。谢谢。
  • 您是否在 FRM_PIN 上设置了 CardID 的值?仔细检查FRM_PIN.Show()FRM_PIM.CardID = TXB_CardID.TextTXB_CardID.Text = ""Me.Hide()
  • 那将是另一种形式。
  • 是的。您将表单级别变量添加到 FRM_PIN Friend CardID As String 然后在 BTN_Ok_Click 事件中使用此行 FRM_PIM.CardID = TXB_CardID.Text 从 FRM_InsertCardID 设置属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 2016-01-08
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 2016-04-05
相关资源
最近更新 更多