Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all
I've been trying to copy datatable data into sqlserver existing table.I've tried different approaches I found on the web but none of them works for my case :(
Here is the latest approach i used for which I get "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data .dll" error.

Your help is much appreciated

C#
string connectionString = "server=localhost;database=CPSdata;Trusted_Connection=True"; ;

            //Open a connection with destination database;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                using (SqlBulkCopy bulkcopy = new SqlBulkCopy(connection))
                {

                    bulkcopy.DestinationTableName = "dbo.climateRawDataTable";

                    try
                    {
                        bulkcopy.WriteToServer(dt);
                    }
                    catch (Exception ex)
                    {
                    }

                    connection.Close();
                }
            }
Posted

1 solution

Look at the exception - either in the debuggger, or by7 adding a try-catch block. The message and any inner exception details should help to track it. at the moment, we would be just guessing - you have given no where near enough information to solve the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900