2013-10-21 163 views
0

我正在使用Visual Basic在Visual Studio 2013中的應用程序上工作,有些事情我稍微舉起來希望你可以幫助我... ..好吧,我有我的用戶用一個組合框綁定到訪問表中,它會很好地提取數據,對於'客戶名',但我想要做的是編寫代碼,以便在下面的組合框選擇中選擇'客戶名'時文本框自動填寫'客戶地址''客戶電話號碼''客戶帳戶名稱'我有一個工作代碼,但不幸的是它只適用於Int32我希望它能識別使用文本。誰能幫忙?我將與我以前的代碼跟進作爲援助當組合框選擇時填充文本框

感謝詹姆斯

Private Sub cmdGetByIdentifier_Click(sender As Object, e As EventArgs) Handles cmdGetByIdentifier.Click 
If Not String.IsNullOrWhiteSpace(txtIdentifier1.Text) Then 
    Dim Identifier As Int32 = 0 
    If Int32.TryParse(txtIdentifier1.Text, Identifier) Then 
     txtCompanyName1.Text = GetCustomerNameByIdentifier(Identifier) 
    Else 
     MessageBox.Show("'" & txtIdentifier1.Text & "' is not a valid integer.") 
    End If 
Else 
    MessageBox.Show("Must enter an identifier to get a company name.") 
End If 
End Sub 

Private Sub cmdGetCustomer_Click(sender As Object, e As EventArgs) Handles cmdGetCustomer.Click 
If Not String.IsNullOrWhiteSpace(txtIdentifier2.Text) Then 
    Dim Identifier As Int32 = 0 
    If Int32.TryParse(txtIdentifier2.Text, Identifier) Then 
     Dim Cust As Customer = GetCustomer(Identifier) 
     txtCompanyName2.Text = Cust.Name 
     txtContactName2.Text = Cust.ContactName 
    Else 
     MessageBox.Show("'" & txtIdentifier1.Text & "' is not a valid integer.") 
    End If 
Else 
    MessageBox.Show("Must enter an identifier to get a company name.") 
End If 

End Sub 
End Class 


Module DatabaseOperations 
    Public Function GetCustomerNameByIdentifier(ByVal Identifier As Int32) As String 
Dim CompanyName As String = "" 
Dim Builder As New OleDb.OleDbConnectionStringBuilder With {.Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")} 
Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString} 
    Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
     cmd.CommandText = "SELECT CompanyName FROM Customer WHERE Identifier [email protected]" 
     Dim NameParameter As New OleDb.OleDbParameter With {.DbType = DbType.Int32, .ParameterName = "P1", .Value = Identifier} 
     cmd.Parameters.Add(NameParameter) 
     cn.Open() 
     CompanyName = CStr(cmd.ExecuteScalar) 
    End Using 
End Using 
Return CompanyName 
End Function 
Public Function GetCustomer(ByVal Identifier As Int32) As Customer 
Dim Customer As New Customer 

Dim CompanyName As String = "" 
Dim Builder As New OleDb.OleDbConnectionStringBuilder With {.Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")} 
Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString} 
    Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
     cmd.CommandText = "SELECT Identifier, CompanyName,ContactName FROM Customer WHERE Identifier [email protected]" 
     Dim NameParameter As New OleDb.OleDbParameter With {.DbType = DbType.Int32, .ParameterName = "P1", .Value = Identifier} 
     cmd.Parameters.Add(NameParameter) 
     cn.Open() 
     Dim Reader As OleDb.OleDbDataReader = cmd.ExecuteReader 
     If Reader.HasRows Then 
      Reader.Read() 
      Customer.Identifier = Identifier 
      Customer.Name = Reader.GetString(1) 
      Customer.ContactName = Reader.GetString(2) 
     End If 
    End Using 
End Using 

Return Customer 

End Function 
End Module 


Public Class Customer 
Public Property Identifier As Int32 
Public Property Name As String 
Public Property ContactName As String 
Public Sub New() 
End Sub 
End Class 
+0

任何有關此問題的任何建議? – James

+0

創建另一個'GetCustomerNameByIdentifier',它接受一個字符串並傳遞字符串。然後將您的SQL更改爲您要查詢的任何字段。 – Steve

+0

林不知道我明白你的意思@Steve,你介意多解釋一下 – James

回答

0

像這樣:

Public Function GetCustomerNameByName(ByVal Name As String) As String 
Dim CompanyName As String = "" 
Dim Builder As New OleDb.OleDbConnectionStringBuilder With {.Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")} 
Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString} 
    Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
     cmd.CommandText = "SELECT CompanyName FROM Customer WHERE CustName [email protected]" 
     Dim NameParameter As New OleDb.OleDbParameter With {.DbType = DbType.String, .ParameterName = "P1", .Value = Name} 
     cmd.Parameters.Add(NameParameter) 
     cn.Open() 
     CompanyName = CStr(cmd.ExecuteScalar) 
    End Using 
End Using 
Return CompanyName 
End Function 
+0

它仍然有驗證代碼,雖然cmd.CommandText =「SELECT CompanyName FROM Customer WHERE CustName = @ P1」in the data operationcs code @ p1 is如果Int32?林不知道如果我認爲自己 – James

+0

對不起,我錯過了將參數類型更改爲字符串。 – Steve

+0

嗨@Steve,我剛剛運行該代碼,它似乎加載然後得到一個OleDbException未處理,並給出以下內容:在System.Data.dll 發生類型'System.Data.OleDb.OleDbException'未處理的異常附加信息:對於一個或多個所需參數沒有給出值。 - 建議它不能看到表或DB – James