Link to home
Start Free TrialLog in
Avatar of DB_Fury
DB_Fury

asked on

C# insert list into database

is there a way to insert a list into a database
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Could you be more specific.
Avatar of DB_Fury
DB_Fury

ASKER

well first of all i want to get a key, value list or array.  what do i do to get that.  then i want to insert each of those into the database with one call to the db......i barly know what i talking about here
Yes, serialize the list to an xml string and pass that into the SP using an XML variable in the database.  Then simply loop through the XML in the SP.
Avatar of DB_Fury

ASKER

cool thanks, edemcs.  since i want a key and value pair should i be using a dictonary or can this be accomplished with a list?
ASKER CERTIFIED SOLUTION
Avatar of edemcs
edemcs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't know if that's what you need, but you can use a procedure to do a SELECT, and do an INSERT with the return of the SELECT, like:

INSERT INTO TB_YOURTABLE(SELECT TOP 10 FROM TB_ANOTHERTABLE) -- sql server example

or

build your list inside the application and send it to the db using a loop of insert, like:
//C# example
List<string> list = new List<string>();
list.add("test");

for each(string val in list)
{
   _yourDLL.Insert(val);
}

Hope it helps.
Just note - what edemcs suggests is NOT supported by all databases.  Also you need to know yourself what you require else it is going to go downhill rapidly
Good point, Andy.  I made the assumption that you're using SQL Server...
Avatar of DB_Fury

ASKER

I am using SQL server.  question though:  i have about 60 checkboxes on my page.  i cant use a checkbox list becuse i am showing and hiding divs that contain more detailed checkboxes.  i dont really want to pass each checkbox as a parameter, so do you guys thinkn what edemcs has suggestion is the best way to accomplish what i want to do.  or should i just pass all the checkboxes as a parameter
Avatar of DB_Fury

ASKER

am i just making this to hard on myself???? I am trying to make my application the best it can be, i would normally just make each checkbox a parameter and pass it to a stored procedure, but there has got to be a better way
I personally wouldn't do it that way because you'll have to add or remove parameters as your list changes.  If you did it my way, it would be self-sufficient with an input parameter that would never have to be modified.
Avatar of DB_Fury

ASKER

exactly what i was looking for