VS 2005 MS SQL server express query problem

Y

Yogi

I wrote simple application (C#) that use a database (MS SQL serwer)
and I had very strage problem

I want to have pramized query, so my code is...

SELECT name
FROM person
WHERE (name LIKE N'%'+ @partOfName +'%')

I used VS 2005 with MS SQL server Express Edition I used this query in
Designing Queries and Views under "Query Builder"(wizzard). I had
filled in the form which vs 2005 display me after I pressed "Execute
Query"and it produce i.e. one row.

But when I start my application and I fill in my form with the same
text it doesn't produce anything (no rows)... I used SQL Server
Management Studio Express and I applied the same query there with the
same text and it produces one row...

When I use another Database Tool named "Preview data" in VS2005 it
behaves like my application...

Another threat in this case is if my 'name' in person table is
"Mike "(with spaces) and I put as the parametr "Mike" it produce
me one row but when I put "ike" it failes.

Any ideas?

I took me 4 hours to trace this problem without a solution...
I think it's MS's fault... What do you think?

Try it and abuse MS that they have the problem.... :D
 
C

Cowboy \(Gregory A. Beamer\)

Pull the query out of the command object after it has concatenated all of
the bits together. Then run that code in Express Studio and see what you
get. Tune the query until it works and take that back to your application.
That is step 1.

Next, get rid of the concatenation altogether and truly parameterized, as I
can do the following with yours

@partOfName = "') ;DROP TABLE person --"

This will issue the following command:

SELECT name
FROM person
WHERE (name LIKE N'%') ; DROP Table Person -- %'

Cute, huh?

If you have complex logic, put it in a stored procedure to avoid these types
of injection attacks.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*********************************************
Think outside the box!
*********************************************
 
Y

Yogi

Pull the query out of the command object after it has concatenated all of
the bits together. Then run that code in Express Studio and see what you
get. Tune the query until it works and take that back to your application.
That is step 1.

Next, get rid of the concatenation altogether and truly parameterized, as I
can do the following with yours

@partOfName = "') ;DROP TABLE person --"

This will issue the following command:

SELECT name
FROM person
WHERE (name LIKE N'%') ; DROP Table Person -- %'

Cute, huh?

If you have complex logic, put it in a stored procedure to avoid these types
of injection attacks.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBAhttp://gregorybeamer.spaces.live.com

*********************************************
Think outside the box!




- Show quoted text -

As I wrote before my query works in Express Studio!!! and in VS 2005
in Query builder in Execute query but doesn't work in my application
and in VS 2005 in Preview data...

Try it cowboy, please, thanks for answer...

But with getting rid of the concatenation altogether and truly
parameterized.... Sometimes it is nessesary to have truly
parameterized, so... do you see any solution in this case? Do you know
something like "magic quotes" or something like that in C# or in MS
SQL server? I think it is possible, I'm truly convinced that it's
possible!!
 
Y

Yogi

As I wrote before my query works in Express Studio!!! and inVS2005
in Query builder in Execute query but doesn't work in my application
and inVS2005 in Preview data...

Try it cowboy, please, thanks for answer...

But with getting rid of the concatenation altogether and truly
parameterized.... Sometimes it is nessesary to have truly
parameterized, so... do you see any solution in this case? Do you know
something like "magic quotes" or something like that in C# or inMSSQLserver? I think it is possible, I'm truly convinced that it's
possible!!


I found this to prevent the injections I hope this will be helpful for
beginer and other
To be secure with MS SQL read it:
http://msdn2.microsoft.com/en-us/library/aa174437(SQL.80).aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top