You don't have to use DataTable
:
System.Data.SqlClient supports populating table-valued parameters from
DataTable
,DbDataReader
orSystem.Collections.Generic.IEnumerable<SqlDataRecord>
([T:System.Collections.Generic.IEnumerable`1)] objects. You must specify a type name for the table-valued parameter by using the TypeName property of a SqlParameter. The TypeName must match the name of a compatible type previously created on the server. The following code fragment demonstrates how to configure SqlParameter to insert data.
The above is from Table-Valued Parameters (MSDN).
... but using DataTable
is probably much easier than the alternatives; here's a simple example showing how to create a DataTable
from an IEnumerable<string>
:
IEnumerable<string> strings = new List<string>() { "blah", "blah blah", "blah blah blah" };
DataTable table = new DataTable();
table.Columns.Add(
new DataColumn()
{
DataType = Type.GetType("System.String"),
ColumnName = "String"
});
DataRow row;
foreach (string aString in strings)
{
row = table.NewRow();
row["String"] = aString;
table.Rows.Add(row);
}