Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 551979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:27:05+00:00 2026-05-13T11:27:05+00:00

running sqlbulkcopy in c# and I get an error: WriteToServer: Connection property has not

  • 0

running sqlbulkcopy in c# and I get an error:
WriteToServer: Connection property has not been initialized.

it happens at the WriteToServer command. The connection is open.

using (SqlBulkCopy s = new SqlBulkCopy(conn))
{
  foreach (DataTable dt in ds.Tables)
  {

    s.DestinationTableName = "tmp_" + dt.TableName;
    s.NotifyAfter = 5000;
    s.SqlRowsCopied += new SqlRowsCopiedEventHandler(s_SqlRowsCopied);
    s.WriteToServer(dt);
    s.Close();
  }
}

Correct Code:

using (SqlBulkCopy s = new SqlBulkCopy(conn))
{
  foreach (DataTable dt in ds.Tables)
  {
    s.DestinationTableName = "tmp_" + dt.TableName;
    s.NotifyAfter = 5000;
    s.SqlRowsCopied += new SqlRowsCopiedEventHandler(s_SqlRowsCopied);
    s.WriteToServer(dt);
  }
  s.Close();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:27:05+00:00Added an answer on May 13, 2026 at 11:27 am

    At a glance I would guess that the first pass through the foreach loop is executing correctly, then s.Close(); is cleaning up the SqlBulkCopy instance and clearing its Connection property, thus creating the exception on the second pass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Running Nh 3.2 using SQLite throws the following exception: ---> NHibernate.HibernateException: Could not create
Running into a problem where on certain servers we get an error that the
Running command manually: git log --pretty=format:%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s --date=short Using Bash 6bb9849 2012-08-05
Running a Selenium 2 RemoteWebDriver server using java -jar selenium-server-standalone-2.15.0.jar . I always get
Running Puppet v2.7.14 on CEntOs 6 and also using Apache/Passenger instead of WEBrick. I
Running the following line in my script gives me a Permission denied error: $config_out
Running SQL Server 2008 (not R2). I have a few reports that have URLs
I'm using SQLBULKCOPY to copy some data-tables into a database table, however, because the
I'm using SqlClient.SqlBulkCopy to try and bulk copy a csv file into a database.
Running Windows PHP 4.4.2 \cli\php.exe fails with an error Cannot find php4ts.dll if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.