2014-02-21 49 views
0

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

我使用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

我想下面的代码将帮助您

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 

使用该块,你可以从数据库中的记录并应用验证。

+0

这是比较字段吗?理想情况下,我想要做的是将moldvinylweightmin放到一个局部变量中,因为myfield将是一个局部变量,并且不可避免地存储在另一个表中。 – Crabara