具有多个表的DataSet

时间:2009-12-11 00:12:27

标签: .net ado.net

我有点问题。我不知道如何正确使用VB.NET中的DataSet。

在Visual Studio 2008中,我创建了一个名为Network的DataSet。对于DataSet,我从我的数据库tServer和tClient中获取了两个表。 tClient有一个引用tServer中ID的外键。

创建DataSet后,我找到了一个名为NetworkTableAdapter的新命名空间,其中包含tServer,tClient和AdapterManager的适配器。还有一个名为Network的新类,它是DataSet并包含tServer和tClient的DataTable。

但是如何用数据填充这些DataSet并访问它?适配器只有GetData()和Fill()方法,它们填充DataTable,但我想填充DataSet。

抱歉我的英语不好,我希望有人能理解我的问题,并能帮助我。 :)

Torben

2 个答案:

答案 0 :(得分:1)

据我所知,你不能以这种方式自动填充完整的数据集。您必须填充其中的每个表。为此,如果您使用可视化数据集,只需右键单击表适配器,然后添加查询即可。从这里,您可以直接将SQL添加到表适配器中,也可以使用存储过程。

对于select的示例,select必须与数据集中的列匹配。

因此,如果我们有一个名为CustomersTable的DataTable,并且我们添加了一个名为“GetNewCustomers()”的函数,那么您可以执行CustomersTable dtCustomers = adapter.GetNewCustomers()

查看更好的描述和教程从#1和#2 here

开始

然而,数据集是.NET 2.0。我建议也许可以掌握它们,然后查看LINQ2Entities来映射你的数据库。

希望这有帮助。

答案 1 :(得分: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