2014-01-08 96 views
4

我正在處理一個項目,並且因爲「命令文本未設置爲命令對象」而出現錯誤。 我的代碼是:沒有爲命令對象設置命令文本

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,。在此先感謝...

+0

嘗試聲明一個查詢變量作爲sqlcommand數據類型 –

+0

你聲明你的查詢是什麼? –

+0

我已經聲明查詢爲字符串 –

回答

5

因爲它幫助你,我改變了評論作爲一個答案

查詢首次宣佈爲字符串,將其更改爲下面的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(); 
+0

不應該像'query.CommandText =「選擇前10 ....'? – th1rdey3

+0

這是一個艱難的瑣事努力工作,並沒有得到明確的答案,+1的努力。:)。th1rdey3是正確的,但是,你應該修復一個小錯字。 – paqogomez

+0

@paqogomez和Th1rdey3謝謝我編輯了一個nswer now .. :) –