2013-01-23 55 views
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 

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

回答

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 [email protected]" 
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(斌值)

對不起,如果任何錯誤使用記事本對於編輯器,這個基本邏輯與數據庫比較

+0

我嘗試這段代碼,但我得到一個錯誤 –

0

This Small sample,在這裏下載源代碼: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 [email protected]" 
     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