CREATE PROCEDURE [dbo].[GetData] @MyCodes as varchar(500) = '', -- comma delimited list of codes, ie: '''ABC'', ''DEF'', ''GHI'''ASBEGIN DECLARE @query as nvarchar(500) set @query = 'SELECT * FROM DATA WHERE Code IN (@p_MyCodes)' exec SP_EXECUTESQL @query, N'@p_MyCodes varchar(500)', @p_MyCodes = @MyCodesEND
foreach (MyItem item in MyCollection){ if (myCodes.Length > 0) { myCodes += ", "; // Add a comma if data already exists } myCodes += "'" + item.Name + "'";}
using System;using System.Data;using System.Data.SqlClient;SqlConnection MyConnection = null;SqlDataReader MyReader = null;try{ // Create the SQL connection. MyConnection = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI")) MyConnection.Open(); // Create the stored procedure command. SqlCommand MyCommand = new SqlCommand("GetData", MyConnection); // Set the command type property. MyCommand.CommandType = CommandType.StoredProcedure; // Pass the string (array) into the stored procedure. MyCommand.Parameters.Add(new SqlParameter("@MyCodes", myCodes)); // Execute the command MyReader = MyCommand.ExecuteReader(); // ...}catch (Exception excep){}finally{ if (MyReader != null) { MyReader.Close(); MyReader.Dispose(); } if (MyConnection != null) { MyConnection.Close(); MyConnection.Dispose(); }}