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. SQL BulkCopy

SQL BulkCopy

Scheduled Pinned Locked Moved ASP.NET
databasesql-servercomsysadminsecurity
2 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 need to import data from a CSV file to an SQL table i have used tohe following in the past to import an excel file but wondered if/how i can adapt it to import from a CSV. Also, it would be a great help if you could show me how to convert it to use column mapping.

    string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\qs_upload.xls;Extended Properties=""Excel 8.0;HDR=YES;""";

        // Create Connection to Excel Workbook
        using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
        {
            OleDbCommand command = new OleDbCommand("Select \* FROM \[quality\_standard$\]", connection);
            //OleDbCommand command = new OleDbCommand("Select qs\_Id, qs\_Organisation, Convert(varchar,qs\_Start,111), qs\_End FROM \[quality\_standard$\]", connection);
    
            connection.Open();
    
            // Create DbDataReader to Data Worksheet
            using (OleDbDataReader dr = command.ExecuteReader())
            {
                // SQL Server Connection String
                string sqlConnectionString = "Data Source=wibblewobble.com;Initial Catalog=comfound;Persist Security Info=True;User ID=##########;Password=##########";
    
                // Bulk Copy to SQL Server
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                {
                    bulkCopy.DestinationTableName = "tbl\_QualityStandard";
                    bulkCopy.WriteToServer(dr);
                }
            }
        }
    

    Thanks.

    H 1 Reply Last reply
    0
    • M munklefish

      Hi, I need to import data from a CSV file to an SQL table i have used tohe following in the past to import an excel file but wondered if/how i can adapt it to import from a CSV. Also, it would be a great help if you could show me how to convert it to use column mapping.

      string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\qs_upload.xls;Extended Properties=""Excel 8.0;HDR=YES;""";

          // Create Connection to Excel Workbook
          using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
          {
              OleDbCommand command = new OleDbCommand("Select \* FROM \[quality\_standard$\]", connection);
              //OleDbCommand command = new OleDbCommand("Select qs\_Id, qs\_Organisation, Convert(varchar,qs\_Start,111), qs\_End FROM \[quality\_standard$\]", connection);
      
              connection.Open();
      
              // Create DbDataReader to Data Worksheet
              using (OleDbDataReader dr = command.ExecuteReader())
              {
                  // SQL Server Connection String
                  string sqlConnectionString = "Data Source=wibblewobble.com;Initial Catalog=comfound;Persist Security Info=True;User ID=##########;Password=##########";
      
                  // Bulk Copy to SQL Server
                  using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                  {
                      bulkCopy.DestinationTableName = "tbl\_QualityStandard";
                      bulkCopy.WriteToServer(dr);
                  }
              }
          }
      

      Thanks.

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

      Your question is not properly visible.please post it properly.

      himanshu

      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