Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

C# SQL BulkCopy class create SQL table if not there

Hi
       I use the following code to upload data from a data table to a SQL table calling the Bulk Copy class
       If the SQL table is not there then I want to automatically create it. How do I update my code
      to do this?


     DataTable tableDataTable = oDataSet.Tables[0];

            string conString = "Server = localhost; Database = Test1; Trusted_Connection = Yes;";
            using (SqlConnection con = new SqlConnection(conString))
            {

                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                {
                    Create_Table();
                    sqlBulkCopy.DestinationTableName = "MTEST";
                    sqlBulkCopy.BulkCopyTimeout = 0;
                    //Indefinite when zero

                    con.Open();
                    sqlBulkCopy.WriteToServer(tableDataTable);
                    con.Close();
                }
            }
ASKER CERTIFIED SOLUTION
Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What the Create_Table() does?
Avatar of Murray Brown

ASKER

That is code to create a table using a SQL statement that I used to test alternatives. Please ignore it in this post
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you both