【问题标题】:Which one to use:Dataset or Datatable or Datareader用哪一个:Dataset and Datatable vs Datareader
【发布时间】:2011-08-17 10:18:24
【问题描述】:

我已经阅读了有关数据集、数据表和数据阅读器的文章,但我仍然对何时使用什么感到困惑?谁能帮我举个例子来理解哪一个在什么情况下是合适的?

【问题讨论】:

  • 那么你的问题是什么?
  • 从你的数据来看,问题不是我的……

标签: datatable dataset datareader


【解决方案1】:

DataTable 是用于存储单个表的列和行数据的对象。

    Dim dt As New Data.DataTable
    dt.Columns.Add("ColumnA", GetType(String))
    dt.Columns.Add("ColumnB", GetType(Int32))
    dt.Rows.Add("TestData1", 1)
    dt.Rows.Add("TestData2", 2)

    For Each dr As Data.DataRow In dt.Rows
        Response.Write(String.Format("{0}, {1}", dr.Item(0), dr.Item(0)))
    Next

Datareader 是用于从数据库中一次读取一行的对象。

    Using oConn As New Data.SqlClient.SqlConnection
        Dim oCmd = oConn.CreateCommand
        Dim oRead = oCmd.ExecuteReader
        While oRead.Read
            Response.Write(oRead.Item(0).ToString)
        End While
    End Using

数据集是数据表的集合。使用数据集,您还可以存储父表和子表之间的关系和约束。您基本上可以使用数据集在内存中创建整个关系数据库。可以使用代码创建数据集,也可以使用 Visual Studio 中的数据集编辑器创建数据集。如果您使用 Visual Studio(XSD 文件)创建它,则数据集将变为“类型化”,因此您可以通过名称而不是索引或文字来引用已编译代码中的列。

    Dim ds As New dsMain
    Dim drParent = ds.ParentTable.AddParentTableRow("1")
    Dim drChild = ds.ChildTable.AddChildTableRow(drParent, "Somedata")

    Response.Write(drChild.ChildData & drChild.ParentTableRow.ParentId.ToString)

还有 dsMain.XSD 的代码...

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="dsMain" targetNamespace="http://tempuri.org/dsMain.xsd" xmlns:mstns="http://tempuri.org/dsMain.xsd" xmlns="http://tempuri.org/dsMain.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:annotation>
    <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
      <DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
        <Connections />
        <Tables />
        <Sources />
      </DataSource>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="dsMain" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_UserDSName="dsMain" msprop:Generator_DataSetName="dsMain">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="ParentTable" msprop:Generator_TableClassName="ParentTableDataTable" msprop:Generator_TableVarName="tableParentTable" msprop:Generator_TablePropName="ParentTable" msprop:Generator_RowDeletingName="ParentTableRowDeleting" msprop:Generator_UserTableName="ParentTable" msprop:Generator_RowChangingName="ParentTableRowChanging" msprop:Generator_RowEvHandlerName="ParentTableRowChangeEventHandler" msprop:Generator_RowDeletedName="ParentTableRowDeleted" msprop:Generator_RowEvArgName="ParentTableRowChangeEvent" msprop:Generator_RowChangedName="ParentTableRowChanged" msprop:Generator_RowClassName="ParentTableRow">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ParentId" msprop:Generator_ColumnVarNameInTable="columnParentId" msprop:Generator_ColumnPropNameInRow="ParentId" msprop:Generator_ColumnPropNameInTable="ParentIdColumn" msprop:Generator_UserColumnName="ParentId" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="ChildTable" msprop:Generator_TableClassName="ChildTableDataTable" msprop:Generator_TableVarName="tableChildTable" msprop:Generator_TablePropName="ChildTable" msprop:Generator_RowDeletingName="ChildTableRowDeleting" msprop:Generator_UserTableName="ChildTable" msprop:Generator_RowChangingName="ChildTableRowChanging" msprop:Generator_RowEvHandlerName="ChildTableRowChangeEventHandler" msprop:Generator_RowDeletedName="ChildTableRowDeleted" msprop:Generator_RowEvArgName="ChildTableRowChangeEvent" msprop:Generator_RowChangedName="ChildTableRowChanged" msprop:Generator_RowClassName="ChildTableRow">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ParentId" msprop:Generator_ColumnVarNameInTable="columnParentId" msprop:Generator_ColumnPropNameInRow="ParentId" msprop:Generator_ColumnPropNameInTable="ParentIdColumn" msprop:Generator_UserColumnName="ParentId" type="xs:string" />
              <xs:element name="ChildData" msprop:Generator_ColumnVarNameInTable="columnChildData" msprop:Generator_ColumnPropNameInRow="ChildData" msprop:Generator_ColumnPropNameInTable="ChildDataColumn" msprop:Generator_UserColumnName="ChildData" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//mstns:ParentTable" />
      <xs:field xpath="mstns:ParentId" />
    </xs:unique>
    <xs:unique name="ChildTable_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//mstns:ChildTable" />
      <xs:field xpath="mstns:ParentId" />
    </xs:unique>
    <xs:keyref name="FK_ParentTable_ChildTable" refer="Constraint1" msprop:rel_Generator_UserChildTable="ChildTable" msprop:rel_Generator_ChildPropName="GetChildTableRows" msprop:rel_Generator_ParentPropName="ParentTableRow" msprop:rel_Generator_UserRelationName="FK_ParentTable_ChildTable" msprop:rel_Generator_RelationVarName="relationFK_ParentTable_ChildTable" msprop:rel_Generator_UserParentTable="ParentTable">
      <xs:selector xpath=".//mstns:ChildTable" />
      <xs:field xpath="mstns:ParentId" />
    </xs:keyref>
  </xs:element>
</xs:schema>

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 2014-02-11
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2013-12-21
    • 2012-02-08
    • 2012-12-01
    • 2011-06-21
    相关资源
    最近更新 更多