CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2005
    Posts
    73

    Prevent injection MSSql server

    Hello,
    I wanted to ask if anyone knows of a way to prevent injection in an SQL SERVER 2005. I mean, is there any way to do all the blocking in the server and not have to escape each special character one-by-one?
    For example, in PHP I used mysql_escape_string and automatically the string was OK to send to the database... Is there something similar in SQL Server?

    Thank you

  2. #2
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: Prevent injection MSSql server

    It's up to the client to escape the query that it sends to the server. The server cannot know if the client meant for an extra apostrophe to be there or not.

    If you're accessing the database through .NET, this is handled automatically when using the SqlCommand object and parameters:
    Code:
    SqlCommand command = new SqlCommand("SELECT a, b, c FROM table WHERE id = @id", connection);
    command.Parameters.AddWithValue("@id", 123);
    SqlDataReader reader = command.ExecuteReader();

  3. #3
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Prevent injection MSSql server

    as andreasblixt said, the best way is to use parameterized queries.
    In PHP: you can use: mssql_init,mssql_bind,mssql_execute functions.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  4. #4
    Join Date
    Sep 2006
    Posts
    635

    Re: Prevent injection MSSql server

    Quote Originally Posted by ktsirig
    Hello,
    I wanted to ask if anyone knows of a way to prevent injection in an SQL SERVER 2005. I mean, is there any way to do all the blocking in the server and not have to escape each special character one-by-one?
    For example, in PHP I used mysql_escape_string and automatically the string was OK to send to the database... Is there something similar in SQL Server?

    Thank you
    if you want to avoid attack sql injection, you have to user place holder in your app and avoid to generate sql statements

    it's wrong
    Code:
    strSQL="select * from table where field1='" &  anydata  &  "'"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured