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)










0















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:



  1. I passed SourceColumn and SourceValue to SqlCommand using SqlParameter, will this make it SQL injection proof?

  2. Do I need to use TargetColumn together with SqlParameter too for SQL safety purpose? (but it is for SqlDataReader)

  3. 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?

  4. 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!










share|improve this question

















  • 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
















0















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:



  1. I passed SourceColumn and SourceValue to SqlCommand using SqlParameter, will this make it SQL injection proof?

  2. Do I need to use TargetColumn together with SqlParameter too for SQL safety purpose? (but it is for SqlDataReader)

  3. 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?

  4. 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!










share|improve this question

















  • 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














0












0








0








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:



  1. I passed SourceColumn and SourceValue to SqlCommand using SqlParameter, will this make it SQL injection proof?

  2. Do I need to use TargetColumn together with SqlParameter too for SQL safety purpose? (but it is for SqlDataReader)

  3. 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?

  4. 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!










share|improve this question














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:



  1. I passed SourceColumn and SourceValue to SqlCommand using SqlParameter, will this make it SQL injection proof?

  2. Do I need to use TargetColumn together with SqlParameter too for SQL safety purpose? (but it is for SqlDataReader)

  3. 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?

  4. 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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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













  • 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








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













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
);



);













draft saved

draft discarded


















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















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme