Missing Required Parameter in Parameterized Query?

佐手、 提交于 2019-12-10 23:39:20

问题


I am getting the following error trying to execute the code below

No Value Given For One Or More Required Parameters.

string paraName = "CONTROL";
string fullPathToExcel = @"C:\Users\xbbjn2h\Desktop\Mapping.xlsx"; 
string connString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""",fullPathToExcel);
string sql = "SELECT [FUNCTION],[NAME] from [Sheet1$] WHERE [FUNTION] = ?";

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
    cmd.Parameters.AddWithValue("?", paraName);
    DataSet ds = new DataSet();
    conn.Open();
    OleDbDataAdapter dab = new OleDbDataAdapter(cmd);
    dab.Fill(ds);
    dataGridView1.DataSource = ds.Tables[0];
    conn.Close();
}        

回答1:


OleDbCommand does not support named parameters (see the docs). What you need to do is something like:

cmd.Parameters.Add(new OleDbParameter { Value = paraName });

Edit: I haven't tried it, but I suppose it might be possible to use your above code and pass null as the name argument...




回答2:


James, I can see that you are trying to parametrize as you would with SQL. However OleDb wouldn't follow the exact pattern. In OleDb you can simply put question marks in your sql sentence and fill them up later using Parameter.Add().value which is quite straight forward.

As MSDN reference points out:

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text.

I have rewritten your code in the way it should look like

    string paraName = "CONTROL";
    string fullPathToExcel = @"C:\Users\xbbjn2h\Desktop\Mapping.xlsx"; 
    string connString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""",fullPathToExcel);
    string sql = string.Format("SELECT [{0}],[{1}] from [{2}] WHERE [{0}] = ?", strFunction, strName, strSheetName);

    conn.ConnectionString = connString;
    using (OleDbConnection conn = new OleDbConnection())
    {
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;
        cmd.CommandText = sql;
        cmd.Parameter.Add("@Param1", OleDbType.VarChar).Value = paraName; // paraName or any value you wish.

        DataSet ds = new DataSet();
        conn.Open();
        OleDbDataAdapter dab = new OleDbDataAdapter(cmd); //or cmd.ExecuteNonQuery(); in Insert sql commands.
        dab.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
        conn.Close();
    }       

Note, You should put your OleDbConnection inside the using statement rather than your OleDbCommand. I haven't tested it and it may contain some minor error. Other than that punch it there and press play (F5).

You can find more on OleDb parametrization from OleDbCommand.Parameters Property

Good luck and let us know what happens.



来源:https://stackoverflow.com/questions/28010640/missing-required-parameter-in-parameterized-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!