111,109
社区成员




var result = new List<MyDataType>();
using (var conn = SqlHelper.OpenConnection())
{
var cmd = SqlHelper.GetCommand(conn, "select * from [table] where depart='技术部' and name<>@name");
SqlHelper.SetParameter(cmd, "name", "张三");
result = (from IDataRecord rd in cmd.ExecuteReader()
select new DataType
{
Name = (string)rd["Name"],
Cost = (decimal)rd["Cost"]
}).ToList();
}
这样一个查询,直接就查询出 List<MyDataType> 类型的对象集合。性能高(不需要在搞什么 DataTable 到强类型集合的转换),从代码上看确实没有什么废话。
从代码上看,这种 SqlHelper 要帮助做的,是创建 DbConnection、创建 DbCommand、填充参数查询变量。而对于返回查询结果,还是直接在调用程序中直接写这样的一条 Linq select语句就行了,这个功能上没有必要过度封装什么 SqlHelper。