发布时间:2022-09-14 13:00
github: https://github.com/zichenlbl/Learn-Dot-Net-Core 的文件夹《Clone连接PostgreSQL》->《Demo - try语句中使用》
gitee:https://gitee.com/hujinya_admin/learn-dot-net-core 的文件夹《Clone连接PostgreSQL》->《Demo - try语句中使用》
环境:Win10,Vs2017,PostgreSQL10
项目:ASP.NET Core Web应用程序 -> Web应用程序(模型视图控制器) -> 取消勾选为HTTP配置 -> End.
<PackageReference Include="Chloe.PostgreSQL" Version="5.1.1" />
<PackageReference Include="Npgsql" Version="6.0.5" />
Chloe框架文档:https://github.com/shuxinqin/Chloe/wiki/PostgreSQL-DbContext
public class PostgreSQLConnectionFactory : IDbConnectionFactory
{
string _connString = null;
public PostgreSQLConnectionFactory(string connString)
{
this._connString = connString;
}
public IDbConnection CreateConnection()
{
NpgsqlConnection conn = new NpgsqlConnection(this._connString);
return conn;
}
}
DBConnectionString
本地的postgreSQL数据库,默认的postgres用户,密码以及我的数据库名称mydb
public class DBConnectionString
{
///
/// PostgreSQL数据库连接字符串
///
public static string POSTGRE_SQL = "Host=127.0.0.1;Username=postgres;Password=123456;Database=mydb";
}
User
public class User
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
Result
///
/// 前台Ajax请求的统一返回结果类
///
public class Result
{
private Result()
{
}
private ErrCode mErrCode = ErrCode.Success;
///
/// 返回编码
///
public ErrCode S_ErrCode { get { return mErrCode; } set { mErrCode = value; } }
///
/// 错误信息,或者成功信息
///
public string Message { get; set; }
///
/// 成功可能时返回的数据
///
public object Data { get; set; }
#region Error
public static Result Error()
{
return new Result()
{
S_ErrCode = ErrCode.Err
};
}
public static Result Error(string message)
{
return new Result()
{
S_ErrCode = ErrCode.Err,
Message = message
};
}
public static Result Error(string message, ErrCode pErrCode)
{
return new Result()
{
S_ErrCode = pErrCode,
Message = message
};
}
#endregion
#region Success
public static Result Success()
{
return new Result()
{
S_ErrCode = ErrCode.Success
};
}
public static Result Success(string message)
{
return new Result()
{
S_ErrCode = ErrCode.Success,
Message = message
};
}
public static Result Success(object data)
{
return new Result()
{
S_ErrCode = ErrCode.Success,
Data = data
};
}
public static Result Success(object data, string message)
{
return new Result()
{
S_ErrCode = ErrCode.Success,
Data = data,
Message = message
};
}
#endregion
///
/// 错误类型
///
public enum ErrCode
{
///
/// 正常
///
Success = 0,
///
/// 服务器内部错误
///
ServerErr = -100,
}
}
IUserService
public interface IUserService
{
///
/// 查询所有的用户
///
///
Result GetAllUser();
}
UserServiceImpl
public class UserServiceImpl : IUserService
{
public UserServiceImpl()
{
}
///
/// 查询所有的用户
///
///
public Result GetAllUser()
{
//获取PostgreSQLContext实例,表示与数据库的会话,可用于查询和保存实体。
PostgreSQLContext context = new Chloe.PostgreSQL.PostgreSQLContext(new PostgreSQLConnectionFactory(DBConnectionString.POSTGRE_SQL));
try
{//查询用户表
IQuery<User> m_q_user = context.Query<User>();
var mGetUser = m_q_user.OrderBy(a => a.id).ToList();
return Result.Success(mGetUser, "查询成功"); //返回用户信息
}
catch (Exception ex)
{
return Result.Error(ex.Message, Result.ErrCode.ServerErr);
}
finally
{
context.Dispose();
}
}
}
UserController
创建UserController控制器,修改Get()方法的返回类型为返回结果类Result,调用业务接口GetAllUser();方法获取所有用户的信息。
// GET: api/User
[HttpGet]
public Result Get()
{
IUserService userService = new UserServiceImpl();
return userService.GetAllUser();
}
接口查询结果
使用PgAdmin查看数据库中的结果
新增数据,再进行查询