I want read a excel file using OleDB but when insert a parameter in the query, I have this OleDb exception:
No value was given for one or more of the required parameters
Code
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);
}
The parameter name should be CODICE_FISCALE instead of
Param1
. Try this code
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.