2009-12-11 52 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 
    
相关问题