C# insert list into database

is there a way to insert a list into a database
LVL 1
DB_FuryAsked:
Who is Participating?
 
edemcsConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.