Click here to Skip to main content
16,016,562 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi guys,

I have wrote one inline query using C# for the SqlConnection command.Text

this is how am doing, command.Text= "{ select * from table}" why does this "{" and "}" coming before and ending of the actual query .Any clues please do reply

What I have tried:

C#
private List<objecttype> GetFullData(int id, int[] array1)
{

            DbConnection conn = null;
            DbCommand command = null;
            DbDataReader reader = null;
<pre>var templist = new List<objecttype>();
            try
            {  conn = DB.GetConnection();
                command = conn.CreateCommand();              
                command.CommandType = CommandType.Text;              
                var sbSql = new StringBuilder(
                    @"
          SELECT * from table(long inner join of 2 tables)");
                if (workgroups.Length > 0)
                {
                    sbSql.AppendFormat("WHERE column_3 IN({0})", string.Join(",", array1));
                }
                command.CommandText = sbSql.ToString();
                reader = command.ExecuteReader();


}
Posted
Updated 9-Aug-17 6:12am

We can't tell - there is nothing there that should add curly brackets to your string builder.
So either it's in the redacted JOIN, or it's not in that code at all.
Start with the debugger, and check the StringBuilder content while it's running, and see if you can identify exactly where the brackets are going - that may give you a clue as to where they are coming from, and it will at least ensure that it is that exact code that generates them.
If that doesn't help, truncate the SQL string until they disappear, and look at the last bit you removed. It doesn't matter that the SQL won't work, as you won't be executing it anyway!

Sorry, but we can't do any of that for you!
 
Share this answer
 
NEVER use string concatenation (or string.Format, or AppendFormat, or interpolated strings *, ...) to build a SQL query. ALWAYS use a parameterized query.

In this particular case, since your parameters are all int values, you've avoided a SQL Injection[^] vulnerability. But it's a bad habit to start, and it's far too easy to slip up and use string concatenation for things that aren't safe.

Passing multiple parameter values correctly is slightly more complicated than it should be. But it's not particularly difficult:
C#
private List<objecttype> GetFullData(int id, int[] workgroups)
{
    try
    {
        using (var conn = DB.GetConnection())
        using (var command = conn.CreateCommand())
        {
            command.CommandType = CommandType.Text;
            
            var sb = new StringBuilder(@"SELECT * FROM ...");
            if (workgrouns.Length != 0)
            {
                sb.Append(" WHERE column_3 IN (");
                for (int index = 0; index < workgroups.Length; index++)
                {
                    if (index != 0) sb.Append(", ");
                    
                    string name = "@p" + index;
                    command.Parameters.AddWithName(name, workgroups[index]);
                    sb.Append(name);
                }
                sb.Append(")");
            }
            
            command.CommandText = sb.ToString();
            
            using (var reader = command.ExecuteReader())
            {
                ...


You might want to take a look at Dapper[^], which makes this sort of query a lot easier.


* There was an article which explained how to do this safely, but I can't find the link at the moment.


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900