Click here to Skip to main content
15,884,913 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi all,

I am new to asp.net,Can help any body help me on how to insert list items to database..

Thanks
Posted
Comments
Karthik_Mahalingam 5-Jan-14 11:47am    
not clear
pls provide more information..
xyz from Bangalore 5-Jan-14 12:26pm    
Thanks for your reply.I have list of items. I need to insert those items to database table.
Maciej Los 5-Jan-14 13:15pm    
Sorry, but this is not informative at all... ;(
Karthik_Mahalingam 6-Jan-14 0:01am    
pls be specific to what you are asking...
xyz from Bangalore 6-Jan-14 0:05am    
I have generic list,I need to insert that to database. and bulk insert.

I am not sure what your issue is - are you having a problem designing the table?
You cannot store list items in the table directly.
You need to store selected list items one by one in a 'normalized' manner.
For e.g. if your list is
A
B
C

Your table would probably look like
1 A
2 B
3 C

Here the number is the primary key.

If you are just having a problem writing code to save data, then this[^] might help you.
 
Share this answer
 
v2
Comments
Maciej Los 5-Jan-14 12:18pm    
I'm not sure i understand you well ;(
Can you improve your answer to post more details?
Abhinav S 5-Jan-14 12:36pm    
I have tried explaining data storage for list data. I'm not sure if that is OP's problem.
Maciej Los 5-Jan-14 12:57pm    
Link you provided is OK, but i'm affraid that OP not be able to use it without step by step guide ;)
Check the following links on how to Bulk Insert with Generic List collections.

How to bulk insert efficiently[^]

http://ruijarimba.wordpress.com/2012/03/25/bulk-insert-dot-net-applications-part1/[^]

How to bulk insert efficiently[^]

Hope this helps you...
 
Share this answer
 
Try like this,,

modify as per your needs, this will help you...

using System.Collections.Generic;
using System.Data.SqlClient;



class Program
{
    static void Main(string[] args)
    {
        List<Entity> list = new List<Entity>();
        list.Add(new Entity() { ID = 1, Name = "karthik", Address = "Bangalore" });
        list.Add(new Entity() { ID = 1, Name = "Parthip", Address = "Ahmdabad" });
        list.Add(new Entity() { ID = 1, Name = "Krishna", Address = "Delhi" });
        string conn = @"Data Source=.;Initial Catalog=databasename;Integrated Security=True;";
        foreach (Entity item in list)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SqlConnection objConn = new SqlConnection();
            objConn.ConnectionString = conn;
            objConn.Open();
            sqlCmd.Connection = objConn;
            sqlCmd.CommandTimeout = 30;
            string values = string.Format("({0},'{1}','{2}')", item.ID, item.Name, item.Address);
            // for string datatypes u have to use '{}'  , for number datatype use {}
            sqlCmd.CommandText = "insert into tablename (ID,Name,Address,Column1,Column2....) values  " + values;
            sqlCmd.ExecuteNonQuery();
        }
    }
    class Entity
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }

    }
}
 
Share this answer
 
Comments
xyz from Bangalore 6-Jan-14 10:44am    
Hi, thanks for ur reply. I have to do bulk insert..I am binding gridview data to list. I have use same list for inserting data. how can i do it?
Karthik_Mahalingam 6-Jan-14 19:48pm    
yes u can use the same list to bind data to gridview..
Karthik_Mahalingam 6-Jan-14 19:50pm    
check this link for bulk insert..
http://www.morgantechspace.com/2013/08/bulk-insert-records-into-sql-server.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900