本文整理匯總了C#中System.Data.DataTable.ReadFlatFile方法的典型用法代碼示例。如果您正苦於以下問題:C# DataTable.ReadFlatFile方法的具體用法?C# DataTable.ReadFlatFile怎麽用?C# DataTable.ReadFlatFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Data.DataTable
的用法示例。
在下文中一共展示了DataTable.ReadFlatFile方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: TestReadFlatFile_ClearsSchemaAndData
public void TestReadFlatFile_ClearsSchemaAndData()
{
// fill the table with some existing columns, constraints and data
DataTable table = new DataTable();
DataColumn column = table.Columns.Add("blah", typeof(int));
table.Columns.Add("bleh", typeof(string));
table.Constraints.Add("PK_blah", column, true);
DataRow row = table.Rows.Add(new object[] { 123, "dopey" });
row.AcceptChanges();
const string text = @"id,name,created,avg
123,Bob,12/31/2012,3.14159";
SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true };
Stream stream = new MemoryStream(Encoding.Default.GetBytes(text));
IParser parser = new SeparatedValueParser(stream, options);
table.ReadFlatFile(parser);
Assert.AreEqual(4, table.Columns.Count, "The wrong number of columns were extracted.");
Assert.IsTrue(table.Columns.Contains("id"), "The ID column was not extracted.");
Assert.IsTrue(table.Columns.Contains("name"), "The name column was not extracted.");
Assert.IsTrue(table.Columns.Contains("created"), "The created column was not extracted.");
Assert.IsTrue(table.Columns.Contains("avg"), "The AVG column was not extracted.");
Assert.AreEqual(1, table.Rows.Count, "Not all of the records were extracted.");
row = table.Rows[0];
object[] expected = new object[] { "123", "Bob", "12/31/2012", "3.14159" };
object[] values = row.ItemArray;
CollectionAssert.AreEqual(expected, values, "The wrong values were extracted");
}
示例2: TestReadFlatFile_ClearsSchemaAndData
public void TestReadFlatFile_ClearsSchemaAndData()
{
// fill the table with some existing columns, constraints and data
DataTable table = new DataTable();
DataColumn column = table.Columns.Add("blah", typeof(int));
table.Columns.Add("bleh", typeof(string));
table.Constraints.Add("PK_blah", column, true);
DataRow row = table.Rows.Add(new object[] { 123, "dopey" });
row.AcceptChanges();
const string text = @"id,name,created,avg
123,Bob,12/31/2012,3.14159";
SeparatedValueOptions options = new SeparatedValueOptions() { IsFirstRecordSchema = true };
StringReader stringReader = new StringReader(text);
IReader parser = new SeparatedValueReader(stringReader, options);
table.ReadFlatFile(parser);
Assert.Equal(4, table.Columns.Count);
Assert.True(table.Columns.Contains("id"), "The ID column was not extracted.");
Assert.True(table.Columns.Contains("name"), "The name column was not extracted.");
Assert.True(table.Columns.Contains("created"), "The created column was not extracted.");
Assert.True(table.Columns.Contains("avg"), "The AVG column was not extracted.");
Assert.Equal(1, table.Rows.Count);
row = table.Rows[0];
object[] expected = new object[] { "123", "Bob", "12/31/2012", "3.14159" };
object[] values = row.ItemArray;
Assert.Equal(expected, values);
}
示例3: TestReadFlatFile_ExtractsSchema_PopulatesTable
public void TestReadFlatFile_ExtractsSchema_PopulatesTable()
{
const string text = @"id,name,created,avg
123,Bob,12/31/2012,3.14159";
SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true };
Stream stream = new MemoryStream(Encoding.Default.GetBytes(text));
DataTable table = new DataTable();
IParser parser = new SeparatedValueParser(stream, options);
table.ReadFlatFile(parser);
Assert.AreEqual(4, table.Columns.Count, "The wrong number of columns were extracted.");
Assert.IsTrue(table.Columns.Contains("id"), "The ID column was not extracted.");
Assert.IsTrue(table.Columns.Contains("name"), "The name column was not extracted.");
Assert.IsTrue(table.Columns.Contains("created"), "The created column was not extracted.");
Assert.IsTrue(table.Columns.Contains("avg"), "The AVG column was not extracted.");
Assert.AreEqual(1, table.Rows.Count, "Not all of the records were extracted.");
DataRow row = table.Rows[0];
object[] expected = new object[] { "123", "Bob", "12/31/2012", "3.14159" };
object[] values = row.ItemArray;
CollectionAssert.AreEqual(expected, values, "The wrong values were extracted");
}
示例4: TestReadFlatFile_ParserNull_Throws
public void TestReadFlatFile_ParserNull_Throws()
{
DataTable table = new DataTable();
IParser parser = null;
table.ReadFlatFile(parser);
}
示例5: TestReadFlatFile_SchemaProvided_TypesUsed
public void TestReadFlatFile_SchemaProvided_TypesUsed()
{
const string text = @"123,Bob,12/31/2012,3.14159";
Stream stream = new MemoryStream(Encoding.Default.GetBytes(text));
DataTable table = new DataTable();
Schema schema = new Schema();
schema.AddColumn(new Int32Column("id"))
.AddColumn(new StringColumn("name"))
.AddColumn(new DateTimeColumn("created"))
.AddColumn(new DoubleColumn("avg"));
IParser parser = new SeparatedValueParser(stream, schema);
table.ReadFlatFile(parser);
Assert.AreEqual(4, table.Columns.Count, "The wrong number of columns were extracted.");
Assert.IsTrue(table.Columns.Contains("id"), "The ID column was not extracted.");
Assert.IsTrue(table.Columns.Contains("name"), "The name column was not extracted.");
Assert.IsTrue(table.Columns.Contains("created"), "The created column was not extracted.");
Assert.IsTrue(table.Columns.Contains("avg"), "The AVG column was not extracted.");
Assert.AreEqual(1, table.Rows.Count, "Not all of the records were extracted.");
DataRow row = table.Rows[0];
object[] expected = new object[] { 123, "Bob", new DateTime(2012, 12, 31), 3.14159 };
object[] values = row.ItemArray;
CollectionAssert.AreEqual(expected, values, "The wrong values were extracted");
}
示例6: TestReadFlatFile_ExtractsSchema_PopulatesTable
public void TestReadFlatFile_ExtractsSchema_PopulatesTable()
{
SeparatedValueSchema schema = new SeparatedValueSchema();
schema.AddColumn(new Int32Column("id"))
.AddColumn(new StringColumn("name"))
.AddColumn(new DateTimeColumn("created") { InputFormat = "MM/dd/yyyy", OutputFormat = "MM/dd/yyyy" })
.AddColumn(new DecimalColumn("avg"));
SeparatedValueOptions options = new SeparatedValueOptions() { IsFirstRecordSchema = true };
StringWriter stringWriter = new StringWriter();
SeparatedValueWriter builder = new SeparatedValueWriter(stringWriter, schema, options);
builder.Write(new object[] { 123, "Bob", new DateTime(2012, 12, 31), 3.14159m });
StringReader stringReader = new StringReader(stringWriter.ToString());
DataTable table = new DataTable();
IReader parser = new SeparatedValueReader(stringReader, options);
table.ReadFlatFile(parser);
Assert.Equal(4, table.Columns.Count);
Assert.True(table.Columns.Contains("id"), "The ID column was not extracted.");
Assert.True(table.Columns.Contains("name"), "The name column was not extracted.");
Assert.True(table.Columns.Contains("created"), "The created column was not extracted.");
Assert.True(table.Columns.Contains("avg"), "The AVG column was not extracted.");
Assert.Equal(1, table.Rows.Count);
DataRow row = table.Rows[0];
object[] expected = new object[] { "123", "Bob", "12/31/2012", "3.14159" };
object[] values = row.ItemArray;
Assert.Equal(expected, values);
}
示例7: TestReadFlatFile_SchemaProvided_TypesUsed
public void TestReadFlatFile_SchemaProvided_TypesUsed()
{
const string text = @"123,Bob,12/31/2012,3.14159";
DataTable table = new DataTable();
SeparatedValueSchema schema = new SeparatedValueSchema();
schema.AddColumn(new Int32Column("id"))
.AddColumn(new StringColumn("name"))
.AddColumn(new DateTimeColumn("created"))
.AddColumn(new DoubleColumn("avg"));
StringReader stringReader = new StringReader(text);
IReader parser = new SeparatedValueReader(stringReader, schema);
table.ReadFlatFile(parser);
Assert.Equal(4, table.Columns.Count);
Assert.True(table.Columns.Contains("id"), "The ID column was not extracted.");
Assert.True(table.Columns.Contains("name"), "The name column was not extracted.");
Assert.True(table.Columns.Contains("created"), "The created column was not extracted.");
Assert.True(table.Columns.Contains("avg"), "The AVG column was not extracted.");
Assert.Equal(1, table.Rows.Count);
DataRow row = table.Rows[0];
object[] expected = new object[] { 123, "Bob", new DateTime(2012, 12, 31), 3.14159 };
object[] values = row.ItemArray;
Assert.Equal(expected, values);
}
示例8: TestReadFlatFile_ParserNull_Throws
public void TestReadFlatFile_ParserNull_Throws()
{
DataTable table = new DataTable();
IReader parser = null;
Assert.Throws<ArgumentNullException>(() => table.ReadFlatFile(parser));
}