我绝对是 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/