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


C# DataTable.ImportRow方法代码示例

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


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

示例1: ImportRowTest

        public void ImportRowTest()
        {
            // build source table
            DataTable src = new DataTable();
            src.Columns.Add("id", typeof(int));
            src.Columns.Add("name", typeof(string));

            src.PrimaryKey = new DataColumn[] { src.Columns[0] };

            src.Rows.Add(new object[] { 1, "mono 1" });
            src.Rows.Add(new object[] { 2, "mono 2" });
            src.Rows.Add(new object[] { 3, "mono 3" });
            src.AcceptChanges();

            src.Rows[0][1] = "mono changed 1";  // modify 1st row
            src.Rows[1].Delete();              // delete 2nd row
                                               // 3rd row is unchanged
            src.Rows.Add(new object[] { 4, "mono 4" }); // add 4th row

            // build target table
            DataTable target = new DataTable();
            target.Columns.Add("id", typeof(int));
            target.Columns.Add("name", typeof(string));

            target.PrimaryKey = new DataColumn[] { target.Columns[0] };

            // import all rows
            target.ImportRow(src.Rows[0]);     // import 1st row
            target.ImportRow(src.Rows[1]);     // import 2nd row
            target.ImportRow(src.Rows[2]);     // import 3rd row
            target.ImportRow(src.Rows[3]);     // import 4th row

            try
            {
                target.ImportRow(src.Rows[2]); // import 3rd row again
                Assert.False(true);
            }
            catch (ConstraintException ex)
            {
                // Column 'id' is constrained to be unique.
                // Value '3' is already present
                Assert.Equal(typeof(ConstraintException), ex.GetType());
                Assert.Null(ex.InnerException);
                Assert.NotNull(ex.Message);
                Assert.True(ex.Message.IndexOf("'id'") != -1);
                Assert.True(ex.Message.IndexOf("'3'") != -1);
            }

            // check row states
            Assert.Equal(src.Rows[0].RowState, target.Rows[0].RowState);
            Assert.Equal(src.Rows[1].RowState, target.Rows[1].RowState);
            Assert.Equal(src.Rows[2].RowState, target.Rows[2].RowState);
            Assert.Equal(src.Rows[3].RowState, target.Rows[3].RowState);

            // check for modified row (1st row)
            Assert.Equal((string)src.Rows[0][1], (string)target.Rows[0][1]);
            Assert.Equal((string)src.Rows[0][1, DataRowVersion.Default], (string)target.Rows[0][1, DataRowVersion.Default]);
            Assert.Equal((string)src.Rows[0][1, DataRowVersion.Original], (string)target.Rows[0][1, DataRowVersion.Original]);
            Assert.Equal((string)src.Rows[0][1, DataRowVersion.Current], (string)target.Rows[0][1, DataRowVersion.Current]);
            Assert.False(target.Rows[0].HasVersion(DataRowVersion.Proposed));

            // check for deleted row (2nd row)
            Assert.Equal((string)src.Rows[1][1, DataRowVersion.Original], (string)target.Rows[1][1, DataRowVersion.Original]);

            // check for unchanged row (3rd row)
            Assert.Equal((string)src.Rows[2][1], (string)target.Rows[2][1]);
            Assert.Equal((string)src.Rows[2][1, DataRowVersion.Default], (string)target.Rows[2][1, DataRowVersion.Default]);
            Assert.Equal((string)src.Rows[2][1, DataRowVersion.Original], (string)target.Rows[2][1, DataRowVersion.Original]);
            Assert.Equal((string)src.Rows[2][1, DataRowVersion.Current], (string)target.Rows[2][1, DataRowVersion.Current]);

            // check for newly added row (4th row)
            Assert.Equal((string)src.Rows[3][1], (string)target.Rows[3][1]);
            Assert.Equal((string)src.Rows[3][1, DataRowVersion.Default], (string)target.Rows[3][1, DataRowVersion.Default]);
            Assert.Equal((string)src.Rows[3][1, DataRowVersion.Current], (string)target.Rows[3][1, DataRowVersion.Current]);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:75,代码来源:DataTableTest.cs

示例2: GetNewDataTable

 /// <summary>
 /// DataTable条件查询获取子DataTable
 /// </summary>
 /// <param name="dt"></param>
 /// <param name="condition">条件</param>
 /// <returns></returns>
 public static DataTable GetNewDataTable(this DataTable dt, string condition)
 {
     DataTable newdt = new DataTable();
     newdt = dt.Clone();
     DataRow[] dr = dt.Select(condition);
     for (int i = 0; i < dr.Length; i++)
     {
         newdt.ImportRow((DataRow)dr[i]);
     }
     return newdt;//返回的查询结果
 }
开发者ID:Caiqb-kooboo,项目名称:DBDcoumentCreater,代码行数:17,代码来源:StringExt.cs

示例3: ImportRowDeletedTest

        public void ImportRowDeletedTest()
        {
            DataTable table = new DataTable();
            table.Columns.Add("col", typeof(int));
            table.Columns.Add("col1", typeof(int));

            DataRow row = table.Rows.Add(new object[] { 1, 2 });
            table.PrimaryKey = new DataColumn[] { table.Columns[0] };
            table.AcceptChanges();

            // If row is in Deleted state, then ImportRow loads the
            // row.
            row.Delete();
            table.ImportRow(row);
            Assert.Equal(2, table.Rows.Count);

            // Both the deleted rows shud be now gone
            table.AcceptChanges();
            Assert.Equal(0, table.Rows.Count);

            //just add another row
            row = table.Rows.Add(new object[] { 1, 2 });
            // no exception shud be thrown
            table.AcceptChanges();

            // If row is in Deleted state, then ImportRow loads the
            // row and validate only on RejectChanges
            row.Delete();
            table.ImportRow(row);
            Assert.Equal(2, table.Rows.Count);
            Assert.Equal(DataRowState.Deleted, table.Rows[1].RowState);

            try
            {
                table.RejectChanges();
                Assert.False(true);
            }
            catch (ConstraintException ex)
            {
                // Column 'col' is constrained to be unique.
                // Value '1' is already present
                Assert.Equal(typeof(ConstraintException), ex.GetType());
                Assert.Null(ex.InnerException);
                Assert.NotNull(ex.Message);
                Assert.True(ex.Message.IndexOf("'col'") != -1);
                Assert.True(ex.Message.IndexOf("'1'") != -1);
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:48,代码来源:DataTableTest.cs

示例4: ImportRowDetachedTest

        public void ImportRowDetachedTest()
        {
            DataTable table = new DataTable();
            DataColumn col = new DataColumn();
            col.ColumnName = "Id";
            col.DataType = typeof(int);
            table.Columns.Add(col);

            table.PrimaryKey = new DataColumn[] { col };

            col = new DataColumn();
            col.ColumnName = "Name";
            col.DataType = typeof(string);
            table.Columns.Add(col);

            DataRow row = table.NewRow();
            row["Id"] = 147;
            row["name"] = "Abc";

            // keep silent as ms.net ;-), though this is not useful.
            table.ImportRow(row);

            //if RowState is detached, then dont import the row.
            Assert.Equal(0, table.Rows.Count);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:25,代码来源:DataTableTest.cs


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