【问题标题】:Get specific value from database to validate data从数据库中获取特定值以验证数据
【发布时间】:2014-03-23 05:40:36
【问题描述】:

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

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

我已经创建了一个与我需要的数据库的开放连接,我想我只是不知道 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

【问题讨论】:

    标签: sql vb.net winforms ms-access


    【解决方案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
    

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

    【讨论】:

    • 这是比较字段吗?理想情况下,我想做的是把 Moldvinylweightmin 放入一个局部变量中,因为 myfield 将是一个局部变量并且不可避免地存储到另一个表中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2016-05-08
    • 2013-09-02
    • 2019-06-16
    • 2017-07-04
    相关资源
    最近更新 更多