【问题标题】:Command text was not set for the command object没有为命令对象设置命令文本
【发布时间】:2014-01-09 11:05:39
【问题描述】:

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

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,。提前谢谢...

【问题讨论】:

  • 尝试将查询变量声明为 sqlcommand 数据类型
  • 您将查询声明为什么??
  • 我已将查询声明为字符串
  • @AmarnathBalasubramanian 查询是一个“字符串”。
  • 我不擅长 c#,但我检查了一些示例,您确定您正确附加了参数吗?看这个例子stackoverflow.com/a/15368537/1692632

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


【解决方案1】:

因为它对你有帮助,所以我将评论更改为答案

查询最初被声明为字符串,因此将其更改为 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();

【讨论】:

  • 努力工作却没有得到答案,这是一项艰巨的任务,努力+1。 :)。 th1rdey3 是对的,但是你应该改正那个小错误。
  • @paqogomez 和 Th1rdey3 谢谢我现在编辑了答案.. :)
  • @AmarnathBalasubramanian 抱歉,您迟到了将您的回复标记为“答案”。实际上,我正忙于我的项目并没有得到它们。希望你不介意..
  • @SantoshKumar 别担心,兄弟..!!快乐编码:)
猜你喜欢
  • 1970-01-01
  • 2021-04-24
  • 2022-01-20
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 2021-01-19
相关资源
最近更新 更多