Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Microsoft.ACE.OLEDB和SqlBulkCopy正确导入启用了换行文本的excel列_C#_Sql Server_Excel_Oledb_Sqlbulkcopy - Fatal编程技术网

C# 如何使用Microsoft.ACE.OLEDB和SqlBulkCopy正确导入启用了换行文本的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

我正在使用大容量插入将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 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之后,我将更新有问题的列,如下所述