一:环境
.net core 2.0 类库
在NuGet中引用:NPoco
二:类库代码
添加BaseQuery类:
using NPoco;
using System.Data.SqlClient;
namespace Query
{
public class BaseQuery
{
private const string sqlConn = "server=localhost;port=5006;database=testDb;user=sa;password=123456";
public static class Connection
{
public static IDatabase ConnGlobal => new Database(sqlConn, DatabaseType.SqlServer2012, SqlClientFactory.Instance);
}
}
}
在UserQuery类中运用
public class UserQuery : BaseQuery
{
public IEnumerable<User> Find()
{
try
{
using (var db = Connection.ConnGlobal)
{
var query = db.Query<User>().ToList();
var result = query;
return result;
}
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}
}
Over~
PS:关于NPoco参考:https://docs.microsoft.com/en-us/nuget/what-is-nuget