Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SQLCommand parameters to prevent SQL injections

SQLCommand parameters to prevent SQL injections

Scheduled Pinned Locked Moved C#
databasequestion
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GermanDM
    wrote on last edited by
    #1

    Hi, Would this be a good way to prevent some SQL Injections? I am trying to find the best easiest way to use of command parameters whenever i receive user input to query a database. I have alot of queries so im trying to find a easy reusable procedures to make use of sqlcommand parameters. SqlConnection conn = new SqlConnection(_WebConfig.ConnectionString.ToString()); try { DataTable dtVal = new DataTable(); SqlDataAdapter da = new SqlDataAdapter("SELECT ID, Full_Name, Surname " + "FROM Users " + "WHERE Full_Name = @LoginUname " + "AND Password = @LoginPword", conn); da.SelectCommand.Parameters.Add("@LoginUname", SqlDbType.VarChar, 30); da.SelectCommand.Parameters["@LoginUname"].Value = LoginUname; da.SelectCommand.Parameters.Add("@LoginPword", SqlDbType.VarChar, 15); da.SelectCommand.Parameters["@LoginPword"].Value = LoginPword; if (conn.State == ConnectionState.Closed | conn.State == ConnectionState.Closed) { conn.Open(); } da.Fill(dtVal); if (dtVal.Rows.Count == 1) { return true; } else { return false; } } catch (Exception ex) { throw ex; } finally { conn.Close(); conn.Dispose(); } Any advice or other methods would be greatly appreciated Thanks

    S 1 Reply Last reply
    0
    • G GermanDM

      Hi, Would this be a good way to prevent some SQL Injections? I am trying to find the best easiest way to use of command parameters whenever i receive user input to query a database. I have alot of queries so im trying to find a easy reusable procedures to make use of sqlcommand parameters. SqlConnection conn = new SqlConnection(_WebConfig.ConnectionString.ToString()); try { DataTable dtVal = new DataTable(); SqlDataAdapter da = new SqlDataAdapter("SELECT ID, Full_Name, Surname " + "FROM Users " + "WHERE Full_Name = @LoginUname " + "AND Password = @LoginPword", conn); da.SelectCommand.Parameters.Add("@LoginUname", SqlDbType.VarChar, 30); da.SelectCommand.Parameters["@LoginUname"].Value = LoginUname; da.SelectCommand.Parameters.Add("@LoginPword", SqlDbType.VarChar, 15); da.SelectCommand.Parameters["@LoginPword"].Value = LoginPword; if (conn.State == ConnectionState.Closed | conn.State == ConnectionState.Closed) { conn.Open(); } da.Fill(dtVal); if (dtVal.Rows.Count == 1) { return true; } else { return false; } } catch (Exception ex) { throw ex; } finally { conn.Close(); conn.Dispose(); } Any advice or other methods would be greatly appreciated Thanks

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      SQL Injection Attacks and Some Tips on How to Prevent Them[^]


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups