【问题标题】:Unable to compare the columns in SqlBulkCopy无法比较 SqlBulkCopy 中的列
【发布时间】:2012-10-24 07:40:13
【问题描述】:

这是我的代码:

 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 !";
          }
       }
    }

我知道我需要添加如下内容:

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

但我不确定我应该在上面的代码中添加它的什么地方

【问题讨论】:

    标签: c# asp.net sql-server


    【解决方案1】:

    列映射将被添加到bulkCopy.ColumnsMappings 集合中:

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

    在执行WriteToServer 调用之前进行映射。

    SqlBulkCopyColumnMappingMSDN documentation 有进一步的文档和示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 2022-11-27
      • 2018-06-30
      • 2012-02-12
      • 2022-01-12
      • 2021-02-13
      • 1970-01-01
      相关资源
      最近更新 更多