2013-05-09 76 views
0

我的項目有問題。錯誤 - 沒有給出一個或多個所需參數的值

我試圖從Excel文件讀取數據。當我嘗試選擇大於Col1Value的行時,但在添加AND Gender = " + gender;後,它可以正常工作,但它給了我錯誤「沒有爲一個或多個必需參數提供的值」我無法設置特定性別列,因爲它在每個Excel中都不相同文件雖然列名是相同的,當我試圖填充數據集時出現錯誤。

if (boxGender.Text != "") 

string gender = boxGender.Text; 
string col1Name = lbl1stColumn.Text; 


string Query = "select * from [data$] where " + 
       col1Name + " > " + Col1Value + 
       " AND Gender = " + gender;             
OleDbDataAdapter dacol1 = new OleDbDataAdapter(Query, con);       
    Column1Data.Clear(); 
    dacol1.Fill(Column1Data) 
    lblStuCount1Col.Text = Column1Data.Tables[0].Rows.Count.ToString(); 
+3

當心SQL注入 – arunlalam 2013-05-09 18:19:23

+0

的[我們應包括問題標題標籤(HTTP://meta.stackexchange .com/questions/19190/should-questions-include-tags-in-their-titles)? :不,我們不應該。我們是否應該用** BOLD **來書寫標題? :不,我們不應該。 – phadaphunk 2013-05-09 18:20:51

+0

只有我會使用它:) :) – 2013-05-09 18:20:51

回答

0

我想你可能在你的SQL查詢缺失報價:

string Query = "select * from [data$] where " + col1Name + " > '" + Col1Value + "' AND Gender = '" + gender +"'"; 

注單引號(')符號添加。

+0

Yeap就是這個問題,謝謝Sir – 2013-05-09 18:27:55

+0

@EimantasBaigys沒問題 – 2013-05-09 18:30:34

+0

當我執行這個查詢時,它會根據性別獲取所有數據,但是它不會過濾例如Male,其中'col1Name> Col1Value'代替它顯示所有男性....任何建議? – 2013-05-10 13:02:01

1

您需要在單引號字符串值,並在方括號中的列名:

string Query = "select * from [data$] where [" + 
       col1Name + "] > " + Col1Value + 
       " AND Gender = '" + gender + "'"; 
+0

括號中的列名不應該是必需的,對吧?沒有空格,肯​​定'col1Name'不是關鍵字。 – Brad 2013-05-09 18:29:34

+0

'col1name'是一個變量,而不是文字。它可能包含具有空格的值。 – 2013-05-09 18:42:02

+0

你是對的...我被這些明亮的燈光遮住了這裏 – Brad 2013-05-09 20:48:09

相關問題