选择组合框数据后,使用数据库中的数据填写文本框

时间:2013-10-20 17:17:54

标签: vb.net

我正在处理一个应用程序,我有一个问题,我有一个组合框绑定到我的数据库中的表,当在组合框中选择数据时,我希望'textbox 1'和'textbox2'到从表中自动填写数据,这可能吗?例如,当我在组合框中选择'Richard'时,'Richards''Address1'和'Postcode'填写在文本框中。我有一个示例代码执行此操作但它似乎只适用于Int32,请参阅下面的内容。

Public Class Form1

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

2 个答案:

答案 0 :(得分:0)

1.请参阅下面的链接以从Sql server获取数据

2.创建一个将ID作为参数返回数据的函数 Dataadapter with dataset - sql sever

答案 1 :(得分:0)

不是100%确定使用Access,但理论上你要做的是拥有一个客户对象

您将拥有SQL存储过程

Select * from Customers where id=@ID
...

然后是一个班级

Public Class Customer
public property ID as Integer
Public property Name as string
public Property Surname as string
....
End Class

在您访问数据的代码中,您将拥有类似

的代码
Public Function GetCustomerDetailsByID (byval CustomerID as Integer) As Customer ' Or whatever you want to return it as
Dim myCust as New Customer
' Code to access SQL
' Call your SQL procedure called GetCustByID(CustID)

With myCust
 .Name = valueFromYourProcedure
End With

End Function

在您的客户端应用中,您将调用上述内容,然后将值分配给您的控件,即

Dim SingleCustomer as Customer = GetCustomerDetailsByID(1) ' i.e. value form your dropdown

txtBoxName.Text = SingleCustomer.Name

这是一个非常粗略的例子,并不是完全完整的,但应该让你知道如何解决这个问题。有很多方法可以做到这一点,每个人都有自己的方式,所以你可能需要做一些额外的研究,以适应它。

希望这有帮助