Select statement ignoring parameters?

▼魔方 西西 提交于 2019-12-25 06:29:55

问题


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.

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


回答1:


Looks like you're using two different DataSets:

DataSet FooDS = new DataSet();   // <-- FooDS?
OleDbCon.Open();
OleDbAdpt.Fill(ExpediteDS);   // <-- filing a different dataset?
OleDbCon.Close();
OleDbCon.Dispose();
DataTable EmployeeInfo = FooDS.Tables[0];  // <-- not the dataset you just filled!

If this is a copy/paste error please post your code exactly as you have it - if you try and "dummy" up your code you're going to cause folks to chase rabbits and not expose the real problem.




回答2:


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.

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

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)

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.

My fixed Code:

        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];



回答3:


As documented by MSDN:

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used.

I tried out a variant of your query and an OLEDB connection to SQL Server using the ? and it worked as expected. Using a named parameter fails.

As requested, here is the sample:

        string SelectOleDb = "SELECT Top 1 * From users Where [application_user_name]=? Order By application_user_id DESC";

        OleDbConnection OleDbCon = new OleDbConnection("Provider=SQLOLEDB;Data Source=SERVER\\INSTANCE;Initial Catalog=samples;Trusted_Connection=yes");

        OleDbDataAdapter OleDbAdpt = new OleDbDataAdapter();
        OleDbCommand OleDbCom = new OleDbCommand(SelectOleDb, OleDbCon);
        OleDbCom.Parameters.AddWithValue("@EmployeeName", "smith");
        OleDbAdpt.SelectCommand = OleDbCom;

        DataSet ExpediteDS = new DataSet();

        DataSet FooDS = new DataSet();
        OleDbCon.Open();
        OleDbAdpt.Fill(ExpediteDS);
        OleDbCon.Close();
        OleDbCon.Dispose();

it is important to note, that with OLEDB, parameter order matters. You must add them to the ParameterCollection in the order you want them referenced in the query.



来源:https://stackoverflow.com/questions/15367028/select-statement-ignoring-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!