如何使用 Microsoft.ACE.OLEDB 和 SqlBulkCopy 正确导入启用了换行的 excel 列
How to properly import excel columns that have wrap text enabled using Microsoft.ACE.OLEDB and SqlBulkCopy
我正在使用批量插入将行从 excel sheet 导入 sql 服务器 table。
excel 源代码中的某些列启用了换行,它们导致了一些问题。
如果说 "wrapped text (1)" 和 "wrapped text (2)",它们将在 excel 单元格中显示如下。
wrapped text (1)
wrapped text (2)
我遇到的问题是,当该行导入 SQL 服务器时,它们将如下所示
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();
}
}
}
打击是我的excel连接字符串
strexcelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(strfilename) + "; Extended Properties=\"Excel 12.0 XML;HDR=YES;IMEX=1;\"";
在我的 BulkInsert 之后,我正在按照此处所述更新我的有问题的列。 How to check my data in SQL Server have carriage return and line feed?
我正在使用批量插入将行从 excel sheet 导入 sql 服务器 table。 excel 源代码中的某些列启用了换行,它们导致了一些问题。
如果说 "wrapped text (1)" 和 "wrapped text (2)",它们将在 excel 单元格中显示如下。
wrapped text (1)
wrapped text (2)
我遇到的问题是,当该行导入 SQL 服务器时,它们将如下所示
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();
}
}
}
打击是我的excel连接字符串
strexcelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(strfilename) + "; Extended Properties=\"Excel 12.0 XML;HDR=YES;IMEX=1;\"";
在我的 BulkInsert 之后,我正在按照此处所述更新我的有问题的列。 How to check my data in SQL Server have carriage return and line feed?