C# 如何使用Microsoft.ACE.OLEDB和SqlBulkCopy正确导入启用了换行文本的excel列
我正在使用大容量插入将excel工作表中的行导入sql server表。 excel源代码中的某些列启用了换行文本,导致了一些问题 如果说“包装文本(1)”和“包装文本(2)”,它们将在excel单元格中显示如下所示C# 如何使用Microsoft.ACE.OLEDB和SqlBulkCopy正确导入启用了换行文本的excel列,c#,sql-server,excel,oledb,sqlbulkcopy,C#,Sql Server,Excel,Oledb,Sqlbulkcopy,我正在使用大容量插入将excel工作表中的行导入sql server表。 excel源代码中的某些列启用了换行文本,导致了一些问题 如果说“包装文本(1)”和“包装文本(2)”,它们将在excel单元格中显示如下所示 wrapped text (1) wrapped text (2) 我遇到的问题是,当该行被导入SQL server时,它们将如下所示 wrapped text (1)wrapped text (2)//Note that there is no space between t
wrapped text (1)
wrapped text (2)
我遇到的问题是,当该行被导入SQL server时,它们将如下所示
wrapped text (1)wrapped text (2)//Note that there is no space between the two texts
wrapped text (1) wrapped text (2)//note the space between them
我希望它们看起来像下面这样
wrapped text (1)wrapped text (2)//Note that there is no space between the two texts
wrapped text (1) wrapped text (2)//note the space between them
下面是我用于导入的和平代码
public static void insertdata1(string strexcelConnectionString, string strcommand, string strsqlConnectionString, string strtrunsqlQuery, string strtablename)
{
using (OleDbConnection connection = new OleDbConnection(strexcelConnectionString))
{
connection.Open();
OleDbCommand command = new OleDbCommand(strcommand, connection);
//truncate the table before inserting new data.
SqlConnection cnntrunc = new SqlConnection(strsqlConnectionString);
//open the connection.
cnntrunc.Open();
//begin the transaction.
SqlTransaction myTrans = cnntrunc.BeginTransaction();//New a transaction
SqlCommand truntble = new SqlCommand();
truntble.Transaction = myTrans;
truntble.Connection = cnntrunc;
try
{
//Create DbDataReader to Data Worksheet.
using (DbDataReader dr = command.ExecuteReader())
{
//Bulk Copy to SQL Server.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(strsqlConnectionString))
{
bulkCopy.DestinationTableName = strtablename;
bulkCopy.WriteToServer(dr);
}
//commit the transaction.
myTrans.Commit();
}
}
catch (OleDbException ex)
{
//my error handling code here
}
catch (InvalidOperationException ex)
{
myTrans.Rollback();
//more logging here
throw ex;
}
finally
{
cnntrunc.Close();
}
}
}
blow是我的excel连接字符串
strexcelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(strfilename) + "; Extended Properties=\"Excel 12.0 XML;HDR=YES;IMEX=1;\"";
在我的BulkInsert之后,我将更新有问题的列,如下所述