c# - 对 C# 中如何处理此查询有一些疑问?

标签 c#

我绝对是 C# 的新手(我来自 Java),我对以下代码的工作有疑问。

所以在对数据库表执行某些操作的管理器类中,我有类似的东西:

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 + "%");
            }

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

所以我对前面代码的最后一个 if 到底执行了什么有一些疑问

在我看来,如果字段 nome(名称)MyUser 过滤器对象不为空且不为空,则它用于确定 strWHERE 字符串(表示查询 WHERE 条件)。

我不明白的两件事是 @NOME 的确切作用是什么

strWHERE += " AND nome like  @NOME";

这另一行到底是做什么的:

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

谢谢

安德里亚

最佳答案

@NOME 是查询的参数。 addParameter 将参数替换为格式化值(转到 addParameter 的定义以查看其具体作用)。

关于c# - 对 C# 中如何处理此查询有一些疑问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21879116/

相关文章:

c# - 我应该打开与本地 SQLite 数据库的单个还是多个连接?

c# - 如何删除数据 GridView 中的一行并在删除按钮单击事件时更新 MySQL 数据库?

c# - SqlParameter 和 IN 语句

c# - 我应该如何使用 Entity Framework 实体分部类?

c# - 如何停止此算法循环?

c# - 如何将 DTO 的 ODataQueryOptions 应用于底层 EntitySet?

c# - ASP.Net Core - 将 SAML 断言转换为 ClaimsPrincipal

c# - XNA : How to use double value within Draw() method instead of float to get precise rotation?

c# - 显示图像的时间很短

c# - 针对特定异常的单元测试异步方法