未为命令对象设置命令文本

时间:2014-01-08 06:52:20

标签: c# asp.net sql-server ms-access-2007 runtime-error

我正在处理一个项目并收到错误,因为“没有为命令对象设置命令文本”。 我的代码是:

query = "select Top 10    
                 Name,
                 R_Line2,
                 BirthDate,
                 BirthTime,
                 Height,
                 Weight,
                 BirthCity,
                 BirthCountry,
                 FatherName,
                 MonthlyIncome,
                 FamilyIncome,
                 Add1,
                 Add2,
                 PinCode,
                 Tel1,
                 Tel2
          from InpRegistration  
          where DateDiff('yyyy', [Birthdate],
                  Now()) BETWEEN @AgeFrom AND
                  @AgeTo and Weight BETWEEN @MinWeight AND 
                  @MaxWeight and Height BETWEEN @MinHeight AND @MaxHeight and   
                  MonthlyIncome BETWEEN @MinIncome AND @MaxIncome";

connf.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data   Source=D:\project\Milan_Data.mdb";

connf.Open();

   OleDbCommand cmdf = new OleDbCommand(query, connf);

    cmdf.CommandType = CommandType.Text;

        cmdf.Parameters.AddWithValue("@AgeFrom", ddlminage.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@AgeTo", ddlmaxage.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@MinWeight", ddlweightmin.SelectedValue);
        cmdf.Parameters.AddWithValue("@MaxWeight", ddlweightmax.SelectedValue);
        cmdf.Parameters.AddWithValue("@MinHeight", ddlheightmin.SelectedValue);
        cmdf.Parameters.AddWithValue("@MaxHeight", ddlheightmax.SelectedValue);
        cmdf.Parameters.AddWithValue("@MinIncome", ddlminincome.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@MinIncome", ddlmaxincome.SelectedItem.Text);

        OleDbDataAdapter daf = new OleDbDataAdapter(cmdf);
        DataSet dsf = new DataSet();
        daf.Fill(dsf);
        Repeater2.DataSource = dsf;
        Repeater2.DataBind();


        connf.Close();

请帮帮我。我在网上搜索但没有得到任何解决方案。Similar Problem ,.提前谢谢......

1 个答案:

答案 0 :(得分:6)

因为它帮助了你,我正在改变评论作为答案

查询首先声明为字符串,因此将其更改为sqlcommand,如下所示

SqlCommand query = new sqlcommand(); 
query.commandText = "select Top 10..."     

最终版本:

SqlCommand query = new sqlcommand();   
query.commandText = "select Top 10    
                 Name,
                 R_Line2,
                 BirthDate,
                 BirthTime,
                 Height,
                 Weight,
                 BirthCity,
                 BirthCountry,
                 FatherName,
                 MonthlyIncome,
                 FamilyIncome,
                 Add1,
                 Add2,
                 PinCode,
                 Tel1,
                 Tel2
          from InpRegistration  
          where DateDiff('yyyy', [Birthdate],
                  Now()) BETWEEN @AgeFrom AND
                  @AgeTo and Weight BETWEEN @MinWeight AND 
                  @MaxWeight and Height BETWEEN @MinHeight AND @MaxHeight and   
                  MonthlyIncome BETWEEN @MinIncome AND @MaxIncome";

connf.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data   Source=D:\project\Milan_Data.mdb";

connf.Open();

   OleDbCommand cmdf = new OleDbCommand(query, connf);

    cmdf.CommandType = CommandType.Text;

        cmdf.Parameters.AddWithValue("@AgeFrom", ddlminage.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@AgeTo", ddlmaxage.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@MinWeight", ddlweightmin.SelectedValue);
        cmdf.Parameters.AddWithValue("@MaxWeight", ddlweightmax.SelectedValue);
        cmdf.Parameters.AddWithValue("@MinHeight", ddlheightmin.SelectedValue);
        cmdf.Parameters.AddWithValue("@MaxHeight", ddlheightmax.SelectedValue);
        cmdf.Parameters.AddWithValue("@MinIncome", ddlminincome.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@MinIncome", ddlmaxincome.SelectedItem.Text);

        OleDbDataAdapter daf = new OleDbDataAdapter(cmdf);
        DataSet dsf = new DataSet();
        daf.Fill(dsf);
        Repeater2.DataSource = dsf;
        Repeater2.DataBind();


        connf.Close();