Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. SQLBulkCopy & Adding values [modified]

SQLBulkCopy & Adding values [modified]

Scheduled Pinned Locked Moved ASP.NET
databasehelpsql-serversysadmin
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    munklefish
    wrote on last edited by
    #1

    Hi, I am using the following code to take data from a csv file then adding it to an SQL database using SQLBulkCopy. I need to add an additional column of data to each record transfered. This extra column is present in the SQL2005 database but not the CSV file, and relates to the users unique ID. At present i have this:

                using (OleDbCommand dcmd = new OleDbCommand("SELECT \* FROM " + strUPloadFile + "", dconn))
                {
                    try
                    {
                        dconn.Open();
    
                        using (OleDbDataReader dreader = dcmd.ExecuteReader())
                        {
    
                            // Bulk Copy to SQL Server
                            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(WebConfigurationManager.ConnectionStrings\["######"\].ConnectionString))
                            {
                                //bulkCopy.ColumnMappings.Add("adds\_ABMId", 45);
                                bulkCopy.ColumnMappings.Add("adds\_Email", "E-mail Address");
                                bulkCopy.ColumnMappings.Add("adds\_RecipientForename", "First Name");
                                bulkCopy.ColumnMappings.Add("adds\_RecipientSurname", "Last Name");
    
                                bulkCopy.DestinationTableName = "tbl\_AddressBookAddresses";
    
                                // Set the BatchSize.
                                bulkCopy.BatchSize = 500;
                                try
                                {//perform copy
                                    bulkCopy.WriteToServer(dreader);
                                }
                                catch (Exception ex)
                                {//error
                                    throw(ex);
                                }
                            }
    
                        }
                    }
                    finally
                    {
                        dconn.Close();
                    }
                }
            }
    

    I could really do with some help on this. Thanks guys! :confused::confused::confused::confused::confused:

    modified on Tuesday, June 16, 2009 6:49 AM

    H 1 Reply Last reply
    0
    • M munklefish

      Hi, I am using the following code to take data from a csv file then adding it to an SQL database using SQLBulkCopy. I need to add an additional column of data to each record transfered. This extra column is present in the SQL2005 database but not the CSV file, and relates to the users unique ID. At present i have this:

                  using (OleDbCommand dcmd = new OleDbCommand("SELECT \* FROM " + strUPloadFile + "", dconn))
                  {
                      try
                      {
                          dconn.Open();
      
                          using (OleDbDataReader dreader = dcmd.ExecuteReader())
                          {
      
                              // Bulk Copy to SQL Server
                              using (SqlBulkCopy bulkCopy = new SqlBulkCopy(WebConfigurationManager.ConnectionStrings\["######"\].ConnectionString))
                              {
                                  //bulkCopy.ColumnMappings.Add("adds\_ABMId", 45);
                                  bulkCopy.ColumnMappings.Add("adds\_Email", "E-mail Address");
                                  bulkCopy.ColumnMappings.Add("adds\_RecipientForename", "First Name");
                                  bulkCopy.ColumnMappings.Add("adds\_RecipientSurname", "Last Name");
      
                                  bulkCopy.DestinationTableName = "tbl\_AddressBookAddresses";
      
                                  // Set the BatchSize.
                                  bulkCopy.BatchSize = 500;
                                  try
                                  {//perform copy
                                      bulkCopy.WriteToServer(dreader);
                                  }
                                  catch (Exception ex)
                                  {//error
                                      throw(ex);
                                  }
                              }
      
                          }
                      }
                      finally
                      {
                          dconn.Close();
                      }
                  }
              }
      

      I could really do with some help on this. Thanks guys! :confused::confused::confused::confused::confused:

      modified on Tuesday, June 16, 2009 6:49 AM

      H Offline
      H Offline
      himanshu2561
      wrote on last edited by
      #2

      Add column this way

      string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
      DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
      DbDataAdapter adapter = factory.CreateDataAdapter();
      DbCommand selectCommand = factory.CreateCommand();
      selectCommand.CommandText = "SELECT Fname FROM [Sheet1$]";
      DbConnection connection = factory.CreateConnection();
      connection.ConnectionString = connectionString;
      selectCommand.Connection = connection;
      adapter.SelectCommand = selectCommand;
      DataTable dt = new DataTable();
      adapter.Fill(dt);
      DataColumn dc = new DataColumn();
      dc.ColumnName = "Lname";
      dt.Columns.Add(dc);
      foreach (DataRow dr in dt.Rows)
      {
      dr["Lname"] = "LastName";
      }

      himanshu

      M 1 Reply Last reply
      0
      • H himanshu2561

        Add column this way

        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
        DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
        DbDataAdapter adapter = factory.CreateDataAdapter();
        DbCommand selectCommand = factory.CreateCommand();
        selectCommand.CommandText = "SELECT Fname FROM [Sheet1$]";
        DbConnection connection = factory.CreateConnection();
        connection.ConnectionString = connectionString;
        selectCommand.Connection = connection;
        adapter.SelectCommand = selectCommand;
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        DataColumn dc = new DataColumn();
        dc.ColumnName = "Lname";
        dt.Columns.Add(dc);
        foreach (DataRow dr in dt.Rows)
        {
        dr["Lname"] = "LastName";
        }

        himanshu

        M Offline
        M Offline
        munklefish
        wrote on last edited by
        #3

        Himanshu, I see this creates a data table, which isnt what i really wanted to do. Thanks for the suggestion, but how do i then extend this code to perform the SQLBulkCopy over to the server???? Thanks.

        H 1 Reply Last reply
        0
        • M munklefish

          Himanshu, I see this creates a data table, which isnt what i really wanted to do. Thanks for the suggestion, but how do i then extend this code to perform the SQLBulkCopy over to the server???? Thanks.

          H Offline
          H Offline
          himanshu2561
          wrote on last edited by
          #4

          Hi, i dont think there is any way to add columns to datareader. try this

          string connectionString1 = @"Data Source='local\SQLEXPRESS';Integrated Security=True;Pooling=False;Initial Catalog='MASTER'";

                  string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
                  OleDbConnection con=new OleDbConnection(connectionString);
                  using (OleDbCommand dcmd = new OleDbCommand("SELECT Fname FROM \[Sheet1$\]",con))
                      {
                          try
                          {
                              con.Open();
          
                              using (OleDbDataReader dreader = dcmd.ExecuteReader())
                              {
                                  DataRow myNewRow;
                                  DataRow\[\] drcol ;
                                  DataTable dt = new DataTable();
                                  dt.Columns.Add("Fname");
                                  dt.Columns.Add("Lname");
                                  myNewRow=dt.NewRow();
                              
                                   while (dreader.Read())
                                   {
                                       myNewRow\[0\] = dreader.GetValue(0).ToString();
                                       myNewRow\[1\] = "Test123";
                                       dt.Rows.Add(myNewRow);
                                       
                                   }
                                  drcol = dt.Select();
                                  using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString1))
                                  {
                                      bulkCopy.ColumnMappings.Add("Fname", "FirstName");
                                      bulkCopy.ColumnMappings.Add("Lname", "LastName");
                                      bulkCopy.DestinationTableName = "T\_EmpDetail";
                                      bulkCopy.BatchSize = 500;
                                      bulkCopy.WriteToServer(drcol);
                                      
                                  }
          
                              }
                          }
                          finally
                          {
                              con.Close();
                          }
                      }
          

          himanshu

          M 1 Reply Last reply
          0
          • H himanshu2561

            Hi, i dont think there is any way to add columns to datareader. try this

            string connectionString1 = @"Data Source='local\SQLEXPRESS';Integrated Security=True;Pooling=False;Initial Catalog='MASTER'";

                    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
                    OleDbConnection con=new OleDbConnection(connectionString);
                    using (OleDbCommand dcmd = new OleDbCommand("SELECT Fname FROM \[Sheet1$\]",con))
                        {
                            try
                            {
                                con.Open();
            
                                using (OleDbDataReader dreader = dcmd.ExecuteReader())
                                {
                                    DataRow myNewRow;
                                    DataRow\[\] drcol ;
                                    DataTable dt = new DataTable();
                                    dt.Columns.Add("Fname");
                                    dt.Columns.Add("Lname");
                                    myNewRow=dt.NewRow();
                                
                                     while (dreader.Read())
                                     {
                                         myNewRow\[0\] = dreader.GetValue(0).ToString();
                                         myNewRow\[1\] = "Test123";
                                         dt.Rows.Add(myNewRow);
                                         
                                     }
                                    drcol = dt.Select();
                                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString1))
                                    {
                                        bulkCopy.ColumnMappings.Add("Fname", "FirstName");
                                        bulkCopy.ColumnMappings.Add("Lname", "LastName");
                                        bulkCopy.DestinationTableName = "T\_EmpDetail";
                                        bulkCopy.BatchSize = 500;
                                        bulkCopy.WriteToServer(drcol);
                                        
                                    }
            
                                }
                            }
                            finally
                            {
                                con.Close();
                            }
                        }
            

            himanshu

            M Offline
            M Offline
            munklefish
            wrote on last edited by
            #5

            I managed to come up with a very similar solution to this but a little simpler since i only need to add a column and populate it, rather than adding new rows. Ill post the details later. Thanks again for all the help everyone!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups