0

任何想法如何编写代码以使用访问数据库在 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

谁能给我一些想法

4

2 回答 2

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
于 2013-01-24T02:27:39.120 回答
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)

抱歉,如果使用记事本编辑器出现任何错误,此基本逻辑将与数据库进行比较

于 2013-01-23T03:34:02.583 回答