Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLBulkCopy doesn't fail, but doesn't Insert

Tags:

asp.net

So I have the following code to take a Excel file and write it to a table in my DB.

        string target = Server.MapPath("~/Upload");
        if (fupStation.HasFile)
        {
            fupStation.SaveAs(System.IO.Path.Combine(target, fupStation.FileName));

            string connectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", target + "\\" + fupStation.FileName); string query = String.Format("select * from [{0}$]", "Client Station Assignments");
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);

            DataSet dsStation = new DataSet();
            dataAdapter.Fill(dsStation);

            DataTable dtStation = new DataTable();
            dtStation = dsStation.Tables[0];

            gvOne.DataSource = dtStation;
            gvOne.DataBind();

            using (SqlBulkCopy s = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["cnSQL"].ConnectionString))
            {
                s.DestinationTableName = "StationImport";
                s.NotifyAfter = 100;
                s.WriteToServer(dtStation);
                s.Close();
            }

        }

So Here is the issue. It reads the file and populate the Datatable just fine (proven by populating the Grid with it) And I know the SQLBulkCopy code is being called, but in the end it gives no errors, but nothing shows up in my table!

Anyone been thru this before? This is my first time using BulkCopy (and reading files too!) so I wouldn't be suprized if I am doing soemthing wrong.

Thanks

like image 610
Limey Avatar asked May 29 '26 02:05

Limey


1 Answers

Do all the columns map correctly? "the copy will not succeed if there are any mismatched columns between the two." (source http://www.sqlteam.com/article/use-sqlbulkcopy-to-quickly-load-data-from-your-client-to-sql-server)

like image 184
Kevin Main Avatar answered May 31 '26 20:05

Kevin Main