Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT blahblahblah WHERE blahblahblah IN @LISTOFWORDS
- DataTable dt = new DataTable();
- dt.Columns.Add("word", typeof(string));
- foreach (String word in listOfWords)
- {
- dt.Rows.Add(word);
- }
- comm.Parameters.Add("LISTOFWORDS", System.Data.SqlDbType.Structured).Value = dt;
- SELECT * FROM [Table] WHERE ID IN (1,2,3)
- SELECT * FROM [Table] WHERE ID IN ( SELECT TableID FROM [OtherTable] WHERE OtherTableID= @OtherTableID )
- StringBuilder sb = new StringBuilder();
- for (int i=0; i< listOfWords.Count; i++)
- {
- sb.AppendFormat("p{0},",i);
- comm.Parameters.AddWithValue("p"+i.ToString(), listOfWords[i]);
- }
- comm.CommandText = string.Format(""SELECT blahblahblah WHERE blahblahblah IN ({0})",
- sb.ToString().TrimEnd(','));
- SELECT blah WHERE blah IN (p0,p1,p2,p3...)...p0='aaa',p1='bbb'
- ALTER FUNCTION [dbo].[Split]
- (
- @RowData nvarchar(max),
- @SplitOn nvarchar(5) = ','
- )
- RETURNS @RtnValue table
- (
- Id int identity(1,1),
- Data nvarchar(100)
- )
- AS
- BEGIN
- Declare @Cnt int
- Set @Cnt = 1
- While (Charindex(@SplitOn,@RowData)>0)
- Begin
- Insert Into @RtnValue (data)
- Select
- Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
- Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
- Set @Cnt = @Cnt + 1
- End
- Insert Into @RtnValue (data)
- Select Data = ltrim(rtrim(@RowData))
- Return
- END
- Select Id, Data from dbo.Split('123,234,345,456',',')
- CREATE PROCEDURE [dbo].[findDuplicates]
- @ids nvarchar(max)
- as
- begin
- select ID
- from SomeTable with (nolock)
- where ID in (select Data from dbo.Split(@ids,','))
- end
- public void SomeFunction(List<int> ids)
- {
- var idsAsDelimitedString = string.Join(",", ids.Select(id => id.ToString()).ToArray());
- // ... or however you make your connection
- var con = GetConnection();
- try
- {
- con.Open();
- var cmd = new SqlCommand("findDuplicates", con);
- cmd.Parameters.Add(new SqlParameter("@ids", idsAsDelimitedString));
- var reader = cmd.ExecuteReader();
- // .... do something here.
- }
- catch (Exception)
- {
- // catch an exception?
- }
- finally
- {
- con.Close();
- }
- }
Add Comment
Please, Sign In to add comment