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. General Programming
  3. C#
  4. SqlBulkCopy

SqlBulkCopy

Scheduled Pinned Locked Moved C#
helpdatabasequestionworkspace
4 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.
  • E Offline
    E Offline
    Expert Coming
    wrote on last edited by
    #1

    Not sure if it is a Sql issue or a syntax error of sorts so here it is. I have a DataTable with a single column Player Name. It is filled with some 20,000 rows. I need to insert this into a Sql database. I have a Sql database setup with a table called Unknown, it has 2 columns, an Indentity column ID, and a varchar(50) column Player Name. I copied the data over one-by-one and it took like 15-30 minutes to do, not I am using SqlBulkCopy. The problem is, it simply isn't working. No error, no warnings, no nothing. I have SQL Profiler open and don't see it even hitting the database. Code is attached, what am I doing wrong?

    SqlConnection connection = new SqlConnection(Properties.Settings.Default.TibiaSQLConnection);

            connection.Open();
    
            SqlBulkCopy copier = new SqlBulkCopy(connection);
            copier.BatchSize = 500;
            copier.NotifyAfter = 1000;
            copier.SqlRowsCopied += new SqlRowsCopiedEventHandler(copier\_SqlRowsCopied);
            copier.DestinationTableName = "Unknown";
            copier.WriteToServer(dtUnknowns);
    
            copier.Close();
            connection.Close();
    

    The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

    C 1 Reply Last reply
    0
    • E Expert Coming

      Not sure if it is a Sql issue or a syntax error of sorts so here it is. I have a DataTable with a single column Player Name. It is filled with some 20,000 rows. I need to insert this into a Sql database. I have a Sql database setup with a table called Unknown, it has 2 columns, an Indentity column ID, and a varchar(50) column Player Name. I copied the data over one-by-one and it took like 15-30 minutes to do, not I am using SqlBulkCopy. The problem is, it simply isn't working. No error, no warnings, no nothing. I have SQL Profiler open and don't see it even hitting the database. Code is attached, what am I doing wrong?

      SqlConnection connection = new SqlConnection(Properties.Settings.Default.TibiaSQLConnection);

              connection.Open();
      
              SqlBulkCopy copier = new SqlBulkCopy(connection);
              copier.BatchSize = 500;
              copier.NotifyAfter = 1000;
              copier.SqlRowsCopied += new SqlRowsCopiedEventHandler(copier\_SqlRowsCopied);
              copier.DestinationTableName = "Unknown";
              copier.WriteToServer(dtUnknowns);
      
              copier.Close();
              connection.Close();
      

      The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

      C Offline
      C Offline
      CodingYoshi
      wrote on last edited by
      #2

      copier.ColumnMappings.Add(sourceCol, DestColumn); Just a note: I would open the connection right before WriteToServer() method and wrap it in a try block and catch exceptions

      E 2 Replies Last reply
      0
      • C CodingYoshi

        copier.ColumnMappings.Add(sourceCol, DestColumn); Just a note: I would open the connection right before WriteToServer() method and wrap it in a try block and catch exceptions

        E Offline
        E Offline
        Expert Coming
        wrote on last edited by
        #3

        It all is wrapped in a try except. That was just a snippit. Let me try that and see how it works. Do you have to do that even when both columns have the same name?

        The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

        1 Reply Last reply
        0
        • C CodingYoshi

          copier.ColumnMappings.Add(sourceCol, DestColumn); Just a note: I would open the connection right before WriteToServer() method and wrap it in a try block and catch exceptions

          E Offline
          E Offline
          Expert Coming
          wrote on last edited by
          #4

          That worked great, thanks a ton.

          The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo

          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