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");
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?