Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
i have this code in web page
my problem:
there is 2 records in the database one of it have ip=99 and the other one have ip=000099 as varchar
but when i requset ip=99 i get record for ip=000099
i tried to concatenate the command text using
VB
string.Concat();
string.Format();
string.Append();
stringBuilder

but no change
my code:
C#
try
                {
                    OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
                    myOleDbConnection.Open();
                    OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
                  string str="99";
                    myOleDbCommand.CommandText = "select sale_price,postion from item where ip=" + str;
                    OleDbDataReader dr = myOleDbCommand.ExecuteReader();
                    dr.Read();
///////////get results
 dr.Close();
                    myOleDbConnection.Close();

                }
                catch 
                {
                    
                }
Posted

You have forgotten to enclose your str in the where clause with single quote. But you should not inject variable directly into the sql query. Instead, you should use parameterized query[^] to prevent sql injection[^].
 
Share this answer
 
v2
"i tried to concatenate the command text"

DON"T!!!!

Use a parameterized query. Every time. It always works.
 
Share this answer
 
Comments
M. Daban 26-Mar-14 9:47am    
can you give an example please
Raul Iloc 27-Mar-14 7:53am    
The parameterized string should be used only in the case when the SQL is made by using user input, but here is not the case!
PIEBALDconsult 27-Mar-14 9:10am    
No. Every time. It's not just about SQL injection; there are actions that can be performed only with parameters, and parameters can improve performance as well.
Concatenation should be avoided except for when parameters won't work -- for example specifying the table name, or a TOP value.
Parameters should always be your first choice, not your last.
Because your IP type is varchar, you should correct the SQL in this way:
C#
myOleDbCommand.CommandText = string.Format("select sale_price,postion from item where ip='{0}'", str);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900