RD Design

RD Design

  • NA
  • 25
  • 26.7k

How to pass value from string array to Select statement?

Feb 23 2015 5:34 AM
Hi,

I have a List and there I have stored customerNo from database like this

                    List<string[]> _customerDetails = new List<string[]>();
                    while (readerCus.Read())
                    {
                        string[] fields = new string[2];
                        fields[0] = readerCus["Customer"].ToString();
                        _customerDetails.Add(fields);
                    }

Inside a foreach I need to get this "Customer" no one by one and pass it to SQL select statement.

foreach (string[] fields in _customerDetails)
                    {
                        string cusId = fields[0];
  using (SqlConnection con2 = new SqlConnection(connectionString))
                        {
                            con2.Open();
                            using (SqlCommand command2 = new SqlCommand("SELECT * FROM dbo.Customers WHERE Customer = '" + fields[0].ToString() + "'", con2))
                            using (SqlDataReader reader2 = command2.ExecuteReader())
                            {
 
                                while (reader2.Read())
                                {
                                        //My code and here I am generating a token for customer
                                }
                            }
                         }
                      }

My issue is all the time it runs one time and generate a token.Not continuing and not completing all available customers (say 1000 customers).Any one can point me how can I loop through all customers one by one.

Thank



Answers (3)