选择语句忽略参数? [英] Select statement ignoring parameters?

查看:124
本文介绍了选择语句忽略参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用此代码时,它返回表中的每一行,我也不知道为什么.

When I use this code it returns every row in the table and i have no idea why.

string SelectOleDb = "SELECT Top 1 * From `Employee Info` Where [Employee Name]=@EmployeeName" Order By ID DESC";

OleDbConnection OleDbCon = new OleDbConnection(EmployeeInfo.Properties.Settings.Default.cstrEmployeeInfoDatabase);
OleDbDataAdapter OleDbAdpt = new OleDbDataAdapter();
OleDbCommand OleDbCom = new OleDbCommand(SelectOleDb, OleDbCon);
OleDbCom.Parameters.AddWithValue("@EmployeeName", employee_NameComboBox.Text);
OleDbAdpt.SelectCommand = OleDbCom;

DataSet FooDS = new DataSet();
OleDbCon.Open();
OleDbAdpt.Fill(FooDS);
OleDbCon.Close();
OleDbCon.Dispose();
DataTable EmployeeInfo = FooDS.Tables[0];

我什至将雇员姓名"列中的值粘贴粘贴到文本框中,以验证我使用的是有效的雇员姓名.如果语句不正确,我希望什么也不会返回,而不是所有返回.

And i even copy pasted a value from the Employee Name column into the text box to verify i was using a valid employee name. I would expect nothing to be returned instead of everything if the statement was incorrect though.

更新:我也尝试过删除命名参数"@EmployeeName",并输入一个用单引号引起来的硬名.然而,语句仍会返回员工信息"中的所有内容

UPDATE: I have also tried removing the Named Paramter "@EmployeeName" and entering a hard corded name surrounded by single quotes. Yet still statement returns every thing in Employee Info

推荐答案

其他人说OleDb需要一个?并且它不接受命名参数.这是错误的.我已经修复了我的代码,并且可以正常工作.当前的问题是该声明需要不同的方式来定义空间.

Others have stated that OleDb required a ? and that it did not accept Named Parameters. This is false. I have fixed my code and it is working. The problem at hand was that the Statement required different ways to define spaces.

使用OleDB连接时,表名(如果必须有空格)必须放在EITHER`(刻度)中,否则两者将以相同的方式工作.

With the OleDB connection the Table Name if it had a space had to be in EITHER `(Ticks) or both will work the same.

当列名带有空格时,混乱就开始了.生成语句时,所有列名称必须在空格的列名称前加一个_(下划线).而((Ticks)和)对于列名都是可选的.所需要做的就是用_(低于分数)替换"(空格)

The confusions begins when you have Column Names with spaces. When the statement is built All column names have to have an _ (Under Score) in place of the Spaces the column names. While both `(Ticks) and are optional for Column names. All that is REQUIRED is the replacement of " "(Space) with _(under score)

让表名混乱的事实是,表名必须具有`(Ticks),或者如果您确实用_(under score)替换了(Space)",它将找不到该表.

What adds to the confusion is the fact that the table name is REQUIRED to have either `(Ticks) or and if you do replace a " "(Space) with _(under score) it will not find the table.

我的固定代码:

        string SelectOleDb = "SELECT Top 1 * From [Employee Info] Where Employee_Name= @EmployeeName Order By ID DESC";

        OleDbConnection OleDbCon = new OleDbConnection(EmployeeInfo.Properties.Settings.Default.cstrEmployeeInfoDatabase);
        OleDbDataAdapter OleDbAdpt = new OleDbDataAdapter();
        OleDbCommand OleDbCom = new OleDbCommand(SelectOleDb, OleDbCon);
        OleDbCom.Parameters.AddWithValue("@EmployeeName", employee_NameComboBox.Text);
        OleDbAdpt.SelectCommand = OleDbCom;

            DataSet EmployeeInfoDS = new DataSet();
            OleDbCon.Open();
            OleDbAdpt.Fill(EmployeeInfoDS);
            OleDbCon.Close();
            OleDbCon.Dispose();
            DataTable EmployeeInfoDT = EmployeeInfoDS.Tables[0];

这篇关于选择语句忽略参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆