Skip to content

Instantly share code, notes, and snippets.

@jeremylong
Created June 11, 2013 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremylong/5753584 to your computer and use it in GitHub Desktop.
Save jeremylong/5753584 to your computer and use it in GitHub Desktop.
Binding a user defined table type for use within an IN clause.
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
var row = dt.NewRow();
row["id"] = 1;
dt.Rows.Add(row);
row = dt.NewRow();
row["id"] = 2;
dt.Rows.Add(row);
SqlConnection conn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=test");
conn.Open();
SqlCommand cmd = new SqlCommand("usp_GetDetails @ids", conn);
SqlParameter param = cmd.Parameters.AddWithValue("@ids", dt);
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.PrimaryKeyINT";
var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read()) {
Object[] values = new Object[reader.FieldCount];
int fieldCount = reader.GetValues(values);
Console.WriteLine(String.Format("ID:{0}, Product:'{1}', Date:{2}, Price:{3}", values));
}
Console.ReadLine();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment