0

What I want to do : Pass this parameter 'TV','OV','CK' as a single string into Stored Procedure (GetAllDataViaInQuery)

CREATE PROCEDURE GetAllDataViaInQuery @param varchar(240)
AS
BEGIN
SELECT TOP 100 [Model_No]
      ,[AppCode]
      ,[Model]
FROM [S_ModelMaster] where AppCode in (@param)  
END

Then

I need to Pass parameter value via C# application as a single parameter.Because some time in values are may be vary.

Ex : string paramValue = "TV,OV,CK";

Then I wrote this C# code snippet.

using (SqlConnection con = new SqlConnection(Properties.Settings.Default.Setting))
            {
                try
                {
                    //hard coded parameter values
                    string paramValue = "TV,OV,CK";
                    con.Open();
                    DataSet ds = new DataSet();
                    SqlCommand com = new SqlCommand("GetAllDataViaInQuery", con);
                    com.CommandType = CommandType.StoredProcedure;
                    SqlParameter param = new SqlParameter("@param", paramValue);
                    com.Parameters.Add(param);

                    SqlDataAdapter adp = new SqlDataAdapter(com);
                    adp.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

But this is not work yet. Then I execute Stored Procedure manually with SSMS.

enter image description here

DECLARE @return_value int

EXEC    @return_value = [application].[GetAllDataViaInQuery]
        @param = N'TV,OV,CK'

SELECT  'Return Value' = @return_value

But it's NOT WORKED!

Then I try it in sql query

SELECT TOP 100 [Model_No]
      ,[AppCode]
      ,[Model]
FROM [S_ModelMaster] where AppCode in ('TV','OV','CK')  

And it's work.So what is the correct way to pass parameter to IN query in C#?

Elshan
  • 7,339
  • 4
  • 71
  • 106
  • 2
    A string that *contains* single quote marks and commas is not at all the same thing as *multiple* strings. This is as true in T-SQL as it is in most languages. SQL Server has built in support for two data types that are *designed* for holding multiple values - tables and XML. So why force yourself to jump through hoops by passing a string that then has to be parsed and pulled apart to turn it into multiple values? – Damien_The_Unbeliever Sep 09 '15 at 08:41
  • Try this --> http://stackoverflow.com/a/1525153/2020893 – Karthik AMR Sep 09 '15 at 08:42
  • Seriously, this question is asked about once a week. It's not exactly the easiest thing to find, but there's already more than ten questions with accepted answers. – Luaan Sep 09 '15 at 08:48
  • @Luaan so can could you please give the link ?? – Elshan Sep 09 '15 at 08:56
  • Look at the duplicate link at the top of your question :) – Luaan Sep 09 '15 at 09:04

1 Answers1

1

The way i see around this is, Use table valued parameters and send parameter in datatable format from c#.

And in Stored procedures something like select * from TableName where AppCode in(select parameter from tvpTable)

This is similar

Table valued parameters

Community
  • 1
  • 1
Mandar Jogalekar
  • 3,199
  • 7
  • 44
  • 85