2012-01-02 91 views
0

我想用OLEDB讀取Excel文件,但是當在查詢中插入參數,我有這樣的OleDb例外:參數傳遞到Excel查詢在asp.net

沒有值給出一個或多個所需的參數

代碼

string conn = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES"";", opFileImport.FileName); 

string sql = "Select CODICE_FISCALE From [Parte_4$] "; 
sql += "where CODICE_FISCALE = ?"; 

using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter()) 
{ 
    dataAdapter.SelectCommand = new OleDbCommand(sql, excelConn); 
    dataAdapter.SelectCommand.Parameters.Add(new OleDbParameter("Param1", strCodFisc)); 
    DataTable dt = new DataTable(); 
    dataAdapter.Fill(dt); 
} 

回答

0

參數名稱應該是CODICE_FISCALE代替 Param1 。試試這個代碼

var conn = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES"";", opFileImport.FileName); 
string sql = "Select CODICE_FISCALE From [Parte_4$] "; 
sql += "where CODICE_FISCALE = ?"; 
DataTable dt = new DataTable(); 
using (OleDbConnection excelConn = new OleDbConnection(conn)) 
{ 
    using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter()) 
    { 
     dataAdapter.SelectCommand = new OleDbCommand(sql, excelConn); 
     dataAdapter.SelectCommand.Parameters.Add(new OleDbParameter("CODICE_FISCALE", strCodFisc)); 
     dataAdapter.Fill(dt); 
    } 
}