Answer the question
In order to leave comments, you need to log in
How to parametrize a query using an array (WHERE field IN (...)?
"SELECT * FROM testimony WHERE Name IN('" + "Record 1" + "','"+ "Record 2" + "')" works. How to make the values loaded from an array (array)?
The array itself:
string[] array = new string[] { "Record 1", "Record 2" };
Answer the question
In order to leave comments, you need to log in
Create a type in the database
CREATE TYPE [dbo].[TableValueType] AS TABLE(
[value] varchar(50) NOT NULL
)
var tableParam = new SqlParameter("@paramName", SqlDbType.Structured);
tableParam.TypeName = "[dbo].[TableValueType]";
tableParam.Direction = ParameterDirection.Input;
// Установка значений
var rows = new List<SqlDataRecord>();
var sqlMetaData = new SqlMetaData("value", SqlDbType.VarChar);
foreach (var value in values)
{
var row = new SqlDataRecord(sqlMetaData);
row.SetValues(value);
rows.Add(row);
}
tableParam.Value = rows;
SELECT * FROM testimony WHERE Name IN (select [value] from @paramName)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question