0

我正在开发一个应用程序,我有一个问题,我有一个绑定到我的数据库中的表的组合框,当在组合框中选择数据时,我希望自动填充“文本框 1”和“文本框 2”从表中取出数据,这可能吗?例如,当我在组合框中选择“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
4

2 回答 2

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

这是一个非常粗略的示例,并不完全完整,但应该让您对如何进行此操作有所了解。上面有很多方法可以做到,每个人都有自己的方法,所以你可能需要做一些额外的研究才能适应它。

希望这可以帮助

于 2013-10-21T20:04:41.073 回答
0

1.参考下面的链接从 Sql server 获取数据

2.创建一个函数,它将返回您的数据,其中 ID 作为参数 Dataadapter 和数据集 - sql sever

于 2013-10-20T19:08:22.467 回答