2011-01-14 41 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,並且想要更多的信息,請隨時跟進。

相關問題