zzzprojects / Bulk-Operations

C# SQL Bulk Operations | High-performance C# bulk insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL, and SQLite.
https://bulk-operations.net
142 stars 34 forks source link

An error occured while retrieving the InformationSchemaTable information #67

Closed Bea1906 closed 3 years ago

Bea1906 commented 3 years ago

I am developing a tool to migrate data and I am using your Z.BulkOperations to insert data from source to destiny. The problem is that when I perform the bulk.BulkInsert(dataTable) I am getting this error : “An error occured while retrieving the InformationSchemaTable information”. I do not know why this happens, but in my code I am only adding the columns to the data Table that are also in the destiny schema. The part of my code where I do this is before filling the DataTable dt : (The code passes here with no problems)

                        DataTable dtSchema = reader.GetSchemaTable();
                        DataTable dt = new DataTable();

                        SqlCommand targetSchema = new SqlCommand("SELECT * FROM "+TargetPhysicalTableName, destiny);
                        SqlDataReader targetSchemaReader = targetSchema.ExecuteReader(CommandBehavior.SchemaOnly);
                        DataTable dtSchemaTarget = targetSchemaReader.GetSchemaTable();

                        if (dtSchema != null && dtSchemaTarget.Rows.Count >0)
                        {

                            foreach (DataRow drow in dtSchema.Rows)
                            {
                                string columnName = System.Convert.ToString(drow["ColumnName"]);
                                DataColumn column = new DataColumn(columnName, (Type)(drow["DataType"]));

                                DataRow r = dtSchemaTarget.Select("ColumnName = '" + columnName + "'").Single();

                                if (r!=null) //if the columns exists in the destiny 
                                {

                                    dt.Columns.Add(column);

                                    if (!columnName.Equals("ID"))
                                    {
                                        bulk.ColumnMappings.Add(columnName, columnName);

                                    }

                                }

                            }

                        }

Then after add the data to the dataTable I perform the bulk.BulkInsert(dt) that is where the error occurs Do you know why the error occurs and how can I solve it?

JonathanMagnan commented 3 years ago

Hello @Bea1906 ,

This exception should have an inner exception, is it possible to get the inner exception message as well?

That will for sure give us some information to better understand what is happening in your case.

Best Regards,

Jon

Bea1906 commented 3 years ago

Hello Jonathan, I am using the Visual Studio as an OutSystems extension, it works the same but is harder to debug, I found the inner Exception:

System.Exception: An error occured while retrieving the InformationSchemaTable information. See the inner exception for details. ---> System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at .(DbCommand , CommandBehavior , BulkOperation , Int32 ) at .(Boolean , String[] ) at Z.BulkOperations.BulkOperation.() --- End of inner exception stack trace --- at Z.BulkOperations.BulkOperation.() at Z.BulkOperations.BulkOperation.Execute() at OutSystems.NssBirdlyData.CssBirdlyData.MssMigrate(RCMigrationConfigRecord ssConfig, Boolean& ssIsSuccess, String& sserrorMessage)

Bea1906 commented 3 years ago

Hello JonathanMagnan , using this Visual studio version connected to Outsystems is hard to debug but since I could print the innerException I knew what the error was and it is fixed. I want to thank you for the help, even if it was just to print the inner exception, you reply really fast and I am really thankful for that !

JonathanMagnan commented 3 years ago

Awesome ;)

No problem, I'm glad you find out about this issue.

Have a great weekend,

Best Regards,

Jon