使用哪一个:Dataset 或 Datatable 或 Datareader

发布于 2024-11-29 21:47:58 字数 62 浏览 3 评论 0原文

我读过有关数据集、数据表和数据读取器的文章,但我仍然很困惑何时使用什么?谁能帮我举例说明哪个例子适合哪种情况?

I have read articles on dataset, datatable and datareader, but still I am in confuse when to use what? Can anyone help me with examples to understand which one is proper in which context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2024-12-06 21:47:59

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>

希望这有帮助。

A DataTable is an object used to store column and row data for a single table.

    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

A Datareader is an object used to read one row at a time from a database.

    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

A Dataset is a collection of DataTables. With a Dataset you can also store relationships and constraints between parent tables and child tables. You can essentially create an entire relational database in memory with a Dataset. Datasets can be either created with code or created using the dataset editor in Visual Studio. If you make it using Visual Studio (XSD file) the dataset becomes "typed", so you can refer to columns in compiled code by name instead of by index or literal.

    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)

And the code for 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>

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文