从数据库中获取特定值以验证数据

时间:2014-02-21 15:51:34

标签: sql vb.net winforms ms-access

我想获取用户输入的值,并根据数据库中的已知值对其进行验证。

我正在使用VB.net(VS 2010)作为前端winform并将数据转储到MSAccess中。我有一个包含最小重量和最大重量等值的表格,用户将输入一个重量,它需要在表格中的最小重量和最大重量值之间。

我已经创建了一个与我需要的数据库的开放连接,我想我只是不知道enogh VB.net和SQL让它做我想要的。

我想让用户输入具有最小重量和最大重量的表的主键,然后使用它来输入最小最大重量,然后检查用户输入的重量与最小最大重量。

这是所有代码;唯一真正相关的代码是底部的验证按钮点击。我只是认为这可能会提供更多背景信息。正确方向上的任何一点都会有所帮助,即使它为谷歌提供了更好的关键词。谢谢!

Imports System.Data.OleDb

Public Class Form1
Dim dbInsert As New OleDb.OleDbCommand
Dim dbConnect As New OleDb.OleDbConnection
Dim Line As String = Environment.NewLine
Dim Job As VariantType

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Try
        dbConnect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\crabara\Desktop\Project Alpha 3\MDB.accdb;Persist Security Info=False;"
        dbConnect.Open()
    Catch ex As Exception
        MessageBox.Show(ex.Message + Line + "Main Database Not Found" + Line + "Check form_AccessMaintenance source code" + Line + "Database Path", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Me.Close()
    End Try

End Sub

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim Part As String, Job As String, Emp As String, Weight As String, Oven As String

    Part = txtPart.Text
    Job = txtJob.Text
    Emp = txtEmp.Text
    Weight = txtWeight.Text
    Oven = txtOven.Text

    If ((Job.StartsWith("JH") And Job.Length = 10) Or Job.Equals("MT")) = False Then
        MsgBox("Please input the correct Job Number.")
        txtJob.Clear()
        txtJob.Focus()
        Exit Sub
    ElseIf Part.Length = 0 Then
        MsgBox("Please input the correct Part Number.")
        txtPart.Clear()
        txtPart.Focus()
        Exit Sub
    ElseIf Emp.Length = 0 Then
        MsgBox("Please input the correct Employee Number.")
        txtEmp.Clear()
        txtEmp.Focus()
        Exit Sub
    ElseIf Weight.Length = 0 Then
        MsgBox("Please input the correct Weight.")
        txtWeight.Clear()
        txtWeight.Focus()
        Exit Sub
    ElseIf Oven.Length = 0 Then
        MsgBox("Please input the correct Oven Number.")
        txtOven.Clear()
        txtOven.Focus()
        Exit Sub
    End If

    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "Part"
    dbInsert.Parameters.Item("Part").Value = txtPart.Text
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "Job"
    dbInsert.Parameters.Item("Job").Value = txtJob.Text
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "Emp"
    dbInsert.Parameters.Item("Emp").Value = txtEmp.Text
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "Weight"
    dbInsert.Parameters.Item("Weight").Value = txtWeight.Text
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "Oven"
    dbInsert.Parameters.Item("Oven").Value = txtOven.Text
    Try
        dbInsert.CommandText = "INSERT INTO Foam(Part,Job,Emp,Weight,Oven) VALUES(txtPart.Text, txtJob.Text, txtEmp.Text, txtWeight.Text, txtOven.Text);"
        dbInsert.CommandType = CommandType.Text
        dbInsert.Connection = dbConnect
        dbInsert.ExecuteNonQuery()
        MessageBox.Show("Data has been successfully submitted" + Line + txtPart.Text)
        txtPart.Clear()
        txtJob.Clear()
        txtEmp.Clear()
        txtWeight.Clear()
        txtOven.Clear()
        Control.MousePosition.Equals(txtPart)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
Private Sub txtJob_GotFocus(sender As Object, e As System.EventArgs)
    txtJob.Clear()
End Sub
Private Sub txtJob_LostFocus(sender As Object, e As System.EventArgs)
    Dim Job2 As String

    Job2 = txtJob.Text

    If txtJob.Text.Length = 8 Then
        txtJob.Text = "JH" + Job2
    End If
End Sub
Private Sub btnValidate_Click_1(sender As System.Object, e As System.EventArgs) Handles btnValidate.Click
    Dim Pcr As Integer
    Pcr = txtPcr.Text

    Try

        dbInsert.CommandText = "SELECT MoldVinylWeightMin FROM PROCESS_INFO where PCRNumber= '" & txtPcr.Text & "'"""
        'How can I get this data I've selected into a variable I can work with, also not sure if the above command actually works also this only gives me the min I need the max too
        dbInsert.CommandType = CommandType.Text
        dbInsert.Connection = dbConnect
        dbInsert.ExecuteNonQuery()
        MessageBox.Show("Data has been successfully submitted" + Line + txtPart.Text)
    Catch ex As Exception
    End Try
  End Sub
End Class

1 个答案:

答案 0 :(得分:1)

我认为以下代码可以帮助您

    Dim table = New DataTable()
    Dim adp As New System.Data.OleDb.OleDbDataAdapter("SELECT MoldVinylWeightMin FROM PROCESS_INFO where PCRNumber= '" & txtPcr.Text & "'", dbConnect)
    adp.Fill(table)
    If table.Rows.Count <> 0 Then
        If table.Rows(0).Item("MoldVinylWeightMin") <> YourField Then
                Display the message
        End If
    Else
        No record found
    End If

使用此块,您可以从数据库中获取记录并应用验证。