Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
For "Name"(Line No 20)

In My Excel Sheet Data is Akshada,Shivani,Malvika,Rutu


But I want to store in data table like this

id     Name
1     Akshada
2     Shivani
3     Malvika
4     Rutu


// below code stores all same to same data from excel to database
sqlBulkCopy.ColumnMappings.Add("Name", "Name");
// I want to separate data for second row's Second column
// hear(Line No 23) I get all excel sheet data in dtExcelData
con.Open();
            sqlBulkCopy.WriteToServer(dtExcelData);
con.Close();


What I have tried:

1 conString = string.Format(conString, excelPath);
2    using (OleDbConnection excel_con = new OleDbConnection(conString))
3    {
4        excel_con.Open();
5        string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
6        DataTable dtExcelData = new DataTable();
 
        //[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
7        dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
8                new DataColumn("Name", typeof(string)),
9                new DataColumn("Salary",typeof(decimal)) });
 
10        using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
11        {
12            oda.Fill(dtExcelData);
13       }
14       excel_con.Close();
 
15       string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
16       using (SqlConnection con = new SqlConnection(consString))
        {
17           using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
            {
                //Set the database table name
18                sqlBulkCopy.DestinationTableName = "dbo.tblPersons";
 
                //[OPTIONAL]: Map the Excel columns with that of the database table
19                sqlBulkCopy.ColumnMappings.Add("Id", "PersonId");
20                sqlBulkCopy.ColumnMappings.Add("Name", "Name");
21                sqlBulkCopy.ColumnMappings.Add("Salary", "Salary");
22               con.Open();
23              sqlBulkCopy.WriteToServer(dtExcelData);
24             con.Close();
            }
        }
    }
Posted
Updated 7-Dec-17 19:33pm
Comments
CHill60 6-Dec-17 9:20am    
What is wrong with the code you have copied?
Try saving your excel data to CSV format with no quotes on text fields and see if that will load.
You could load the data to a staging table and use a Split[^] function to process it further.

1 solution

conString = string.Format(conString, excelPath);
        using (OleDbConnection excel_con = new OleDbConnection(conString))
        {
            excel_con.Open();
            string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
            DataTable dtExcelData = new DataTable();
            //[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
            dtExcelData.Columns.AddRange(new DataColumn[2]              
            {                
                new DataColumn("Id", typeof(int)),
                             new DataColumn("Name", typeof(string))   });
 
            using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
            {
                oda.Fill(dtExcelData);
            }
            excel_con.Close();
            string consString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            using (SqlConnection con = new SqlConnection(consString))
            {
                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                {
                    if (dtExcelData.Rows.Count > 0)
                    {
                        foreach (DataRow dRow in dtExcelData.Rows)
                        {
                            
                            objContactBO.id = dRow["id"].ToString();                            
                            objContactBO.Name = dRow["Name"].ToString();
                            
						
							
							// saperate data value
                            string strValue = objContactBO.Name;
                            string[] strArray = strValue.Split(',');
							// insert data in new column
                            foreach (String Name in strArray)
                            {
                                objContactBO.Name = dt.Rows[0]["Name"].ToString();
								//insert for second Name in different column on same id
                                result2 = objContactBL.Insert_ContactInDiffColumName(objContactBO);
                            }
                        }
                    }
                }
            }
        }
 
Share this answer
 

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