Will this way of using SQLParameter make my function SQL injection proof?2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Are PDO prepared statements sufficient to prevent SQL injection?How does the SQL injection from the “Bobby Tables” XKCD comic work?Avoiding SQL injection without parametersFunction vs. Stored Procedure in SQL ServerSQL injection that gets around mysql_real_escape_string()Is this Sql-injection-proof Asp.net code?Using SqlParameter can avoid sql injection totally?Prevent SQL Injection when the table name and where clause are variablesHow to deal with this SQL injection warning (CA2100)
What does "Four-F." mean?
How do hiring committees for research positions view getting "scooped"?
How is the partial sum of a geometric sequence calculated?
What is the term when voters “dishonestly” choose something that they do not want to choose?
Pronounciation of the combination "st" in spanish accents
Is there a term for accumulated dirt on the outside of your hands and feet?
Comment Box for Substitution Method of Integrals
Help rendering a complicated sum/product formula
Maths symbols and unicode-math input inside siunitx commands
Deletion of copy-ctor & copy-assignment - public, private or protected?
Should I be concerned about student access to a test bank?
What does "^L" mean in C?
Is there a creature that is resistant or immune to non-magical damage other than bludgeoning, slashing, and piercing?
Is it insecure to send a password in a `curl` command?
Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?
Hausdorff dimension of the boundary of fibres of Lipschitz maps
I seem to dance, I am not a dancer. Who am I?
Practical application of matrices and determinants
Synchronized implementation of a bank account in Java
Why is there so much iron?
In Aliens, how many people were on LV-426 before the Marines arrived?
Probably overheated black color SMD pads
Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?
Have the tides ever turned twice on any open problem?
Will this way of using SQLParameter make my function SQL injection proof?
2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Are PDO prepared statements sufficient to prevent SQL injection?How does the SQL injection from the “Bobby Tables” XKCD comic work?Avoiding SQL injection without parametersFunction vs. Stored Procedure in SQL ServerSQL injection that gets around mysql_real_escape_string()Is this Sql-injection-proof Asp.net code?Using SqlParameter can avoid sql injection totally?Prevent SQL Injection when the table name and where clause are variablesHow to deal with this SQL injection warning (CA2100)
Here are some simple codes that search for the value in TargetColumn where SourceColumn = SourceValue.
Here are these codes:
string cmdText = "select * from " + TableName + " where " + SourceColumn + " = '" + SourceValue + "'";
SqlCommand dbCommand = new SqlCommand(cmdText, dbConnection);
SqlParameter sqlParam = new SqlParameter("@" + SourceColumn, SourceValue);
dbCommand.Parameters.Add(sqlParam);
SqlDataReader dbReader = dbCommand.ExecuteReader();
dbReader.Read();
string _targetValue = dbReader[TargetColumn].ToString();
dbReader.Close();
dbCommand.Dispose();
return _targetValue;
And my questions are:
- I passed SourceColumn and SourceValue to SqlCommand using SqlParameter, will this make it SQL injection proof?
- Do I need to use TargetColumn together with SqlParameter too for SQL safety purpose? (but it is for SqlDataReader)
- If I use SqlParameter for SqlCommand, do I still need to compose a command text in a string and pass it to SqlCommand before SqlParameter is used?
- Why do I need to add an "@" for SourceColumn? (I just followed the tutorial and added it) And why SourceValue doesn't need an "@"?
The above codes works well to return the expected value, but I'm so not sure about the above questions.
Thanks very much!
c# sql sql-server sql-injection
add a comment |
Here are some simple codes that search for the value in TargetColumn where SourceColumn = SourceValue.
Here are these codes:
string cmdText = "select * from " + TableName + " where " + SourceColumn + " = '" + SourceValue + "'";
SqlCommand dbCommand = new SqlCommand(cmdText, dbConnection);
SqlParameter sqlParam = new SqlParameter("@" + SourceColumn, SourceValue);
dbCommand.Parameters.Add(sqlParam);
SqlDataReader dbReader = dbCommand.ExecuteReader();
dbReader.Read();
string _targetValue = dbReader[TargetColumn].ToString();
dbReader.Close();
dbCommand.Dispose();
return _targetValue;
And my questions are:
- I passed SourceColumn and SourceValue to SqlCommand using SqlParameter, will this make it SQL injection proof?
- Do I need to use TargetColumn together with SqlParameter too for SQL safety purpose? (but it is for SqlDataReader)
- If I use SqlParameter for SqlCommand, do I still need to compose a command text in a string and pass it to SqlCommand before SqlParameter is used?
- Why do I need to add an "@" for SourceColumn? (I just followed the tutorial and added it) And why SourceValue doesn't need an "@"?
The above codes works well to return the expected value, but I'm so not sure about the above questions.
Thanks very much!
c# sql sql-server sql-injection
5
Not even close. You are creating parameters and not using them in your query. You are still just building up an unsafe string and executing it. But why of why are you selecting every column and then only returning one? This has all kinds of red flags everywhere but if you are determined to use a single method to read any table (highly unadvised) you should only select the column you need.
– Sean Lange
Mar 7 at 17:26
There is always a risk ofSQL Injection
when using string concatenation. If the user can input anything that is sent as SQL, there's a risk. Personally, I run checks on values and choose which table/stored procedure to run from there (meaning my users never have access to send SQL Injection attempts).
– Symon
Mar 7 at 17:26
add a comment |
Here are some simple codes that search for the value in TargetColumn where SourceColumn = SourceValue.
Here are these codes:
string cmdText = "select * from " + TableName + " where " + SourceColumn + " = '" + SourceValue + "'";
SqlCommand dbCommand = new SqlCommand(cmdText, dbConnection);
SqlParameter sqlParam = new SqlParameter("@" + SourceColumn, SourceValue);
dbCommand.Parameters.Add(sqlParam);
SqlDataReader dbReader = dbCommand.ExecuteReader();
dbReader.Read();
string _targetValue = dbReader[TargetColumn].ToString();
dbReader.Close();
dbCommand.Dispose();
return _targetValue;
And my questions are:
- I passed SourceColumn and SourceValue to SqlCommand using SqlParameter, will this make it SQL injection proof?
- Do I need to use TargetColumn together with SqlParameter too for SQL safety purpose? (but it is for SqlDataReader)
- If I use SqlParameter for SqlCommand, do I still need to compose a command text in a string and pass it to SqlCommand before SqlParameter is used?
- Why do I need to add an "@" for SourceColumn? (I just followed the tutorial and added it) And why SourceValue doesn't need an "@"?
The above codes works well to return the expected value, but I'm so not sure about the above questions.
Thanks very much!
c# sql sql-server sql-injection
Here are some simple codes that search for the value in TargetColumn where SourceColumn = SourceValue.
Here are these codes:
string cmdText = "select * from " + TableName + " where " + SourceColumn + " = '" + SourceValue + "'";
SqlCommand dbCommand = new SqlCommand(cmdText, dbConnection);
SqlParameter sqlParam = new SqlParameter("@" + SourceColumn, SourceValue);
dbCommand.Parameters.Add(sqlParam);
SqlDataReader dbReader = dbCommand.ExecuteReader();
dbReader.Read();
string _targetValue = dbReader[TargetColumn].ToString();
dbReader.Close();
dbCommand.Dispose();
return _targetValue;
And my questions are:
- I passed SourceColumn and SourceValue to SqlCommand using SqlParameter, will this make it SQL injection proof?
- Do I need to use TargetColumn together with SqlParameter too for SQL safety purpose? (but it is for SqlDataReader)
- If I use SqlParameter for SqlCommand, do I still need to compose a command text in a string and pass it to SqlCommand before SqlParameter is used?
- Why do I need to add an "@" for SourceColumn? (I just followed the tutorial and added it) And why SourceValue doesn't need an "@"?
The above codes works well to return the expected value, but I'm so not sure about the above questions.
Thanks very much!
c# sql sql-server sql-injection
c# sql sql-server sql-injection
asked Mar 7 at 17:23
solidcomersolidcomer
714
714
5
Not even close. You are creating parameters and not using them in your query. You are still just building up an unsafe string and executing it. But why of why are you selecting every column and then only returning one? This has all kinds of red flags everywhere but if you are determined to use a single method to read any table (highly unadvised) you should only select the column you need.
– Sean Lange
Mar 7 at 17:26
There is always a risk ofSQL Injection
when using string concatenation. If the user can input anything that is sent as SQL, there's a risk. Personally, I run checks on values and choose which table/stored procedure to run from there (meaning my users never have access to send SQL Injection attempts).
– Symon
Mar 7 at 17:26
add a comment |
5
Not even close. You are creating parameters and not using them in your query. You are still just building up an unsafe string and executing it. But why of why are you selecting every column and then only returning one? This has all kinds of red flags everywhere but if you are determined to use a single method to read any table (highly unadvised) you should only select the column you need.
– Sean Lange
Mar 7 at 17:26
There is always a risk ofSQL Injection
when using string concatenation. If the user can input anything that is sent as SQL, there's a risk. Personally, I run checks on values and choose which table/stored procedure to run from there (meaning my users never have access to send SQL Injection attempts).
– Symon
Mar 7 at 17:26
5
5
Not even close. You are creating parameters and not using them in your query. You are still just building up an unsafe string and executing it. But why of why are you selecting every column and then only returning one? This has all kinds of red flags everywhere but if you are determined to use a single method to read any table (highly unadvised) you should only select the column you need.
– Sean Lange
Mar 7 at 17:26
Not even close. You are creating parameters and not using them in your query. You are still just building up an unsafe string and executing it. But why of why are you selecting every column and then only returning one? This has all kinds of red flags everywhere but if you are determined to use a single method to read any table (highly unadvised) you should only select the column you need.
– Sean Lange
Mar 7 at 17:26
There is always a risk of
SQL Injection
when using string concatenation. If the user can input anything that is sent as SQL, there's a risk. Personally, I run checks on values and choose which table/stored procedure to run from there (meaning my users never have access to send SQL Injection attempts).– Symon
Mar 7 at 17:26
There is always a risk of
SQL Injection
when using string concatenation. If the user can input anything that is sent as SQL, there's a risk. Personally, I run checks on values and choose which table/stored procedure to run from there (meaning my users never have access to send SQL Injection attempts).– Symon
Mar 7 at 17:26
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049607%2fwill-this-way-of-using-sqlparameter-make-my-function-sql-injection-proof%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049607%2fwill-this-way-of-using-sqlparameter-make-my-function-sql-injection-proof%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Not even close. You are creating parameters and not using them in your query. You are still just building up an unsafe string and executing it. But why of why are you selecting every column and then only returning one? This has all kinds of red flags everywhere but if you are determined to use a single method to read any table (highly unadvised) you should only select the column you need.
– Sean Lange
Mar 7 at 17:26
There is always a risk of
SQL Injection
when using string concatenation. If the user can input anything that is sent as SQL, there's a risk. Personally, I run checks on values and choose which table/stored procedure to run from there (meaning my users never have access to send SQL Injection attempts).– Symon
Mar 7 at 17:26