AndreaNobili
AndreaNobili

Reputation: 42967

Some doubts about how this query is handled in C#?

I am absolutly new in C# (I came from Java) and I have a doubt about work the following code.

So in a manager class that perform some operation on a DB table I have something like it:

public class UserManager : ManagerDB
{

    // Define the field to select on a t1 database table:
    public const string _sqlElencoUtenti = "SELECT  t1.user_id, t1.my_login, t1.nome, t1.cognome , t1.email,  t1.date_added, t1.date_last_login, t1.is_enabled, t1.customer_id , t1.uidAD ";

    // Return a list of MyUser object filtered according to the values that are contained into a MyUser object
    public List<Models.MyUser> getList(out int totalRecords, Models.MyUser filter, int pageSize = -1, int pageIndex = -1, string sort = "MY_LOGIN", string sortOrder = "ASC")
    {

        List<Models.MyUser> result;
        result= new List<Models.MyUser>();

        // Selection of the interesting fields from UTENTE DB table:
        _strSQL = _sqlElencoUtenti + " FROM UTENTE as t1";

        System.Data.Common.DbCommand command;
        command = _connection.CreateCommand();

        string strWHERE = "";    // Beginning WHERE condiction

        if (filter != null) {    // If exist a filter object use it to filter the query
            // If the MyUser filter object have the name field that is not null and not empty:
            if (!String.IsNullOrEmpty(filter.nome)){
                // Create the new strWHERE that represent my where condicton
                strWHERE += " AND nome like  @NOME";
                addParameter(command, "@NOME", "%" + filter.nome + "%");
            }

            ................................
            ................................
            ................................
            }
    }
}

So I have some doubt about what exactly perform inside the last if of the previous code

It seems to me that if the field nome (the name) MyUser filter object is not null and not an empty it is used to valorize the strWHERE String (that represent the query WHERE condiction).

The 2 things that I can't understand is what is the exact roole of the @NOME inside the line

strWHERE += " AND nome like  @NOME";

and what exactly do this other line:

addParameter(command, "@NOME", "%" + filter.nome + "%");

Tnx

Andrea

Upvotes: 1

Views: 86

Answers (3)

Aftab Ahmed
Aftab Ahmed

Reputation: 1737

There are some major flaws in this code.

1) Why you are declaring query string as public const ??

2) You can't pass parameters you are trying to pass.

Use AddParameter of Command object.

3) Why you are trying to get results in List i think you need to use either DataSet or DataTable to fill then try to add results to your custom list.

Upvotes: 1

Spook Kruger
Spook Kruger

Reputation: 367

@Nome acts as a placeholder. It tells sql that you are going to add an parameter to the query. The addParameter function then tells the sql command what the parameter should resolve to.

Hope this helps.

Happy C#'ing!!

Upvotes: 2

Konstantin Dinev
Konstantin Dinev

Reputation: 34905

@NOME is a parameter to the query. The addParameter replaces the parameter with a formatted value (go to definition of the addParameter to see what it does exactly).

Upvotes: 4

Related Questions