这是一个例子
public IEnumerable<Product> ProductSearch(int? userID,DateTime? modifIEdAfter,DateTime? modifIEdBefore,GuID? productID) { IList<Product> products; using (var connection = _connection.OpenConnection()) { const string sproc = "dbo.stp_Product_Search"; products = connection.query<JobProduct>(sproc,new { User_ID = userID,ModifIEd_After = modifIEdAfter,ModifIEd_Before = modifIEdBefore,Product_ID = productID },commandType: CommandType.StoredProcedure) .ToList(); } return products; }
我有很多这样的代码,但使用了不同的参数和实体.有没有人有任何好的例子?
解决方法 感谢您的建议.这就是我最终使用的意思,这意味着我不必使用语句来编写每次使用少量代码行时打开连接:public class Repository<T> where T : class{ protected Readonly IComplianceConnection Connection; public Repository(IComplianceConnection connection) { Connection = connection; } public IEnumerable<T> Get(string query,object arguments) { IList<T> entitIEs; using (var connection = Connection.OpenConnection()) { entitIEs = connection.query<T>(query,arguments,commandType: CommandType.StoredProcedure).ToList(); } return entitIEs; } public T GetSingleOrDefault(string query,object arguments) { T entity; using (var connection = Connection.OpenConnection()) { entity = connection.query<T>(query,commandType: CommandType.StoredProcedure).SingleOrDefault(); } return entity; } public voID Update(string query,object arguments) { using (var connection = Connection.OpenConnection()) { connection.Execute(query,commandType: CommandType.StoredProcedure); } } public int ExecuteScalar(string query,object arguments) { var ID = 0; using (var connection = Connection.OpenConnection()) { ID = connection.ExecuteScalar<int>(query,commandType: CommandType.StoredProcedure); } return ID; }}总结
以上是内存溢出为你收集整理的DAL与小巧玲珑和C#全部内容,希望文章能够帮你解决DAL与小巧玲珑和C#所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)