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 =@P1"
        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 =@P1"
        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
4

1 に答える 1

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 =@P1"
        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
于 2013-10-21T21:35:53.513 に答える