我绝对是 C#(我来自Java)的新手,我对以下代码的工作表示怀疑。
因此,在对DB表执行某些操作的管理器类中,我有类似的东西:
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 + "%");
}
................................
................................
................................
}
}
}
所以我对前一个代码
中最后一个内容的确切表现有些怀疑在我看来,如果字段 nome (名称) MyUser 过滤器对象不为空且不为空,则用于对 strWHERE进行定值字符串(表示查询WHERE condiction)。
我无法理解的两件事是行内 @NOME 的确切内容
strWHERE += " AND nome like @NOME";
这究竟是怎么做的:
addParameter(command, "@NOME", "%" + filter.nome + "%");
TNX
安德烈
答案 0 :(得分:4)
@NOME
是查询的参数。 addParameter
将格式化的值替换为参数(转到addParameter
的定义以查看它的确切作用)。
答案 1 :(得分:2)
@Nome充当占位符。它告诉sql你要为查询添加一个参数。 然后addParameter函数告诉sql命令参数应解析的内容。
希望这有帮助。
快乐的C#'ing !!
答案 2 :(得分:1)
此代码存在一些主要缺陷。
1)为什么要将查询字符串声明为public const
??
2)您无法传递您尝试传递的参数。
使用Command对象的AddParameter
。
3)为什么要尝试在List
中获得结果我认为您需要使用DataSet
或DataTable
来填充然后尝试将结果添加到自定义列表中。