C# insert list into database

is there a way to insert a list into a database
LVL 1
DB_FuryAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Could you be more specific.
0
DB_FuryAuthor Commented:
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
0
edemcsCommented:
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.
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

DB_FuryAuthor Commented:
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?
0
edemcsCommented:
I would create a generic list with a class that you create.  i.e. List(Of MyObject)

public class MyObject{
  string key;
  string value;
}

Then you simply deserialize the list and you know what your xml will look like.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tbsolutionsCommented:
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.
0
AndyAinscowFreelance programmer / ConsultantCommented:
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
0
edemcsCommented:
Good point, Andy.  I made the assumption that you're using SQL Server...
0
DB_FuryAuthor Commented:
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
0
DB_FuryAuthor Commented:
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
0
edemcsCommented:
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.
0
DB_FuryAuthor Commented:
exactly what i was looking for
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.