2009-12-11 93 views
2

我有一個小問題。我不知道如何正確使用VB.NET中的DataSet。具有多個表的數據集

在Visual Studio 2008中,我創建了一個名爲Network的DataSet。對於DataSet,我從我的數據庫tServer和tClient中取出兩張表。 tClient有一個外鍵引用tServer中的ID。

創建DataSet後,我發現一個名爲NetworkTableAdapter的新名稱空間,其中包含tServer,tClient和AdapterManager的適配器。還有一個叫做Network的新類,它是DataSet,包含tServer和tClient的DataTables。

但是我怎樣才能用這些數據填充這些DataSet並訪問它呢?該適配器只有GetData()和Fill()方法,它們填充DataTable,但我想填充DataSet。

對不起,我的英語不好,我希望有人瞭解我的問題,可以幫助我。 :)

託本

+0

我不擔心你的英語;這是完全可以理解的。但更重要的是,你的問題是詳細和具體的,你接受所有問題的答案。這比很多人管理的要好。 – 2009-12-11 00:45:50

回答

1

至於即時知道你不能自動地填寫完整的數據集這種方式。你必須填充它中的每個表。爲此,如果您使用可視化數據集,只需右鍵單擊表適配器並添加查詢。從這裏您可以直接將SQL添加到表格適配器中,或使用存儲過程。

對於select的示例,select必須與數據集中的列匹配。

因此,如果我們有一個名爲CustomersTable數據表,我們已經添加了一個名爲「GetNewCustomers(),你可以做CustomersTable dtCustomers = adapter.GetNewCustomers FUNC()

看到一個更好的說明和教程開始#1和#2 here

數據集是.NET 2.0不過,我會建議可能讓與他們交手,然後看着LINQ2Entities映射數據庫。

希望這有助於。

+0

謝謝。我知道,該數據集是.NET 2.0,但我必須使用.NET 2.0。 :)但是當我必須單獨填充每個表時,我不明白這個生成的數據集類的好處。除此之外,我不知道如何填寫這些表格。 – Torben 2009-12-11 08:16:01

+0

數據集表是強類型的,可以直接綁定到所有的asp.net數據控件。 請參閱我的原始答案中的鏈接,以便更好地描述如何填充表格。從第一個教程開始。 – Jammin 2009-12-11 09:36:19

0

我們從存儲過程填充返回多個結果集(表)的數據集。 我們的巫師在俄克拉荷馬州機構做到了這一點,我只是做了樣板表格。

  1. SP返回3個結果集:

    CREATE PROCEDURE dbo.GetAllLists 
    AS 
    
    SELECT UserKey, FirstName, MiddleInitial, LastName, Suffix, EmailAddress, PeopleSoftID, ChangeDate, ChangeUser 
    FROM USERS 
    ORDER BY LastName, FirstName 
    
    SELECT a.UserKey, a.DisciplineID, b.Description AS 'DisciplineDescription' 
    FROM USER_DISCIPLINES a 
    INNER JOIN Discipline b ON a.DisciplineID = b.DisciplineID 
    
    SELECT a.UserKey, a.ApplicationID, b.ApplicationName, a.UserName, a.UserSID, a.UserDomain, a.Active, a.Integrated, a.PositionID, a.ChangeUser, a.ChangeDate 
    FROM USER_MAPPINGS a 
    INNER JOIN APPLICATION_TABLE b ON a.ApplicationID = b.ApplicationID 
    
  2. clsDataManagement程序 - 部分過程,做的工作

    Public Shared Function GetInfoInDataset(ByVal StoredProcedureName As String, ByVal DatabaseID As Integer, ByVal ParamArray SQLParams() As SqlClient.SqlParameter) As DataSet 
        Dim dsTemp As New DataSet 
        Dim cmdSQL As SqlClient.SqlCommand = Nothing 
        Dim conSQL As SqlClient.SqlConnection = Nothing 
        Dim daSQL As SqlClient.SqlDataAdapter = Nothing 
        Try 
         conSQL = New SqlClient.SqlConnection(BuildConnection(DatabaseID)) 
         conSQL.Open() 
         cmdSQL = New SqlClient.SqlCommand(StoredProcedureName, conSQL) 
         cmdSQL.CommandType = CommandType.StoredProcedure 
         cmdSQL.CommandTimeout = 60 
         If Not IsNothing(SQLParams) Then 
          For Each p As SqlClient.SqlParameter In SQLParams 
           If Not IsNothing(p) Then 
            If IsNothing(p.Value) Then p.Value = DBNull.Value 
            cmdSQL.Parameters.Add(p) 
           End If 
          Next 
         End If 
         daSQL = New SqlClient.SqlDataAdapter(cmdSQL) 
         daSQL.Fill(dsTemp) 
         Return dsTemp 
        Catch sqlEx As SqlClient.SqlException 
         'MessageBox.Show("A SQL database error occurred preventing PHOCIS from performing the intended function." + Environment.NewLine + "Function was: " + StoredProcedureName + "." + Environment.NewLine + "Error was: " + sqlEx.Message & Environment.NewLine & "Please contact support for assistance.", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
         MessageBox.Show(sqlEx.Message, "Database Error", MessageBoxButtons.OK) 
         'MsgBox("There was an error retrieving the data using procedure " & StoredProcedureName & Environment.NewLine & sqlEx.Message, MsgBoxStyle.OkOnly, "Error Loading Data") 
         Return Nothing 
        Catch ex As Exception 
         'MessageBox.Show("An error occurred preventing PHOCIS from performing the intended function." + Environment.NewLine + "Function was: " + StoredProcedureName + "." + Environment.NewLine + "Error was: " + ex.Message & Environment.NewLine & "Please contact support for assistance.", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
         MessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK) 
         'MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error Loading Data") 
         Return Nothing 
        Finally 
         dsTemp = Nothing 
         If Not IsNothing(conSQL) Then conSQL.Close() 
         cmdSQL = Nothing 
         conSQL = Nothing 
         daSQL = Nothing 
        End Try 
    End Function 
    
    ' Missing lots of overloaded procs with name GetInfoInDataset() 
    
  3. clsData調用過程:

    ''' <returns>3 tables, 
    ''' 1) OSDH_Apps.USERS 
    ''' 2) OSDH_Apps.USER_DISCIPLINES 
    ''' 3) OSDH_Apps.USER_MAPPINGS </returns> 
    Public Function GetAllUserLists() As DataSet 
        Try 
         Return GetInfoInDataset("GetAllLists") 
        Catch ex As Exception 
         Return Nothing 
        Finally   
        End Try 
    End Function 
    
    frmMain.loadstuff() 
    
    Dim userData As DataSet = Nothing 
    Dim a As New clsData 
    
    Try 
        userData = a.GetAllUserLists