本文整理汇总了C#中DataTable.LoadDataRow方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.LoadDataRow方法的具体用法?C# DataTable.LoadDataRow怎么用?C# DataTable.LoadDataRow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataTable
的用法示例。
在下文中一共展示了DataTable.LoadDataRow方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadRowTest
public void LoadRowTest()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(new object[] { 1, "mono 1" });
dt.Rows.Add(new object[] { 2, "mono 2" });
dt.Rows.Add(new object[] { 3, "mono 3" });
dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
dt.AcceptChanges();
dt.LoadDataRow(new object[] { 4, "mono 4" }, LoadOption.Upsert);
Assert.Equal(4, dt.Rows.Count);
}
示例2: DataSetExtendedPropertiesTest
public void DataSetExtendedPropertiesTest()
{
DataSet dataSet1 = new DataSet();
dataSet1.ExtendedProperties.Add("DS1", "extended0");
DataTable table = new DataTable("TABLE1");
table.ExtendedProperties.Add("T1", "extended1");
table.Columns.Add("C1", typeof(int));
table.Columns.Add("C2", typeof(string));
table.Columns[1].MaxLength = 20;
table.Columns[0].ExtendedProperties.Add("C1Ext1", "extended2");
table.Columns[1].ExtendedProperties.Add("C2Ext1", "extended3");
dataSet1.Tables.Add(table);
table.LoadDataRow(new object[] { 1, "One" }, false);
table.LoadDataRow(new object[] { 2, "Two" }, false);
string file = Path.Combine(Path.GetTempPath(), "schemas-test.xml");
try
{
dataSet1.WriteXml(file, XmlWriteMode.WriteSchema);
}
catch (Exception ex)
{
Assert.False(true);
}
finally
{
File.Delete(file);
}
DataSet dataSet2 = new DataSet();
dataSet2.ReadXml(new StringReader(
@"<?xml version=""1.0"" standalone=""yes""?>
<NewDataSet>
<xs:schema id=""NewDataSet"" xmlns=""""
xmlns:xs=""http://www.w3.org/2001/XMLSchema""
xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""
xmlns:msprop=""urn:schemas-microsoft-com:xml-msprop"">
<xs:element name=""NewDataSet"" msdata:IsDataSet=""true""
msdata:UseCurrentLocale=""true"" msprop:DS1=""extended0"">
<xs:complexType>
<xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
<xs:element name=""TABLE1"" msprop:T1=""extended1"">
<xs:complexType>
<xs:sequence>
<xs:element name=""C1"" type=""xs:int"" minOccurs=""0""
msprop:C1Ext1=""extended2"" />
<xs:element name=""C2"" type=""xs:string"" minOccurs=""0""
msprop:C2Ext1=""extended3"" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<TABLE1>
<C1>1</C1>
<C2>One</C2>
</TABLE1>
<TABLE1>
<C1>2</C1>
<C2>Two</C2>
</TABLE1>
</NewDataSet>"), XmlReadMode.ReadSchema);
Assert.Equal(dataSet1.ExtendedProperties["DS1"], dataSet2.ExtendedProperties["DS1"]);
Assert.Equal(dataSet1.Tables[0].ExtendedProperties["T1"], dataSet2.Tables[0].ExtendedProperties["T1"]);
Assert.Equal(dataSet1.Tables[0].Columns[0].ExtendedProperties["C1Ext1"],
dataSet2.Tables[0].Columns[0].ExtendedProperties["C1Ext1"]);
Assert.Equal(dataSet1.Tables[0].Columns[1].ExtendedProperties["C2Ext1"],
dataSet2.Tables[0].Columns[1].ExtendedProperties["C2Ext1"]);
}
示例3: LoadDataException
public void LoadDataException()
{
DataTable table = new DataTable();
DataColumn col = new DataColumn();
col.ColumnName = "Id";
col.DataType = typeof(int);
col.DefaultValue = 47;
table.Columns.Add(col);
UniqueConstraint uc = new UniqueConstraint("UK1", table.Columns[0]);
table.Constraints.Add(uc);
col = new DataColumn();
col.ColumnName = "Name";
col.DataType = typeof(string);
col.DefaultValue = "Hello";
table.Columns.Add(col);
table.BeginLoadData();
object[] row = { 147, "Abc" };
DataRow newRow = table.LoadDataRow(row, true);
object[] row1 = { 147, "Efg" };
DataRow newRow1 = table.LoadDataRow(row1, true);
object[] row2 = { 143, "Hij" };
DataRow newRow2 = table.LoadDataRow(row2, true);
try
{
table.EndLoadData();
Assert.False(true);
}
catch (ConstraintException)
{
}
}
示例4: FindByKey_DuringDataLoad
public void FindByKey_DuringDataLoad()
{
DataTable table = new DataTable();
table.Columns.Add("col1", typeof(int));
table.PrimaryKey = new DataColumn[] { table.Columns[0] };
table.Rows.Add(new object[] { 1 });
table.Rows.Add(new object[] { 2 });
table.AcceptChanges();
table.BeginLoadData();
table.LoadDataRow(new object[] { 1000 }, false);
Assert.NotNull(table.Rows.Find(1));
Assert.NotNull(table.Rows.Find(1000));
table.EndLoadData();
Assert.NotNull(table.Rows.Find(1000));
}
示例5: LoadRowTestUpsert
public void LoadRowTestUpsert()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(new object[] { 1, "mono 1" });
dt.Rows.Add(new object[] { 2, "mono 2" });
dt.Rows.Add(new object[] { 3, "mono 3" });
dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
dt.AcceptChanges();
try
{
SubscribeEvents(dt);
ResetEventFlags();
dt.LoadDataRow(new object[] { 2, "mono test" }, LoadOption.Upsert);
Assert.Equal(3, dt.Rows.Count);
Assert.Equal("mono test", dt.Rows[1][1]);
Assert.Equal("mono 2", dt.Rows[1][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Modified, dt.Rows[1].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[1], _rowInAction_Changing);
Assert.Equal(DataRowAction.Change, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[1], _rowInAction_Changed);
Assert.Equal(DataRowAction.Change, _rowAction_Changed);
// Row State tests
// current - modified ; result - modified
ResetEventFlags();
dt.LoadDataRow(new object[] { 2, "mono test 2" }, LoadOption.Upsert);
Assert.Equal("mono test 2", dt.Rows[1][1]);
Assert.Equal("mono 2", dt.Rows[1][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Modified, dt.Rows[1].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[1], _rowInAction_Changing);
Assert.Equal(DataRowAction.Change, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[1], _rowInAction_Changed);
Assert.Equal(DataRowAction.Change, _rowAction_Changed);
// current - Unchanged; result - Unchanged if no new value
dt.AcceptChanges();
ResetEventFlags();
dt.LoadDataRow(new object[] { 2, "mono test 2" }, LoadOption.Upsert);
Assert.Equal("mono test 2", dt.Rows[1][1]);
Assert.Equal("mono test 2", dt.Rows[1][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Unchanged, dt.Rows[1].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[1], _rowInAction_Changing);
Assert.Equal(DataRowAction.Nothing, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[1], _rowInAction_Changed);
Assert.Equal(DataRowAction.Nothing, _rowAction_Changed);
// not the same value again
dt.RejectChanges();
ResetEventFlags();
dt.LoadDataRow(new object[] { 2, "mono test 3" }, LoadOption.Upsert);
Assert.Equal(DataRowState.Modified, dt.Rows[1].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[1], _rowInAction_Changing);
Assert.Equal(DataRowAction.Change, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[1], _rowInAction_Changed);
Assert.Equal(DataRowAction.Change, _rowAction_Changed);
// current - added; result - added
dt.Rows.Add(new object[] { 4, "mono 4" });
ResetEventFlags();
dt.LoadDataRow(new object[] { 4, "mono 4" }, LoadOption.Upsert);
Assert.Equal("mono 4", dt.Rows[3][1]);
try
{
object o = dt.Rows[3][1, DataRowVersion.Original];
Assert.False(true);
}
catch (VersionNotFoundException) { }
Assert.Equal(DataRowState.Added, dt.Rows[3].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[3], _rowInAction_Changing);
Assert.Equal(DataRowAction.Change, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[3], _rowInAction_Changed);
Assert.Equal(DataRowAction.Change, _rowAction_Changed);
// current - none; result - added
ResetEventFlags();
dt.LoadDataRow(new object[] { 5, "mono 5" }, LoadOption.Upsert);
Assert.Equal("mono 5", dt.Rows[4][1]);
try
{
object o = dt.Rows[4][1, DataRowVersion.Original];
//.........这里部分代码省略.........
示例6: LoadRowAutoIncrementTest
public void LoadRowAutoIncrementTest()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("age", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns[0].AutoIncrementSeed = 10;
dt.Columns[0].AutoIncrementStep = 5;
dt.Columns[0].AutoIncrement = true;
dt.Rows.Add(new object[] { null, 15, "mono 1" });
dt.Rows.Add(new object[] { null, 25, "mono 2" });
dt.Rows.Add(new object[] { null, 35, "mono 3" });
dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
dt.AcceptChanges();
dt.LoadDataRow(new object[] { null, 20, "mono test" }, LoadOption.OverwriteChanges);
Assert.Equal(4, dt.Rows.Count);
Assert.Equal(25, dt.Rows[3][0]);
Assert.Equal(25, dt.Rows[3][0, DataRowVersion.Original]);
dt.LoadDataRow(new object[] { 25, 20, "mono test" }, LoadOption.Upsert);
dt.LoadDataRow(new object[] { 25, 20, "mono test 2" }, LoadOption.Upsert);
dt.LoadDataRow(new object[] { null, 20, "mono test aaa" }, LoadOption.Upsert);
Assert.Equal(5, dt.Rows.Count);
Assert.Equal(25, dt.Rows[3][0]);
Assert.Equal(25, dt.Rows[3][0, DataRowVersion.Original]);
Assert.Equal(30, dt.Rows[4][0]);
}
示例7: LoadRowDefaultValueTest
public void LoadRowDefaultValueTest()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("age", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns[1].DefaultValue = 20;
dt.Rows.Add(new object[] { 1, 15, "mono 1" });
dt.Rows.Add(new object[] { 2, 25, "mono 2" });
dt.Rows.Add(new object[] { 3, 35, "mono 3" });
dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
dt.AcceptChanges();
dt.LoadDataRow(new object[] { 2, null, "mono test" }, LoadOption.OverwriteChanges);
Assert.Equal(3, dt.Rows.Count);
Assert.Equal(25, dt.Rows[1][1]);
Assert.Equal(25, dt.Rows[1][1, DataRowVersion.Original]);
}
示例8: LoadRowTestPreserveChanges
public void LoadRowTestPreserveChanges()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(new object[] { 1, "mono 1" });
dt.Rows.Add(new object[] { 2, "mono 2" });
dt.Rows.Add(new object[] { 3, "mono 3" });
dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
dt.AcceptChanges();
try
{
SubscribeEvents(dt);
// current - modified; new - modified
ResetEventFlags();
dt.LoadDataRow(new object[] { 2, "mono test" }, LoadOption.PreserveChanges);
Assert.Equal(3, dt.Rows.Count);
Assert.Equal("mono test", dt.Rows[1][1]);
Assert.Equal("mono test", dt.Rows[1][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Unchanged, dt.Rows[1].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[1], _rowInAction_Changing);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[1], _rowInAction_Changed);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changed);
// current - none; new - unchanged
ResetEventFlags();
dt.LoadDataRow(new object[] { 4, "mono 4" }, LoadOption.PreserveChanges);
Assert.Equal(4, dt.Rows.Count);
Assert.Equal("mono 4", dt.Rows[3][1]);
Assert.Equal("mono 4", dt.Rows[3][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Unchanged, dt.Rows[3].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[3], _rowInAction_Changing);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[3], _rowInAction_Changed);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changed);
dt.RejectChanges();
// current - added; new - modified
dt.Rows.Add(new object[] { 5, "mono 5" });
ResetEventFlags();
dt.LoadDataRow(new object[] { 5, "mono test" }, LoadOption.PreserveChanges);
Assert.Equal(5, dt.Rows.Count);
Assert.Equal("mono 5", dt.Rows[4][1]);
Assert.Equal("mono test", dt.Rows[4][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Modified, dt.Rows[4].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[4], _rowInAction_Changing);
Assert.Equal(DataRowAction.ChangeOriginal, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[4], _rowInAction_Changed);
Assert.Equal(DataRowAction.ChangeOriginal, _rowAction_Changed);
dt.RejectChanges();
// current - deleted ; new - deleted ChangeOriginal
ResetEventFlags();
dt.Rows[1].Delete();
Assert.True(_rowDeleting);
Assert.True(_rowDeleted);
Assert.Equal(_rowInAction_Deleting, dt.Rows[1]);
Assert.Equal(_rowInAction_Deleted, dt.Rows[1]);
Assert.Equal(_rowAction_Deleting, DataRowAction.Delete);
Assert.Equal(_rowAction_Deleted, DataRowAction.Delete);
dt.LoadDataRow(new object[] { 2, "mono deleted" }, LoadOption.PreserveChanges);
Assert.Equal(5, dt.Rows.Count);
Assert.Equal("mono deleted", dt.Rows[1][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Deleted, dt.Rows[1].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[1], _rowInAction_Changing);
Assert.Equal(DataRowAction.ChangeOriginal, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[1], _rowInAction_Changed);
Assert.Equal(DataRowAction.ChangeOriginal, _rowAction_Changed);
}
finally
{
UnsubscribeEvents(dt);
}
}
示例9: LoadRowTestOverwriteChanges
public void LoadRowTestOverwriteChanges()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(new object[] { 1, "mono 1" });
dt.Rows.Add(new object[] { 2, "mono 2" });
dt.Rows.Add(new object[] { 3, "mono 3" });
dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
dt.AcceptChanges();
dt.Rows[1][1] = "overwrite";
Assert.Equal(DataRowState.Modified, dt.Rows[1].RowState);
try
{
SubscribeEvents(dt);
ResetEventFlags();
dt.LoadDataRow(new object[] { 2, "mono test" }, LoadOption.OverwriteChanges);
Assert.Equal(3, dt.Rows.Count);
Assert.Equal("mono test", dt.Rows[1][1]);
Assert.Equal("mono test", dt.Rows[1][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Unchanged, dt.Rows[1].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[1], _rowInAction_Changing);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[1], _rowInAction_Changed);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changed);
DataRow r = dt.Rows[1];
r[1] = "test";
Assert.Equal("test", dt.Rows[1][1]);
Assert.Equal("mono test", dt.Rows[1][1, DataRowVersion.Original]);
//Assert.Equal ("ramesh", dt.Rows [1] [1, DataRowVersion.Proposed]);
// Row State tests
// current - modified ; result - modified
ResetEventFlags();
dt.LoadDataRow(new object[] { 2, "mono test 2" }, LoadOption.OverwriteChanges);
Assert.Equal("mono test 2", dt.Rows[1][1]);
Assert.Equal("mono test 2", dt.Rows[1][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Unchanged, dt.Rows[1].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[1], _rowInAction_Changing);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[1], _rowInAction_Changed);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changed);
// current - Unchanged; result - Unchanged
dt.AcceptChanges();
ResetEventFlags();
dt.LoadDataRow(new object[] { 2, "mono test 2" }, LoadOption.OverwriteChanges);
Assert.Equal("mono test 2", dt.Rows[1][1]);
Assert.Equal("mono test 2", dt.Rows[1][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Unchanged, dt.Rows[1].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[1], _rowInAction_Changing);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[1], _rowInAction_Changed);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changed);
// current - added; result - added
dt.Rows.Add(new object[] { 4, "mono 4" });
ResetEventFlags();
dt.LoadDataRow(new object[] { 4, "mono 4" }, LoadOption.OverwriteChanges);
Assert.Equal("mono 4", dt.Rows[3][1]);
Assert.Equal("mono 4", dt.Rows[3][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Unchanged, dt.Rows[3].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[3], _rowInAction_Changing);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[3], _rowInAction_Changed);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changed);
// current - new; result - added
ResetEventFlags();
dt.LoadDataRow(new object[] { 5, "mono 5" }, LoadOption.OverwriteChanges);
Assert.Equal("mono 5", dt.Rows[4][1]);
Assert.Equal("mono 5", dt.Rows[4][1, DataRowVersion.Original]);
Assert.Equal(DataRowState.Unchanged, dt.Rows[4].RowState);
Assert.True(_rowChanging);
Assert.Equal(dt.Rows[4], _rowInAction_Changing);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changing);
Assert.True(_rowChanged);
Assert.Equal(dt.Rows[4], _rowInAction_Changed);
Assert.Equal(DataRowAction.ChangeCurrentAndOriginal, _rowAction_Changed);
// current - deleted; result - added a new row
ResetEventFlags();
dt.AcceptChanges();
dt.Rows[4].Delete();
//.........这里部分代码省略.........