[英]Grab row of data from SQL Server given value of one cell in a column in that row--Visual Basic
[英]How to relate a row to a specific SQL column from Visual Basic?
我正在用 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 中有一個名為 Authentication 的表。" " cmd = New SqlCommand("Select * from Autentication where PIN_CARD='" & PIN_CARD & "'", cn)"
讓我們從clsDBConnection
開始。 您不需要導入System
。 默認情況下就在那里。 System.Data.Sql
從未使用過。 也擺脫它。
有人會認為這個類是關於數據庫連接的。 它不是。 它包含用於身份驗證的代碼。 所以重命名; 類似數據訪問的東西。
永遠不要建立連接、命令和閱讀器類級別的變量。 這些數據庫對象需要關閉和處理,因此類不在聲明它們的地方。 它們需要是局部變量,是使用它們的方法的局部變量。
永遠,永遠不要在使用之前打開連接。 理想情況下,調用.Execute...
方法之前的行。 確保它也被關閉並盡快處理。 您的代碼打開一個連接,讓它隨風飄動。
您可以在 DataAccess 類中做的是使您的連接字符串成為 Private 類級別變量。 Private cnString as String = ...
我根本看不出你在哪里需要自定義構造函數。 去掉Sub New()
我已經在你的類中創建了 2 個方法Shared
這個數據由類的所有實例共享,你沒有聲明類的實例來使用這些方法。 您可以僅通過引用類和方法的名稱來調用共享方法。 conString 也是Shared
因為它被共享方法使用。
我決定引腳號不一定是唯一的,因為它們最多只能達到 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.