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();
4

2 回答 2

1

您需要将字符串值括在单引号中,并将列名括在方括号中:

string Query = "select * from [data$] where [" + 
               col1Name + "] > " + Col1Value + 
               " AND Gender = '" + gender + "'"; 
于 2013-05-09T18:27:43.530 回答
0

我认为您的 SQL 查询中可能缺少引号:

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

注意添加了单引号 (') 符号。

于 2013-05-09T18:22:56.980 回答