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 7440763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:54:09+00:00 2026-05-29T10:54:09+00:00

I am using SqlBulkCopy object to write a datatable into an sql server table.

  • 0

I am using SqlBulkCopy object to write a datatable into an sql server table. However, everytime I recheck my database it remains intact with no changes. A

I have tried to do Google search to determine my problem but i am unable to resolve it.

The datatable came from an .xls file.

public static DataTable dt = new DataTable();

private void ExportToGrid(String path, String filen)
    {
        int idx = filen.IndexOf(".");
        string tf = filen.Remove(idx, 4);

        OleDbConnection MyConnection = null;
        DataSet DtSet = null;
        OleDbDataAdapter MyCommand = null;
        MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties=Excel 8.0;");

        ArrayList TblName = new ArrayList();

        MyConnection.Open();
        DataTable schemaTable = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
        foreach (DataRow row in schemaTable.Rows)
        {
            TblName.Add(row["TABLE_NAME"]);
        }

        MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + TblName[0].ToString() + "]", MyConnection);
        DtSet = new System.Data.DataSet();

        MyCommand.Fill(DtSet);
        MyCommand.FillSchema(DtSet, SchemaType.Source);

        DataTable dt = new DataTable();
        dt = DtSet.Tables[0];
        Session["dt"] = dt;
        int x = dt.Rows.Count;
        MyConnection.Close();

        if (dt.Rows.Count > 0)
        {
            theGridView.DataSource = dt;
            theGridView.DataBind();
        }

        if (System.IO.File.Exists(path))
        {
            System.IO.File.Delete(path);
        }

    }

This is my writer function

private void StartImport()
    {
        string servername = server;
        string database = database;
        string tbl = "dbo.LinkDb";

        Stopwatch sw = new Stopwatch();
        sw.Start();
        SqlBulkCopy bulkCopy = new SqlBulkCopy("Data Source=" + servername + ";Initial Catalog=" + database + ";Integrated Security=SSPI", SqlBulkCopyOptions.TableLock);
        bulkCopy.DestinationTableName = tbl;
        bulkCopy.WriteToServer(dt);
        sw.Stop();
        lblResult.Visible = true;
        lblResult.Text = (sw.ElapsedMilliseconds / 1000.00).ToString();
    }

Below are the screenshot of the tables stored in my sql server. I assure you that I have been complying to Case Sensitive rules.

Tables

There was no exception thrown and average time elapsed is 0.018 – 0.020 secs

Appreciate any helps.

Thank you

  • 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-29T10:54:09+00:00Added an answer on May 29, 2026 at 10:54 am

    Based on the code you have posted, you are writing an empty datatable to the database. Your “ExportToGrid” method fills dt, a DataTable declared locally, which loses scope outside of the method. Your write function is calling the static DataTable dt which is a new datatable.

    Does dt need to be static? it seems as though this could be declared as

    private DataTable dt;
    

    then inside “ExportToGrid” instead of declaring another DataTable just instantiate the already declared dt rather than declaring a new one

    dt = new DataTable();
    

    Alternatively you could extract the DataTable straight from the GridView during the write Method:

    DataTable dt = (DataTable)theGridView.DataSource;
    bulkCopy.WriteToServer(dt);
    

    This removes the need for variables outside of the scope of the method.

    Lastly since you are storing your datatable within the session (I am not generally an advocate of storing large amounts of data in session variables but without knowing the specifics of your site I cannot really pass judgement), you could use the following:

    DataTable dt = (DataTable)Session["dt"];
    bulkCopy.WriteToServer(dt);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQLBULKCOPY to copy some data-tables into a database table, however, because the
I'm using SqlBulkCopy to insert/update from a .net DataTable object to a SQL Server
i have two datatables that i am inserting into sql server 2008 using SqlBulkCopy
I'm exporting data programatically from Excel to SQL Server 2005 using SqlBulkCopy. It works
I am using the SqlBulkCopy object to insert a couple million generated rows into
I'm trying to create a table using SMO, and further use the SqlBulkCopy object
Using SQL Server 2005 Leave Table ID StartDate EndDate 001 02/03/2010 02/03/2010 002 02/03/2010
I'm trying to copy tables form a MS Access database into a SQL Server
I'm using SqlBulkCopy to bulk insert some records from one table into another table.
If Im using SqlBulkCopy in .net 4.0 on a sql 2008 R2 database to

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.