2016-07-05 15 views
0

Entwickeln von Asp.Net für das Extrahieren von Daten aus Excel-Blatt dann in eine gespeicherte Prozedur einfügen, während meiner Suche wusste, dass es nicht möglich ist, und ich kann kopieren Daten aus der Excel-Tabelle in eine temporäre Tabelle dann aus temporären Tabelle der Prozedur gespeichert hier ist der Code hinter i Daten in eine reguläre Tabelle zum Einfügen, bitte helfenEinfügen von Excel-Daten in eine gespeicherte Prozedur in SQL-Server mit asp.net

public partial class WebForm1 : System.Web.UI.Page 
{ 

    OleDbConnection Econ; 
    SqlConnection con; 

    string constr, Query, sqlconn; 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    private void ExcelConn(string FilePath) 
    { 

     constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", FilePath); 
     Econ = new OleDbConnection(constr); 

    } 

    private void connection() 
    { 
     sqlconn = ConfigurationManager.ConnectionStrings["InvoiceProjectConnectionString"].ConnectionString; 
     con = new SqlConnection(sqlconn); 

    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName); 
     InsertExcelRecords(CurrentFilePath); 

    } 

    private void InsertExcelRecords(string FilePath) 
    { 
     ExcelConn(FilePath); 

     Query = string.Format("Select [CODE],[Brand],[SubBrand],[SubBrand2],[Category],[Category2] FROM [{0}]", "Sheet1$"); 

     OleDbCommand Ecom = new OleDbCommand(Query, Econ); 
     Econ.Open(); 

     DataSet ds = new DataSet(); 
     OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ); 
     Econ.Close(); 
     oda.Fill(ds); 
     DataTable Exceldt = ds.Tables[0]; 
     connection(); 
     //creating object of SqlBulkCopy  
     SqlBulkCopy objbulk = new SqlBulkCopy(con); 
     //assigning Destination table name  
     objbulk.DestinationTableName = "tblTest"; 
     //Mapping Table column  
     objbulk.ColumnMappings.Add("CODE", "Code"); 
     objbulk.ColumnMappings.Add("Brand", "Brand"); 
     objbulk.ColumnMappings.Add("SubBrand", "SubBrandOne"); 
     objbulk.ColumnMappings.Add("SubBrand2", "SubBrandTwo"); 
     objbulk.ColumnMappings.Add("Category", "Category"); 
     objbulk.ColumnMappings.Add("Category2", "CategoryTwo"); 

     //inserting Datatable Records to DataBase  
     con.Open(); 
     objbulk.WriteToServer(Exceldt); 
     con.Close(); 
    } 

} 

Antwort

0

der Code ist in Ordnung, aber bin mit ist unvollständig ...

protected void Button1_Click(object sender, EventArgs e) 
     { 
      string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName); 
      //step 1...    
      InsertExcelRecords(CurrentFilePath); 

      //step 2: execute your procedure on sql-server that read your temp-table "tblTest" and insert on your table 
      PopulateTable(); 

     } 

     protected void PopulateTable(){ 
     //Your code for execute your procedure... 
     } 

exam ple für die Ausführung einer Prozedur auf SQL-Server: How to execute a stored procedure within C# program