无法将列在比较的SqlBulkCopy [英] Unable to compare the columns in SqlBulkCopy

查看:73
本文介绍了无法将列在比较的SqlBulkCopy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

 protected void Button1_Click(object sender, EventArgs e)
 {
    string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
    string strFileName = FileUpload1.PostedFile.FileName.ToString();

    FileUpload1.SaveAs(Server.MapPath("~/Import/" + strFileName + strFileType));
    string strNewPath = Server.MapPath("~/Import/" + strFileName + strFileType);

    string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+strNewPath +"; Extended Properties=Excel 8.0;");

    //string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\myFolder\\Book1.xls;" + "Extended Properties=Excel 8.0;"); 

   // Create Connection to Excel Workbook
   using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
   {
       OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Sheet1$]", connection);

       connection.Open();

       // Create DbDataReader to Data Worksheet
       using (DbDataReader dr = command.ExecuteReader())
       {
          // SQL Server Connection String
          string sqlConnectionString = "Data Source=DITSEC3;Initial Catalog=test;Integrated Security=True";

          con.Open();
          DataTable dt1 = new DataTable();
          string s = "select count(*) from ExcelTable"; 
          string r = ""; 
          SqlCommand cmd1 = new SqlCommand(s, con);

          try 
          { 
              SqlDataAdapter da1 = new SqlDataAdapter(cmd1); 
              da1.Fill(dt1);
          }
          catch { } 

          int RecordCount; 
          RecordCount = Convert.ToInt32(cmd1.ExecuteScalar());

          r = RecordCount.ToString(); 
          Label1.Text = r;
          con.Close();
          int prv = Convert.ToInt32(r);

          //matching columns
          //SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("id", "ida");
          //SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("data", "dataa");

          // Bulk Copy to SQL Server
          using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
          {
              bulkCopy.DestinationTableName = "ExcelTable";
              bulkCopy.WriteToServer(dr);
          }

          con.Open();
          DataTable dt = new DataTable(); 
          s = "select count(*) from ExcelTable";  r = ""; 

          SqlCommand cmd = new SqlCommand(s, con); 

          try 
          { 
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(dt); 
          }
          catch { } 

          RecordCount = Convert.ToInt32(cmd.ExecuteScalar()); 
          r = RecordCount.ToString(); Label1.Text = r;

          con.Close();

          int ltr = Convert.ToInt32(r);
          if (prv == ltr)
          {
             Label1.Text = "No records Added";
          }
          else
          {
             Label1.Text = "Records Added Successfully !";
          }
       }
    }

我知道我需要添加类似:

I know I need to add something like:

 SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("id", "ida");
 SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("data", "dataa");

但我不知道在那里我应该将其添加在上面code

but I am not sure where I am supposed to add it in the above code

推荐答案

列映射关系将被添加到 bulkCopy.ColumnsMappings 系列:

The column mappings are to be added to the bulkCopy.ColumnsMappings collection:

var mapping1 = new SqlBulkCopyColumnMapping("id", "ida");
bulkCopy.ColumnMappings.Add(mapping1);

在执行在WriteToServer 调用之前你做的映射。

You do the mapping before you execute the WriteToServer call.

<$的MSDN文档 C $ C> SqlBulkCopyColumnMapping 有更多文档和示例。

The MSDN documentation of SqlBulkCopyColumnMapping has further documentation and an example.

这篇关于无法将列在比较的SqlBulkCopy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆