Insert string into SQL query without quotes
up vote
-4
down vote
favorite
I need to make a query that looks like this:
SELECT * FROM Table WHERE Row.DATA = value
Where DATA
I need to pass through SqlParameter
. If I do something like this:
string value = "DATA";
SqlCommand sql = new SqlCommand("SELECT * FROM Table WHERE Row.@Val = value");
sql.Parameters.Add("@Val", SqlDbType.VarChar).Value = value;
I get following query which is invalid:
SELECT * FROM Table WHERE Row.'DATA' = value
c#
New contributor
add a comment |
up vote
-4
down vote
favorite
I need to make a query that looks like this:
SELECT * FROM Table WHERE Row.DATA = value
Where DATA
I need to pass through SqlParameter
. If I do something like this:
string value = "DATA";
SqlCommand sql = new SqlCommand("SELECT * FROM Table WHERE Row.@Val = value");
sql.Parameters.Add("@Val", SqlDbType.VarChar).Value = value;
I get following query which is invalid:
SELECT * FROM Table WHERE Row.'DATA' = value
c#
New contributor
2
The Parameter is supposed to be your value, not data
– Hooman
yesterday
1
SqlParameters
used to provide values, they're not intended to pass table names, schemas or other things than potentially assigned values.
– Tetsuya Yamamoto
yesterday
Additionally, tryPREPARE stmt1 FROM "yourqueryhere"
thenEXECUTE stmt1 USING @Val
to execute the query.
– Tetsuya Yamamoto
yesterday
add a comment |
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
I need to make a query that looks like this:
SELECT * FROM Table WHERE Row.DATA = value
Where DATA
I need to pass through SqlParameter
. If I do something like this:
string value = "DATA";
SqlCommand sql = new SqlCommand("SELECT * FROM Table WHERE Row.@Val = value");
sql.Parameters.Add("@Val", SqlDbType.VarChar).Value = value;
I get following query which is invalid:
SELECT * FROM Table WHERE Row.'DATA' = value
c#
New contributor
I need to make a query that looks like this:
SELECT * FROM Table WHERE Row.DATA = value
Where DATA
I need to pass through SqlParameter
. If I do something like this:
string value = "DATA";
SqlCommand sql = new SqlCommand("SELECT * FROM Table WHERE Row.@Val = value");
sql.Parameters.Add("@Val", SqlDbType.VarChar).Value = value;
I get following query which is invalid:
SELECT * FROM Table WHERE Row.'DATA' = value
c#
c#
New contributor
New contributor
edited yesterday
Bradley Grainger
19.2k36386
19.2k36386
New contributor
asked yesterday
Vlad Gavrilov
1
1
New contributor
New contributor
2
The Parameter is supposed to be your value, not data
– Hooman
yesterday
1
SqlParameters
used to provide values, they're not intended to pass table names, schemas or other things than potentially assigned values.
– Tetsuya Yamamoto
yesterday
Additionally, tryPREPARE stmt1 FROM "yourqueryhere"
thenEXECUTE stmt1 USING @Val
to execute the query.
– Tetsuya Yamamoto
yesterday
add a comment |
2
The Parameter is supposed to be your value, not data
– Hooman
yesterday
1
SqlParameters
used to provide values, they're not intended to pass table names, schemas or other things than potentially assigned values.
– Tetsuya Yamamoto
yesterday
Additionally, tryPREPARE stmt1 FROM "yourqueryhere"
thenEXECUTE stmt1 USING @Val
to execute the query.
– Tetsuya Yamamoto
yesterday
2
2
The Parameter is supposed to be your value, not data
– Hooman
yesterday
The Parameter is supposed to be your value, not data
– Hooman
yesterday
1
1
SqlParameters
used to provide values, they're not intended to pass table names, schemas or other things than potentially assigned values.– Tetsuya Yamamoto
yesterday
SqlParameters
used to provide values, they're not intended to pass table names, schemas or other things than potentially assigned values.– Tetsuya Yamamoto
yesterday
Additionally, try
PREPARE stmt1 FROM "yourqueryhere"
then EXECUTE stmt1 USING @Val
to execute the query.– Tetsuya Yamamoto
yesterday
Additionally, try
PREPARE stmt1 FROM "yourqueryhere"
then EXECUTE stmt1 USING @Val
to execute the query.– Tetsuya Yamamoto
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
string value = "DATA";
SqlCommand sql = new SqlCommand($"SELECT * FROM Table WHERE Row.value = value");
1
I know that this is one simple solution. But isn't it insecure?
– Vlad Gavrilov
yesterday
1
This will work only if the OP is extremely careful and only uses known strings forvalue
.
– Panagiotis Kanavos
yesterday
@VladGavrilov I think it's security because injection come from the value (in the right), with a strange value for the column , the query will fail
– Antoine V
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
string value = "DATA";
SqlCommand sql = new SqlCommand($"SELECT * FROM Table WHERE Row.value = value");
1
I know that this is one simple solution. But isn't it insecure?
– Vlad Gavrilov
yesterday
1
This will work only if the OP is extremely careful and only uses known strings forvalue
.
– Panagiotis Kanavos
yesterday
@VladGavrilov I think it's security because injection come from the value (in the right), with a strange value for the column , the query will fail
– Antoine V
yesterday
add a comment |
up vote
1
down vote
string value = "DATA";
SqlCommand sql = new SqlCommand($"SELECT * FROM Table WHERE Row.value = value");
1
I know that this is one simple solution. But isn't it insecure?
– Vlad Gavrilov
yesterday
1
This will work only if the OP is extremely careful and only uses known strings forvalue
.
– Panagiotis Kanavos
yesterday
@VladGavrilov I think it's security because injection come from the value (in the right), with a strange value for the column , the query will fail
– Antoine V
yesterday
add a comment |
up vote
1
down vote
up vote
1
down vote
string value = "DATA";
SqlCommand sql = new SqlCommand($"SELECT * FROM Table WHERE Row.value = value");
string value = "DATA";
SqlCommand sql = new SqlCommand($"SELECT * FROM Table WHERE Row.value = value");
answered yesterday
Antoine V
4,7942422
4,7942422
1
I know that this is one simple solution. But isn't it insecure?
– Vlad Gavrilov
yesterday
1
This will work only if the OP is extremely careful and only uses known strings forvalue
.
– Panagiotis Kanavos
yesterday
@VladGavrilov I think it's security because injection come from the value (in the right), with a strange value for the column , the query will fail
– Antoine V
yesterday
add a comment |
1
I know that this is one simple solution. But isn't it insecure?
– Vlad Gavrilov
yesterday
1
This will work only if the OP is extremely careful and only uses known strings forvalue
.
– Panagiotis Kanavos
yesterday
@VladGavrilov I think it's security because injection come from the value (in the right), with a strange value for the column , the query will fail
– Antoine V
yesterday
1
1
I know that this is one simple solution. But isn't it insecure?
– Vlad Gavrilov
yesterday
I know that this is one simple solution. But isn't it insecure?
– Vlad Gavrilov
yesterday
1
1
This will work only if the OP is extremely careful and only uses known strings for
value
.– Panagiotis Kanavos
yesterday
This will work only if the OP is extremely careful and only uses known strings for
value
.– Panagiotis Kanavos
yesterday
@VladGavrilov I think it's security because injection come from the value (in the right), with a strange value for the column , the query will fail
– Antoine V
yesterday
@VladGavrilov I think it's security because injection come from the value (in the right), with a strange value for the column , the query will fail
– Antoine V
yesterday
add a comment |
Vlad Gavrilov is a new contributor. Be nice, and check out our Code of Conduct.
draft saved
draft discarded
Vlad Gavrilov is a new contributor. Be nice, and check out our Code of Conduct.
Vlad Gavrilov is a new contributor. Be nice, and check out our Code of Conduct.
Vlad Gavrilov is a new contributor. Be nice, and check out our Code of Conduct.
draft saved
draft discarded
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205214%2finsert-string-into-sql-query-without-quotes%23new-answer', 'question_page');
);
Post as a guest
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
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
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
2
The Parameter is supposed to be your value, not data
– Hooman
yesterday
1
SqlParameters
used to provide values, they're not intended to pass table names, schemas or other things than potentially assigned values.– Tetsuya Yamamoto
yesterday
Additionally, try
PREPARE stmt1 FROM "yourqueryhere"
thenEXECUTE stmt1 USING @Val
to execute the query.– Tetsuya Yamamoto
yesterday