2011-01-14 51 views
1

一个形式,我可以表适配器很容易地做到这一点,但我似乎无法使用表适配器连接字符串中的变量,或指定“填充”之前使用的连接字符串。有没有办法在不使用任何绑定的情况下填充表单?如何填充,而不使用表适配器

目前我用这个方法 - 用于填充一个列表框搜索表单,并在双击动作,我有这样的:

Private Sub lstResults_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles lstResults.CellMouseDoubleClick 
    'Fills Main Form 
    Dim firstcellvalue As String = lstResults.Rows(e.RowIndex).Cells(0).Value.ToString() 
    frm_IMT.intPendingID = CInt(lstResults.Rows(e.RowIndex).Cells(0).Value.ToString()) 
    frm_IMT.Show() 

然后加载窗体时:

我。 PendingTableAdapter.Fill(Me.Phone_memo_backendDataSet.Tables(“Pending”),intPendingID)

回答

2

您是否可以更具体地确定您想要达到的目标?当你说你似乎无法使用变量来构造连接字符串时,你的意思是,为了连接到后端并检索数据?

因为我不能肯定它是什么,你正在尝试做的,我会做出一些猜测。它看起来像你试图在一个表单上填充某种列表(比如说一个dataGridView)。

然后,您可以允许用户通过双击行中的任何单元格来选择dataGridview中的项目,从而为包含挂起电话备忘录的表中的记录检索主键ID。

然后使用此ID填充TableAdapter,其中包含PendingID作为主键(从而返回单个记录)或外键(因此将0返回给多个记录)的表“Pending”中的所有记录。 。

因为我们不知道你的结果都应该结束了(是否表适配器包含一个记录,或可能有多少?),它是很难回答你的问题。

就个人而言,我会考虑写返回包含要用来填充表单中的信息的DataTable的功能。在这个例子中,函数返回一个dataTable,其中包含每个状态的记录以及相关的数据。假设这个功能,而接下来,被命名为“MyDataFactory”一类的成员:

'Shared Keyword makes this function Available without instantiating the containing class: 
Public Shared Function StatesDataTable(ByVal StateID As Integer) As DataTable 

    'Set up your SQL Statement to execute against your back end: 
    Dim SQL As String = _ 
    "SELECT StateID, State, StateName, HUDStateID " & _ 
    "FROM tblState " & _ 
    "ORDER BY State" 

    'Initialize a DataTable: 
    Dim dt As New DataTable 

    'The Using Keyword is elegant in that it handles bothersome Disposal tasks 
    'for unmanaged objects such as Database Connections: 
    Using cn As New OleDb.OleDbConnection(My.Settings.CreateTHisConnection) 
     Using cmd As New OleDb.OleDbCommand(SQL, cn) 

      'Open the Connection: 
      cn.Open() 

      'Retreive the Data into a DataReader: 
      Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader 

      'Load the contents of the Reader into your datatable: 
      dt.Load(dr) 

      'Clean Up: 
      dr.Close() 
      cn.Close() 

     End Using ' cmd 
    End Using ' cn 

    'Return the DataTable to calling code: 
    Return dt 

End Function ' StatesDataTable 

如果我用这个数据表来填充我与STATEID(局促作为Value成员主要形式的列表例如ListBox)和StateName(Displayed,作为所述ListBox的DisplayMember)。当用户点击列表框中的特定状态时,可以从Valuemember属性中检索StateID,并将其作为参数传递给另一个函数,可能会返回与该状态有关的Counties的数据表:

'Shared Keyword makes this function Available without instantiating the containing class: 
Public Shared Function CountiesDataTable(ByVal CountyID As Integer) As DataTable 

    'Set up your SQL Statement to execute against your back end: 
    Dim SQL As String = _ 
    "SELECT CountyID, StateID, CountyName " & _ 
    "FROM tblCounties " & _ 
    "WHERE CountyID = @CountyID " & _ 
    "ORDER BY CountyName" 

    'Initialize a DataTable: 
    Dim dt As New DataTable 

    'The Using Keyword is elegant in that it handles bothersome Disposal tasks 
    'for unmanaged objects such as Database Connections: 
    Using cn As New OleDb.OleDbConnection(My.Settings.CreateTHisConnection) 
     Using cmd As New OleDb.OleDbCommand(SQL, cn) 

      'Add a parameter which limits the result set to counties in the specified state: 
      cmd.Parameters.AddWithValue("@CountyID", CountyID) 
      'Open the Connection: 
      cn.Open() 

      'Retreive the Data into a DataReader: 
      Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader 

      'Load the contents of the Reader into your datatable: 
      dt.Load(dr) 

      'Clean Up: 
      dr.Close() 
      cn.Close() 

     End Using ' cmd 
    End Using ' cn 

    'Return the DataTable to calling code: 
    Return dt 

End Function ' CountiesDataTable 

然后可以使用此函数的结果输出加载到弹出窗体或某种其他机制以显示与选定状态关联的县。

不知道是否有任何这是你所追求的,但没有一点点多了去了,我已尽我所能。 。 。

我在这里的方法是ADO.NET的土地一点点非正统的,但我喜欢它,因为它让我我从后端拉动的隐性控制。这并不是说对于许多应用,全球范围内这样可“共享”功能会派上用场 - 以“按需” retreive国家(或县)的数据表,我只是做到以下几点:

Dim dtStates As DataTable = MyDataFactory.StatesDataTable 
    Dim dtCounties As DataTable = MyDataFactory.Counties(ByVal StateID As Integer) 

不管怎么说,这有助于。如果你可以澄清你正在尝试做什么,或者找到这个intereesting,并且想要更多的信息,请随时跟进。

相关问题