简体   繁体   中英

Transfer from MsSQL to MySQL error

Im trying to transfer data between databases (from MsSQL Server to MySQL). Source database has ~2700 records. And when im executing program, every 400-600 record VS throws an exception:

Invalid attempt to call IsDBNull when reader is closed.

Code:

foreach (var product in products)
        {
            var prd = product;

            var cmd = connection.CreateCommand();

            cmd.CommandText =
                "INSERT INTO Product(CategoryID, Position, PresentItemID, Guid, ThumbX, ThumbY, " +
                "ImgX, ImgY, Name, Description, IsAvailable, TotalStock, Price, ListPrice, Size, " +
                "meta_tag, desc_tag, page_metatags, FreeDescription, IsVintage, OnSale, Valentine)" +
                "VALUES(@CategoryID, @Position, @PresentItemID, @Guid, @ThumbX, @ThumbY, " +
                "@ImgX, @ImgY, @Name, @Description, @IsAvailable, @TotalStock, @Price, @ListPrice, @Size, " +
                "@meta_tag, @desc_tag, @page_metatags, @FreeDescription, @IsVintage, @OnSale, @Valentine)";

            cmd.Parameters.AddWithValue("@CategoryID", prd.CategoryID);
            cmd.Parameters.AddWithValue("@Position", prd.Position);
            cmd.Parameters.AddWithValue("@PresentItemID", prd.PresentItemID);
            cmd.Parameters.AddWithValue("@Guid", prd.Guid);
            ...
            cmd.Parameters.AddWithValue("@IsVintage", prd.IsVintage);
            cmd.Parameters.AddWithValue("@OnSale", prd.OnSale);
            cmd.Parameters.AddWithValue("@Valentine", prd.Valentine);
            cmd.ExecuteNonQuery();
            progressBar1.PerformStep();
        }

I tried to add this code

    List<Product> prd = products.ToList();

and transfer data from list, not directly from database, but had same error.

Thanks for help.

var cmd = connection.CreateCommand();

You probably need to properly dispose the object being created:

using(var cmd = connection.CreateCommand())
{
    cmd.CommandText = ...
}

To resolve this problem I created back-up of source database, and installed it on machine with destination database. Then programm was executed without any problems. Still cant realize why this exception happened to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM