locked
insert an array values into table at same time? RRS feed

  • Question

  • Hi every one

                 I wann write code to do this

    I have list of food , I want to insert this to sql database 2008 from web site C#.net 2010 at same time

    for example if the user add three kind of food so I have to add this to table food has 2 column id and food'name

    so , Please could you give idea

    I tried to add this list to array after that take it one by one .

    but it is not good idea. close and open connection many times.

     

    thank you

     


    Thanks
    Wednesday, March 30, 2011 1:21 AM

Answers

  • I found the solution for this

    ArrayList AList = new ArrayList();
        if (Chapple.checked == true)
          AList.Add("Apple");
        if (ChOrang.checked== true)
          AList.Add("Oranges");
    
    int id = 10;
    
    
    
     string sql = "insert into Foodtbll (ID,food_name)values ";
    
        int d = AList.Count;
        int i=0;
        foreach (string obj in AList)
        {
          i = i + 1;
          sql = sql + "(" + ID + ",'" + obj + "' )";
          if (i < d)
            sql = sql + ",";
        }

    Thanks
    • Marked as answer by Rawad Ebn MaM Wednesday, March 30, 2011 3:26 AM
    Wednesday, March 30, 2011 3:26 AM

All replies