对数据库中的值进行比较

时间:2013-01-23 01:24:46

标签: vb.net ms-access

任何想法如何使用访问数据库编写代码来比较vb.net中的实际值和用户输入值。我使用If语句但它运行不正常。例如

Table A
type=APW
Bin=1
actual value = 36.00

user input 1
type=APW
Bin=1
value=39.00
status=fail

user input 
type=APW
Bin=3
value=36.00
status=non

任何人都可以给我一些想法

2 个答案:

答案 0 :(得分:0)

使用查询例如

Private Sub Compare2Value(ByVal 1Value As String,ByVal 2Value as String)
dim cmd as new  SqlCommand
dim ds as new dataset
cmd.Connection=GetConnection()
cmd.CommandText="SELECT bin,value FROM TableA WHERE type=@type"
cmd.Parameters.Add("@type",SQLDbType.Varchar,5)
cmd.Parameters("@type").Value=TypeText
cmd.ExecuteReader()
dim da as new SqlDataAdpter(cmd)
da.Fill(ds,"dsname")
If ds.Tables(0).Rows()("bin")=1Value  and  ds.Tables(0).Rows()("value")=2Value Then 
   Status ="OK"
else
   Status ="FAIL"
end If
End Sub

如何使用:Compare2Value(Bin,Value)

抱歉如果有任何错误使用记事本编辑器,这个基本逻辑比较数据库

答案 1 :(得分:0)

此小样本,请在此处下载源代码:http://www.2shared.com/file/1DPE8mRo/Access.html

Imports System.Data.OleDb
Imports System.IO


Public Class Form1
Private Function GetCon() As String
    Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\IT\Documents\Demo.accdb" ' Your Location Database
End Function
Private Function Compare2Value(ByVal bin As Integer, ByVal val As Double) As String
    Dim status As String
    Dim con As New OleDbConnection(GetCon)
    Dim cmd As New OleDbCommand

    If con.State = ConnectionState.Closed Then

        cmd.Connection = con
        con.Open()
        cmd.CommandText = "SELECT BIN,VALUENAME FROM TYPE WHERE TYPEID=@ID"
        cmd.Parameters.Add("@ID", OleDbType.VarChar, 5)
        cmd.Parameters("@ID").Value = txttype.Text
        Dim da As New OleDbDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds, "type")
        If ds.Tables(0).Rows(0)("BIN") = bin And ds.Tables(0).Rows(0)("VALUENAME") = val Then
            status = "OK"
        Else
            status = "FAIL"
        End If
    End If
    Return status
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MsgBox(Compare2Value(txtbin.Text, txtvalue.Text))
End Sub
End Class