当前位置: 首页>>代码示例>>C#>>正文


C# DataTable.CreateDataReader方法代码示例

本文整理汇总了C#中DataTable.CreateDataReader方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.CreateDataReader方法的具体用法?C# DataTable.CreateDataReader怎么用?C# DataTable.CreateDataReader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataTable的用法示例。


在下文中一共展示了DataTable.CreateDataReader方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BuildDataReader

            public IDataReader BuildDataReader()
            {
                var authorizationSetDataTable = new DataTable();
                authorizationSetDataTable.Columns.Add(FieldName.SmallInt, typeof(Int16));
                authorizationSetDataTable.Columns.Add(FieldName.Int, typeof(Int32));
                authorizationSetDataTable.Columns.Add(FieldName.BigInt, typeof(Int64));
                authorizationSetDataTable.Columns.Add(FieldName.Guid, typeof(Guid));
                authorizationSetDataTable.Columns.Add(FieldName.Float, typeof(float));
                authorizationSetDataTable.Columns.Add(FieldName.Double, typeof(Double));
                authorizationSetDataTable.Columns.Add(FieldName.Decimal, typeof(Decimal));
                authorizationSetDataTable.Columns.Add(FieldName.DateTime, typeof(DateTime));
                authorizationSetDataTable.Columns.Add(FieldName.Byte, typeof(Byte));
                authorizationSetDataTable.Columns.Add(FieldName.Boolean, typeof(Boolean));
                authorizationSetDataTable.Columns.Add(FieldName.Something, typeof(DateTime));

                var authorizationSetDataRow = authorizationSetDataTable.NewRow();
                authorizationSetDataRow[FieldName.SmallInt] = 22;
                authorizationSetDataRow[FieldName.Int] = 6134;
                authorizationSetDataRow[FieldName.BigInt] = 61346154;
                authorizationSetDataRow[FieldName.Guid] = Guid.NewGuid();
                authorizationSetDataRow[FieldName.Float] = 642.61;
                authorizationSetDataRow[FieldName.Double] = 67164.64;
                authorizationSetDataRow[FieldName.Decimal] = 94341.61;
                authorizationSetDataRow[FieldName.DateTime] = DateTime.Now;
                authorizationSetDataRow[FieldName.Byte] = 0x12;
                authorizationSetDataRow[FieldName.Boolean] = true;
                authorizationSetDataRow[FieldName.Something] = DateTime.MaxValue;
                authorizationSetDataTable.Rows.Add(authorizationSetDataRow);

                return authorizationSetDataTable.CreateDataReader();
            }
开发者ID:ngadvmaster,项目名称:AutoMapper,代码行数:31,代码来源:DataReaderMapping.cs

示例2: BuildDataReaderWithNullableField

                public IDataReader BuildDataReaderWithNullableField()
                {
                    var table = new DataTable();

                    var col = table.Columns.Add(FieldName, typeof(int));
                    col.AllowDBNull = true;

                    var row1 = table.NewRow();
                    row1[FieldName] = FieldValue;
                    table.Rows.Add(row1);

                    var row2 = table.NewRow();
                    row2[FieldName] = DBNull.Value;
                    table.Rows.Add(row2);

                    return table.CreateDataReader();
                }
开发者ID:ngadvmaster,项目名称:AutoMapper,代码行数:17,代码来源:DataReaderMapping.cs

示例3: TestExceptionIfSchemaChanges

        public void TestExceptionIfSchemaChanges()
        {
            DataTable table = new DataTable();
            table.Columns.Add("col1");
            DataTableReader rdr = table.CreateDataReader();
            Assert.Equal(1, rdr.GetSchemaTable().Rows.Count);

            table.Columns[0].ColumnName = "newcol1";
            try
            {
                rdr.GetSchemaTable();
                Assert.False(true);
            }
            catch (InvalidOperationException)
            {
                // Never premise English.
                //Assert.Equal ("Schema of current DataTable '" + table.TableName + 
                //		"' in DataTableReader has changed, DataTableReader is invalid.", e.Message, "#1");
            }

            rdr = table.CreateDataReader();
            rdr.GetSchemaTable(); //no exception
            table.Columns.Add("col2");
            try
            {
                rdr.GetSchemaTable();
                Assert.False(true);
            }
            catch (InvalidOperationException)
            {
                // Never premise English.
                //Assert.Equal ("Schema of current DataTable '" + table.TableName + 
                //		"' in DataTableReader has changed, DataTableReader is invalid.", e.Message, "#1");
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:35,代码来源:DataTableReaderTest.cs

示例4: EnumeratorTest

        public void EnumeratorTest()
        {
            DataTable table = new DataTable();
            table.Columns.Add("col1", typeof(int));
            table.Rows.Add(new object[] { 0 });
            table.Rows.Add(new object[] { 1 });

            DataTableReader rdr = table.CreateDataReader();
            IEnumerator enmr = rdr.GetEnumerator();

            table.Rows.Add(new object[] { 2 });
            table.Rows.RemoveAt(0);

            //Test if the Enumerator is stable
            int i = 1;
            while (enmr.MoveNext())
            {
                DbDataRecord rec = (DbDataRecord)enmr.Current;
                Assert.Equal(i, rec.GetInt32(0));
                i++;
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:22,代码来源:DataTableReaderTest.cs

示例5: TestSchemaTable

        public void TestSchemaTable()
        {
            var ds = new DataSet();
            DataTable testTable = new DataTable("TestTable1");
            DataTable testTable1 = new DataTable();

            testTable.Namespace = "TableNamespace";

            testTable1.Columns.Add("col1", typeof(int));
            testTable1.Columns.Add("col2", typeof(int));
            ds.Tables.Add(testTable);
            ds.Tables.Add(testTable1);

            //create a col for standard datatype

            testTable.Columns.Add("col_string");
            testTable.Columns.Add("col_string_fixed");
            testTable.Columns["col_string_fixed"].MaxLength = 10;
            testTable.Columns.Add("col_int", typeof(int));
            testTable.Columns.Add("col_decimal", typeof(decimal));
            testTable.Columns.Add("col_datetime", typeof(DateTime));
            testTable.Columns.Add("col_float", typeof(float));

            // Check for col constraints/properties
            testTable.Columns.Add("col_readonly").ReadOnly = true;

            testTable.Columns.Add("col_autoincrement", typeof(long)).AutoIncrement = true;
            testTable.Columns["col_autoincrement"].AutoIncrementStep = 5;
            testTable.Columns["col_autoincrement"].AutoIncrementSeed = 10;

            testTable.Columns.Add("col_pk");
            testTable.PrimaryKey = new DataColumn[] { testTable.Columns["col_pk"] };

            testTable.Columns.Add("col_unique");
            testTable.Columns["col_unique"].Unique = true;

            testTable.Columns.Add("col_defaultvalue");
            testTable.Columns["col_defaultvalue"].DefaultValue = "DefaultValue";

            testTable.Columns.Add("col_expression_local", typeof(int));
            testTable.Columns["col_expression_local"].Expression = "col_int*5";

            ds.Relations.Add("rel", new DataColumn[] { testTable1.Columns["col1"] },
                    new DataColumn[] { testTable.Columns["col_int"] }, false);
            testTable.Columns.Add("col_expression_ext");
            testTable.Columns["col_expression_ext"].Expression = "parent.col2";

            testTable.Columns.Add("col_namespace");
            testTable.Columns["col_namespace"].Namespace = "ColumnNamespace";

            testTable.Columns.Add("col_mapping");
            testTable.Columns["col_mapping"].ColumnMapping = MappingType.Attribute;

            DataTable schemaTable = testTable.CreateDataReader().GetSchemaTable();

            Assert.Equal(25, schemaTable.Columns.Count);
            Assert.Equal(testTable.Columns.Count, schemaTable.Rows.Count);

            //True for all rows
            for (int i = 0; i < schemaTable.Rows.Count; ++i)
            {
                Assert.Equal(testTable.TableName, schemaTable.Rows[i]["BaseTableName"]);
                Assert.Equal(ds.DataSetName, schemaTable.Rows[i]["BaseCatalogName"]);
                Assert.Equal(DBNull.Value, schemaTable.Rows[i]["BaseSchemaName"]);
                Assert.Equal(schemaTable.Rows[i]["BaseColumnName"], schemaTable.Rows[i]["ColumnName"]);
                Assert.False((bool)schemaTable.Rows[i]["IsRowVersion"]);
            }

            Assert.Equal("col_string", schemaTable.Rows[0]["ColumnName"]);
            Assert.Equal(typeof(string), schemaTable.Rows[0]["DataType"]);
            Assert.Equal(-1, schemaTable.Rows[0]["ColumnSize"]);
            Assert.Equal(0, schemaTable.Rows[0]["ColumnOrdinal"]);
            // ms.net contradicts documented behavior
            Assert.False((bool)schemaTable.Rows[0]["IsLong"]);

            Assert.Equal("col_string_fixed", schemaTable.Rows[1]["ColumnName"]);
            Assert.Equal(typeof(string), schemaTable.Rows[1]["DataType"]);
            Assert.Equal(10, schemaTable.Rows[1]["ColumnSize"]);
            Assert.Equal(1, schemaTable.Rows[1]["ColumnOrdinal"]);
            Assert.False((bool)schemaTable.Rows[1]["IsLong"]);

            Assert.Equal("col_int", schemaTable.Rows[2]["ColumnName"]);
            Assert.Equal(typeof(int), schemaTable.Rows[2]["DataType"]);
            Assert.Equal(DBNull.Value, schemaTable.Rows[2]["NumericPrecision"]);
            Assert.Equal(DBNull.Value, schemaTable.Rows[2]["NumericScale"]);
            Assert.Equal(-1, schemaTable.Rows[2]["ColumnSize"]);
            Assert.Equal(2, schemaTable.Rows[2]["ColumnOrdinal"]);

            Assert.Equal("col_decimal", schemaTable.Rows[3]["ColumnName"]);
            Assert.Equal(typeof(decimal), schemaTable.Rows[3]["DataType"]);
            // When are the Precision and Scale Values set ? 
            Assert.Equal(DBNull.Value, schemaTable.Rows[3]["NumericPrecision"]);
            Assert.Equal(DBNull.Value, schemaTable.Rows[3]["NumericScale"]);
            Assert.Equal(-1, schemaTable.Rows[3]["ColumnSize"]);
            Assert.Equal(3, schemaTable.Rows[3]["ColumnOrdinal"]);

            Assert.Equal("col_datetime", schemaTable.Rows[4]["ColumnName"]);
            Assert.Equal(typeof(DateTime), schemaTable.Rows[4]["DataType"]);
            Assert.Equal(4, schemaTable.Rows[4]["ColumnOrdinal"]);

//.........这里部分代码省略.........
开发者ID:dotnet,项目名称:corefx,代码行数:101,代码来源:DataTableReaderTest.cs


注:本文中的DataTable.CreateDataReader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。