Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2010
    Messaggi
    2

    Importare dati Excel in tabella sqlserver

    Salve,
    sto sviluppando un'applicazione in cui devo importare dati in una tabella sql server da un foglio excel; ho già trovato in rete il codice per farlo:

    codice:
             string strSheetName = "Energy Manager";
    
            //excel filePath ce l'ho in hfPathFileUploaded
    
            string sSQLTable = "TempDatiExcel";
    
            string myExcelDataQuery = "Select * from [" + strSheetName + "$" + "] ";
            try
            {  
                string sExcelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + hfPathFileUploaded.Value + ";Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";
    
                string sSqlConnectionString = "Data Source=DMCODK1225\\SQLEXPRESS;Initial Catalog=ProveExcel;Integrated Security=True;Connect Timeout=1000";
    
                          string sClearSQL = "DELETE FROM " + sSQLTable;
                SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
                SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
                SqlConn.Open();
                SqlCmd.ExecuteNonQuery();
                SqlConn.Close();
    
                OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
                OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
                OleDbConn.Open();
    
                OleDbDataReader dr = OleDbCmd.ExecuteReader();
    
                SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString);
                bulkCopy.DestinationTableName = sSQLTable;
                while (dr.Read())
                {
                    bulkCopy.WriteToServer(dr);
                }
                OleDbConn.Close();
            }
            catch (Exception ex)
            {//handle exception
    Il problema è che fino a ieri funzionava tutto ma oggi viene generata un'eccezione quando viene eseguita la "OleDbConn.Open" e non riesco proprio a venirne fuori!!

    Any ideas to resolve it?
    Considerate che sto lavorando con vs2010 rc!!! (e praticamente ho detto tutto)!

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2010
    Messaggi
    2
    ok risolto... era solo una questione di permessi della cartella:
    C:\Documents and Settings\*****\ASPNET

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.