2017-09-30 39 views
1

我想要從Excel工作表導入特定列並將其插入到SQL Server表中的特定列中。從Excel導入到SQL Server中的特定列

下面的代碼不會插入任何數據到表中,我認爲,因爲它主要的關鍵是它顯示數據在網格視圖中的數據成功,但沒有插入數據,所以我想插入一列而不是整個表:

protected void ImportButton_Click(object sender, EventArgs e) 
{ 
    if (FileUpload1.HasFile) 
    { 
     string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); 
     string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName); 
     string FolderPath = ConfigurationManager.AppSettings["FolderPath"]; 

     string FilePath = Server.MapPath(FolderPath + FileName); 
     FileUpload1.SaveAs(FilePath); 
     Import_To_Grid(FilePath); 
    } 
} 

private void Import_To_Grid(string FilePath) 
{ 
    string conStr = ""; 
    //switch (Extension) 
    //{ 
    // case ".xls": //Excel 97-03 
    //  conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"] 
    //     .ConnectionString; 
    //  break; 
    // case ".xlsx": //Excel 07 
    //  conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"] 
    //     .ConnectionString; 
    //  break; 
    //} 
    String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", FilePath); 

    conStr = String.Format(excelConnString, FilePath); 

    OleDbConnection connExcel = new OleDbConnection(conStr); 
    OleDbCommand cmdExcel = new OleDbCommand(); 
    OleDbDataAdapter oda = new OleDbDataAdapter(); 

    DataTable dt = new DataTable(); 
    cmdExcel.Connection = connExcel; 

    //Get the name of First Sheet 
    connExcel.Open(); 
    DataTable dtExcelSchema; 
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
    string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); 
    connExcel.Close(); 

    //Read Data from First Sheet 
    connExcel.Open(); 
    cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; 
    oda.SelectCommand = cmdExcel; 
    oda.Fill(dt); 

    using (OleDbDataReader dReader = cmdExcel.ExecuteReader()) 
    { 
     using (SqlBulkCopy sqlBulk = new SqlBulkCopy(_Conn.ConnStr())) 
     { 
      //Set your Destination table name 
      sqlBulk.DestinationTableName = "[AIS].[Inventory].[Inventory_Movments_TBL]"; 
      sqlBulk.WriteToServer(dt); 

      //Bind Data to GridView 
      GridView1.Caption = Path.GetFileName(FilePath); 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
     } 
    } 

    connExcel.Close(); 
} 

回答

1

我建議你使用的SqlConnection如下(除非你已經在「_conn」變量的的SqlConnection):

using (SqlConnection con = new SqlConnection(_Conn.ConnStr())) 
    { 
     using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con)) 
     { 
      //Set the database table name 
      sqlBulkCopy.DestinationTableName = "dbo.tblPersons"; 

      //Might be a good idea to map excel columns with that of the database table (optional) 
      sqlBulk.ColumnMappings.Add("Id", "PersonId"); 
      sqlBulk.ColumnMappings.Add("Name", "Name"); 
      con.Open(); // these might be the issue since you need to open.. 
      sqlBulk.WriteToServer(dtExcelData); 
      con.Close(); // ..and close the connection 
     } 
    } 

另外我想從這裏取出打開和關閉連接:

connExcel.Close(); 

//Read Data from First Sheet 
connExcel.Open(); 
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; 
oda.SelectCommand = cmdExcel; 
oda.Fill(dt); 

的,而不是做填充後接近1個連接(並刪除「的底部connExcel.Close();」以及):

//Read Data from First Sheet 
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; 
oda.SelectCommand = cmdExcel; 
oda.Fill(dt); 
connExcel.Close(); 
+0

我都尊重我的代碼上的另一臺正常工作,其沒有PK只有一列,我會嘗試你的改變 –