没有给出一个或多个需求参数的值

时间:2011-01-12 19:10:44

标签: c# sql oledb

Plz告诉我我做错了什么,我正在复制有问题的部分代码

query = "SELECT * FROM ACCOUNT_KEEPER WHERE DATEPART(MONTH,[DATE]) = ?";

 DataSet dataSet=new DataSet();
            OleDbDataAdapter dataAdapter=new OleDbDataAdapter();
            try
            {
                if(OpenOledbConnection())
                {
                    dataAdapter.SelectCommand=new OleDbCommand(query,connection);
                    dataAdapter.SelectCommand.Parameters.AddWithValue("@DATEPART(MONTH,[DATE]", date.Month);
                    dataAdapter.Fill(dataSet);
                    // checking if dataset is updated or not
                    int temp = dataSet.Tables[0].Rows.Count;
                    return dataSet;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error While Retrieving data by Month");

            }
            finally
            {
                CloseOledbConnection();
            }
            return null;

先谢谢

MGD

1 个答案:

答案 0 :(得分:1)

而不是

query = "SELECT * FROM ACCOUNT_KEEPER WHERE DATEPART(MONTH,[DATE]) = ?";

query = "SELECT * FROM ACCOUNT_KEEPER WHERE DATEPART(MONTH,[DATE]) = @x";

而不是

dataAdapter.SelectCommand.Parameters.AddWithValue("@DATEPART(MONTH,[DATE]", date.Month);

dataAdapter.SelectCommand.Parameters.AddWithValue("@x", date.Month);