<p>.NET, Java and other high level database API's in various language often provide techniques known as prepared statements and parameter binding as opposed to sending plain text commands to the Database server. What I would like to know is what happens when you execute a statement like this:</p>

<pre><code>SqlCommand cmd = new SqlCommand("GetMemberByID");
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@ID", memberID);
para.DbType = DbType.Integer;
cmd.Parameters.Add(param);
</code></pre>

<p>I know this is a best practice. SQL injection attacks are minimized this way. But what exactly happens under the hood when you execute these statements? Is the end result still a SQL safe string?  If not, what is the end result? And is this enough to prevent SQL injection attacks?</p>

[{'answer_id': 14946, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 4, 'selected': True, 'text': '<p><a href="http://dev.mysql.com/doc/refman/5.0/en/c-api-prepared-statements.html" rel="nofollow noreferrer">The MySQL manual page</a> on prepared statements provides lots of information (which should apply to any other RDBMS).</p>\n\n<p>Basically, your statement is parsed and processed ahead of time, and the parameters are sent separately instead of being handled along with the SQL code.  This eliminates SQL-injection attacks because the SQL is parsed before the parameters are even set.</p>\n'}, {'answer_id': 14986, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 0, 'selected': False, 'text': '<p>If you\'re using MS SQL, load up the profiler and you\'ll see what SQL statements are generated when you use parameterised queries. Here\'s an example (I\'m using Enterprise Libary 3.1, but the results are the same using SqlParameters directly) against SQL Server 2005:</p>\n\n<pre><code>string sql = "SELECT * FROM tblDomains WHERE DomainName = @DomName AND DomainID = @Did";\nDatabase db = DatabaseFactory.CreateDatabase();\nusing(DbCommand cmd = db.GetSqlStringCommand(sql))\n{\n  db.AddInParameter(cmd, "DomName", DbType.String, "xxxxx.net");\n  db.AddInParameter(cmd, "Did", DbType.Int32, 500204);\n\n  DataSet ds = db.ExecuteDataSet(cmd);\n}\n</code></pre>\n\n<p>This generates:</p>\n\n<pre><code>exec sp[underscore]executesql N\'SELECT * FROM tblDomains WHERE DomainName = @DomName AND DomainID = @Did\',\n  N\'@DomName nvarchar(9),\n  @Did int\',\n  @DomName=N\'xxxxx.net\',\n  @Did=500204\n</code></pre>\n\n<p>You can also see here, if quotation characters were passed as parameters, they are escaped accordingly:</p>\n\n<pre><code>db.AddInParameter(cmd, "DomName", DbType.String, "\'xxxxx.net");\n\nexec sp[underscore]executesql N\'SELECT * FROM tblDomains WHERE DomainName = @DomName AND DomainID = @Did\',\n  N\'@DomName nvarchar(10),\n  @Did int\',\n  @DomName=N\'\'\'xxxxx.net\',\n  @Did=500204\n</code></pre>\n'}, {'answer_id': 14995, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 0, 'selected': False, 'text': "<p>in layman terms: if a prepared statement is sent then the DB will use a plan if it is available, it doesn't not have to recreate a plan every time this query is sent over but only the values of the params have changed. this is very similar to how procs work, the additional benefit with procs is that you can give permission through procs only and not to the underlying tables at all</p>\n"}]
['https://Stackoverflow.com/questions/14934', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']