Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I have an application written by native c++. Now I want to use .net sqlbulkcopy function to do bulk insert without using the /clr option. I write the following .net code, and build them as intercop com.

using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace comwrapper
{
    public interface Ibulkcopysql
    {
        int sqlbulkcopy(string source, string dest, string tablename);
    };
    public class bulkcopysql : Ibulkcopysql
    {
        public int sqlbulkcopy(string source, string dest, string tablename)
        {
            Console.WriteLine("enter into c# com");
            //Console.ReadKey();
            OleDbConnection sourceConnection = new OleDbConnection(source);
            String sqlselect = "SELECT * from " + tablename + ";";
            OleDbCommand commandSourceData = new OleDbCommand(sqlselect, sourceConnection);
            sourceConnection.Open();
            OleDbDataReader reader  = commandSourceData.ExecuteReader();
            SqlConnection destinationConnection = new SqlConnection(dest);
            destinationConnection.Open();
            SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection);
            bulkCopy.DestinationTableName = tablename;
            bulkCopy.BatchSize = 1000;
            bulkCopy.BulkCopyTimeout = 100000;
            bulkCopy.WriteToServer(reader);

            return 1;
        }
    }
}

native c++ code
#import "C:\xxxxx\bin\Debug\comwrapper.tlb" 

.......
		HRESULT hr = CoInitialize(NULL);
		cescomwrapper::IcesbulkcopysqlPtr bcpptr(__uuidof(cescomwrapper::cesbulkcopysql));
		bcpptr->sqlbulkcopy(sourcestr.AllocSysString(), deststr.AllocSysString(), tablename.AllocSysString());

......


When I try to debug the C# code, I found it enter into sqlbulkcopy function, but
break out at the following the line without giving any error message

OleDbConnection sourceConnection = new OleDbConnection(source);


Anyone know the reason?
Posted
Updated 30-Mar-11 8:24am
v2

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