Skip to content

Instantly share code, notes, and snippets.

@joshka
Created March 12, 2011 14:03
Show Gist options
  • Save joshka/867254 to your computer and use it in GitHub Desktop.
Save joshka/867254 to your computer and use it in GitHub Desktop.
A C# implementation of Stub for DataTable creation as a test helper.
using System;
using System.Collections;
using System.Data;
namespace TestDataTableBuilder
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new StubDataTable("FirstName", "LastName", "Age")
{
{ "John", "Doe", 25 },
{ "Jane", "Doe", 24 }
};
dt.TableName = "Person"; // can't write xml without a name for the table
dt.WriteXml(Console.Out);
}
}
class StubDataTable : IEnumerable
{
private readonly DataTable _table;
public StubDataTable(params string[] columnNames)
{
_table = new DataTable();
foreach (var columnName in columnNames)
_table.Columns.Add(columnName);
}
public void Add(params object[] values)
{
_table.LoadDataRow(values, LoadOption.OverwriteChanges);
}
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
public static implicit operator DataTable(StubDataTable dt)
{
return dt._table;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment