Reputation: 2845
I've got the following c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringTest
{
class Program
{
static void Main(string[] args)
{
String strSQLCode;
strSQLCode = " select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * "
+= " from view_dg_game_details gd (nolock) "
+= " where gd.gametypeid = {0} "
+= " and gd.numberofrounds = {1} "
+= " and gd.gamevalues = '{2}' ";
}
}
}
For some reason I'm getting an error "The left-hand side of an assignment must be a variable, property or indexer".
I can't see what the error is trying to tell me. I've commented out the offending line but the error simply moves up a line.
I can get the string concation to working using this method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringTest
{
class Program
{
static void Main(string[] args)
{
String strSQLCode;
strSQLCode = " select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * ";
strSQLCode = strSQLCode + " from view_dg_game_details gd (nolock) ";
strSQLCode = strSQLCode + " where gd.gametypeid = {0} ";
strSQLCode = strSQLCode + " and gd.numberofrounds = {1} ";
strSQLCode = strSQLCode + " and gd.gamevalues = '{2}' ";
}
}
}
Can someone explain to me what this error is about?
Thanks
Ken
Upvotes: 1
Views: 250
Reputation: 12624
What you're doing is effectively:
string variable = "string" += "another string";
this is essentially the same as:
string variable;
(variable = "string") += "another string";
Because the result of the parenthetical expression is a string (specifically the value that was assigned), you're now effectively doing this:
string variable;
variable = "string";
"string" += "another string;
And the compiler has a problem with that third line.
Specifically, what the compiler is telling you is that in order to perform the assignment, you have to have something to assign to.
Write it like this:
strSQLCode = @" select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, *
from view_dg_game_details gd (nolock)
where gd.gametypeid = @gameType
and gd.numberofrounds = @numberOfRounds
and gd.gamevalues = @gameValues ";
And use a parameterized query.
Upvotes: 1
Reputation: 1880
like everyone else has mentioned += should of being +. Please if your constructing SQL at least parameterise it, SQL injection is a serious problem. I could from console or winapp textbox drop tables in your db. From first variable you can do
1 ; drop table dg_game_details --
example:
conDatabase =
new SqlConnection("Data Source=(local);" +
"Database='projectGames';" +
"Integrated Security=true");
SqlCommand cmdDatabase =
new SqlCommand("SELECT rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * FROM view_dg_game_details gd (nolock)" +
"WHERE gd.gametypeid= @GameId;", conDatabase);
cmdDatabase.Parameters.Add("@GameId", SqlDbType.Int);
cmdDatabase.Parameters["@GameId"].Value = 1;
Upvotes: 1
Reputation: 81557
In your first code snippet you should not be using +=
A simple will do +
From MSDN:
An expression using the += assignment operator, such as
x += y
is equivalent to
x = x + y
except that x is only evaluated once.
This means that you cannot use the +=
to chain concatenate a bunch of string literals or more than two variables.
Upvotes: 2
Reputation: 170
Use + instead of +=.
Plus, I strongly recommend not to store and concatenate your SQL queries like this because that way it's very unsecure due to SQL injections.
Read about it here: SQL injection
Upvotes: 0
Reputation: 245499
Because you can't string together +=
operators without repeating the variable that you're operating on:
strSQLCode = @"select rank() over (order by percentagecorrect desc,
totalmilliseconds asc) as rank, * ";
strSQLCode += " from view_dg_game_details gd (nolock) ";
strSQLCode += " where gd.gametypeid = {0} ";
strSQLCode += " and gd.numberofrounds = {1} ";
strSQLCode += " and gd.gamevalues = '{2}' ";
If you want to declare it as a "long" one liner, just use +
strSQLCode = @"select rank() over (order by percentagecorrect desc,
totalmilliseconds asc) as rank, * "
+ " from view_dg_game_details gd (nolock) "
+ " where gd.gametypeid = {0} "
+ " and gd.numberofrounds = {1} "
+ " and gd.gamevalues = '{2}' ";
Or, if you don't want any of that, you could just use a single string literal:
strSQLCode =
@"select rank() over (order by percentagecorrect desc,
totalmilliseconds asc) as rank, *
from view_dg_game_details gd (nolock)
where gd.gametypeid = {0}
and gd.numberofrounds = {1}
and gd.gamevalues = '{2}' ";
Upvotes: 12
Reputation: 326
Your syntax is slightly wrong.
Should be:
namespace StringTest
{
class Program
{
static void Main(string[] args)
{
String strSQLCode;
strSQLCode = @" select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * "
+ @" from view_dg_game_details gd (nolock) "
+ @" where gd.gametypeid = {0} "
+ @" and gd.numberofrounds = {1} "
+ @" and gd.gamevalues = '{2}' ";
}
}
}
Upvotes: 1
Reputation: 116188
Just use it this way
strSQLCode = " select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * "
+ " from view_dg_game_details gd (nolock) "
+ " where gd.gametypeid = {0} "
+ " and gd.numberofrounds = {1} "
+ " and gd.gamevalues = '{2}' ";
or
strSQLCode =
@"select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, *
from view_dg_game_details gd (nolock)
where gd.gametypeid = {0}
and gd.numberofrounds = {1}
and gd.gamevalues = '{2}' ";
Upvotes: 2
Reputation: 2655
This is a single statement, so you should be using the following:
strSQLCode = " select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * "
+ " from view_dg_game_details gd (nolock) "
+ " where gd.gametypeid = {0} "
+ " and gd.numberofrounds = {1} "
+ " and gd.gamevalues = '{2}' ";
Upvotes: 3
Reputation: 65166
For your first snippet, what you want is +
, not +=
.
You only want to assign to the variable once, and you do that after concatenating all the parts together in the normal way. And that's +
.
Upvotes: 2