Created
June 11, 2013 00:13
-
-
Save jeremylong/5753584 to your computer and use it in GitHub Desktop.
Binding a user defined table type for use within an IN clause.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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