背景
某个我的项目须要实现根底软件全副国产化,其中操作系统指定河汉麒麟,数据库应用达梦 V8,CPU 平台的范畴包含 x64、龙芯、飞腾、鲲鹏等。思考到这些根底产品对.NET 的反对,最终抉择了.NET Core 3.1。
环境
- CPU 平台:x86-64 / Arm64
- 操作系统:河汉麒麟 v4
- 数据库:DM8
- .NET:.NET Core 3.1
SDK
达梦本人提供了.NET 操作其数据库的 SDK,能够通过 NuGet 装置,也能够通过装置达梦数据库获取。因为 NuGet 上的版本不晓得是谁提供的,所以这里以装置数据库获取相干 SDK 为例。
在官网下载 DM8 的数据库安装文件:https://www.dameng.com/list_1…
下载前须要先登录,轻易注册一个帐号就好了。
这里须要抉择 CPU 和操作系统,依照你的开发环境抉择即可,下载后依照提醒装置。
这里以 Windows10 为例,装置后 SDK 文件的地位在:C:\dmdbms\drivers\dotNet
这里边有 EF 的 SDK,也有 NHibernate 的 SDK,不过这篇文章只应用最根底的基于 ADO.NET 的 SDK。
这些 SDK 在文件夹 DmProvider 下边,这里还提供了一个 Nuget 包,能够放到本人的 Nuget 仓库中,不便外部装置。
能够看到,这个 SDK 能够反对.NET Core2.0 以上的所有.NET 版本。
操作数据库
这里提供两种形式:传统的 DbHelperSQL 形式 和 Dapper 形式。
DbHelperSQL 形式
这种形式早年用的比拟多,当初还有很多我的项目在应用,通过定义一组工具办法包装对数据库的各种增删改查操作。上面给出代码:
public class DmDbHelper
{
private string connectionString = string.Empty;
/// <summary>
/// 初始化 DMClient 的一个新实例
/// </summary>
/// <param name="str"></param>
public DmDbHelper(string str)
{connectionString = str;}
#region 通用快捷办法
/// <summary>
/// 执行一条 SQL 语句,确定记录是否存在
/// </summary>
/// <param name="sql">SQL 查问语句 </param>
/// <returns></returns>
public bool Exists(string sql)
{object obj = GetSingle(sql);
int cmdresult;
if (Equals(obj, null) || Equals(obj, DBNull.Value))
{cmdresult = 0;}
else
{cmdresult = int.Parse(obj.ToString());
}
return cmdresult > 0;
}
/// <summary>
/// 执行一条 SQL 语句,确定记录是否存在
/// </summary>
/// <param name="sql">SQL 查问语句 </param>
/// <returns></returns>
public async Task<bool> ExistsAsync(string sql)
{object obj = await GetSingleAsync(sql);
int cmdresult;
if (Equals(obj, null) || Equals(obj, DBNull.Value))
{cmdresult = 0;}
else
{cmdresult = int.Parse(obj.ToString());
}
return cmdresult > 0;
}
/// <summary>
/// 执行一条 SQL 语句,确定记录是否存在
/// </summary>
/// <param name="sql">SQL 查问语句 </param>
/// <param name="paras">SQL 参数数组 </param>
/// <returns></returns>
public bool Exists(string sql, params DmParameter[] paras)
{object obj = GetSingle(sql, paras);
int cmdresult;
if ((object.Equals(obj, null)) || (object.Equals(obj, DBNull.Value)))
{cmdresult = 0;}
else
{cmdresult = int.Parse(obj.ToString());
}
return cmdresult > 0;
}
/// <summary>
/// 执行一条 SQL 语句,确定记录是否存在
/// </summary>
/// <param name="sql">SQL 查问语句 </param>
/// <param name="paras">SQL 参数数组 </param>
/// <returns></returns>
public async Task<bool> ExistsAsync(string sql, params DmParameter[] paras)
{object obj = await GetSingleAsync(sql, paras);
int cmdresult;
if ((object.Equals(obj, null)) || (object.Equals(obj, DBNull.Value)))
{cmdresult = 0;}
else
{cmdresult = int.Parse(obj.ToString());
}
return cmdresult > 0;
}
/// <summary>
/// 获取记录条数
/// </summary>
/// <param name="tableName"> 表名 </param>
/// <param name="sqlCondition"> 查问条件 </param>
/// <returns></returns>
public int GetCount(string tableName, string sqlCondition)
{string sql = "select count(1) from `" + tableName + "`";
if (!string.IsNullOrWhiteSpace(sqlCondition))
{sql += "where" + sqlCondition;}
object result = GetSingle(sql);
if (result != null)
{return Convert.ToInt32(result);
}
else
{return 0;}
}
/// <summary>
/// 获取记录条数
/// </summary>
/// <param name="tableName"> 表名 </param>
/// <param name="sqlCondition"> 查问条件 </param>
/// <returns></returns>
public async Task<int> GetCountAsync(string tableName, string sqlCondition)
{string sql = "select count(1) from `" + tableName + "`";
if (!string.IsNullOrWhiteSpace(sqlCondition))
{sql += "where" + sqlCondition;}
object result = await GetSingleAsync(sql);
if (result != null)
{return Convert.ToInt32(result);
}
else
{return 0;}
}
/// <summary>
/// 获取记录条数
/// </summary>
/// <param name="tableName"> 表名 </param>
/// <param name="sqlCondition"> 查问条件 </param>
/// <param name="paras">SQL 参数数组 </param>
/// <returns></returns>
public int GetCount(string tableName, string sqlCondition, DmParameter[] paras)
{string sql = "select count(1) from `" + tableName + "`";
if (!string.IsNullOrWhiteSpace(sqlCondition))
{sql += "where" + sqlCondition;}
object result = GetSingle(sql, paras);
if (result != null)
{return Convert.ToInt32(result);
}
else
{return 0;}
}
/// <summary>
/// 获取记录条数
/// </summary>
/// <param name="tableName"> 表名 </param>
/// <param name="sqlCondition"> 查问条件 </param>
/// <param name="paras">SQL 参数数组 </param>
/// <returns></returns>
public async Task<int> GetCountAsync(string tableName, string sqlCondition, DmParameter[] paras)
{string sql = "select count(1) from `" + tableName + "`";
if (!string.IsNullOrWhiteSpace(sqlCondition))
{sql += "where" + sqlCondition;}
object result = await GetSingleAsync(sql, paras);
if (result != null)
{return Convert.ToInt32(result);
}
else
{return 0;}
}
#endregion 通用快捷办法
#region 执行简略 SQL 语句
/// <summary>
/// 执行 SQL 语句,返回影响的记录数
/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <returns> 影响的记录数 </returns>
public int ExecuteSql(string sql)
{using (DmConnection connection = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand(sql, connection))
{connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
}
}
/// <summary>
/// 执行 SQL 语句,返回影响的记录数
/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <returns> 影响的记录数 </returns>
public async Task<int> ExecuteSqlAsync(string sql)
{using (DmConnection connection = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand(sql, connection))
{await connection.OpenAsync();
int rows = await cmd.ExecuteNonQueryAsync();
return rows;
}
}
}
/// <summary>
/// 执行 SQL 语句,返回影响的记录数(可自定义超时工夫)/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <param name="timeout"> 执行超时工夫 </param>
/// <returns> 影响的记录数 </returns>
public int ExecuteSqlByTime(string sql, int timeout)
{using (DmConnection connection = new DmConnection(this.connectionString))
{using (DmCommand cmd = new DmCommand(sql, connection))
{connection.Open();
cmd.CommandTimeout = timeout;
int rows = cmd.ExecuteNonQuery();
return rows;
}
}
}
/// <summary>
/// 执行 SQL 语句,返回影响的记录数(可自定义超时工夫)/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <param name="timeout"> 执行超时工夫 </param>
/// <returns> 影响的记录数 </returns>
public async Task<int> ExecuteSqlByTimeAsync(string sql, int timeout)
{using (DmConnection connection = new DmConnection(this.connectionString))
{using (DmCommand cmd = new DmCommand(sql, connection))
{await connection.OpenAsync();
cmd.CommandTimeout = timeout;
int rows = await cmd.ExecuteNonQueryAsync();
return rows;
}
}
}
/// <summary>
/// 执行多条 SQL 语句,实现数据库事务。/// </summary>
/// <param name="sqlList"> 多条 SQL 语句 </param>
public void ExecuteSqlTrans(ArrayList sqlList)
{using (DmConnection conn = new DmConnection(connectionString))
{conn.Open();
using (DbTransaction trans = conn.BeginTransaction())
{using (DmCommand cmd = new DmCommand())
{
cmd.Connection = conn;
cmd.Transaction = trans;
try
{for (int n = 0; n < sqlList.Count; n++)
{string sql = sqlList[n].ToString();
if (sql.Trim().Length > 1)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();}
}
trans.Commit();}
catch (DmException ex)
{trans.Rollback();
throw ex;
}
}
}
}
}
/// <summary>
/// 执行多条 SQL 语句,实现数据库事务。/// </summary>
/// <param name="sqlList"> 多条 SQL 语句 </param>
public async Task ExecuteSqlTransAsync(ArrayList sqlList)
{using (DmConnection conn = new DmConnection(connectionString))
{await conn.OpenAsync();
using (DbTransaction trans = await conn.BeginTransactionAsync())
{using (DmCommand cmd = new DmCommand())
{
cmd.Connection = conn;
cmd.Transaction = trans;
try
{for (int n = 0; n < sqlList.Count; n++)
{string sql = sqlList[n].ToString();
if (sql.Trim().Length > 1)
{
cmd.CommandText = sql;
await cmd.ExecuteNonQueryAsync();}
}
trans.Commit();}
catch (DmException ex)
{trans.Rollback();
throw ex;
}
}
}
}
}
/// <summary>
/// 执行一条 SQL 查问语句,返回查问后果。/// </summary>
/// <param name="sql">SQL 查问语句 </param>
/// <returns> 查问后果 </returns>
public object GetSingle(string sql)
{using (DmConnection connection = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand(sql, connection))
{connection.Open();
object obj = cmd.ExecuteScalar();
if ((object.Equals(obj, null)) || (object.Equals(obj, DBNull.Value)))
{return null;}
else
{return obj;}
}
}
}
/// <summary>
/// 执行一条 SQL 查问语句,返回查问后果。/// </summary>
/// <param name="sql">SQL 查问语句 </param>
/// <returns> 查问后果 </returns>
public async Task<object> GetSingleAsync(string sql)
{using (DmConnection connection = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand(sql, connection))
{await connection.OpenAsync();
object obj = await cmd.ExecuteScalarAsync();
if ((object.Equals(obj, null)) || (object.Equals(obj, DBNull.Value)))
{return null;}
else
{return obj;}
}
}
}
/// <summary>
/// 执行查问语句,返回 DbDataReader(切记要手工敞开 DbDataReader)/// </summary>
/// <param name="sql"> 查问语句 </param>
/// <returns>DmDataReader</returns>
public DbDataReader ExecuteReader(string sql)
{DmConnection connection = new DmConnection(connectionString);
DmCommand cmd = new DmCommand(sql, connection);
connection.Open();
return cmd.ExecuteReader();}
/// <summary>
/// 执行查问语句,返回 DbDataReader(切记要手工敞开 DbDataReader)/// </summary>
/// <param name="sql"> 查问语句 </param>
/// <returns>DmDataReader</returns>
public async Task<DbDataReader> ExecuteReaderAsync(string sql)
{DmConnection connection = new DmConnection(connectionString);
DmCommand cmd = new DmCommand(sql, connection);
await connection.OpenAsync();
return await cmd.ExecuteReaderAsync();}
/// <summary>
/// 执行查问语句,返回 DataSet
/// </summary>
/// <param name="sql"> 查问语句 </param>
/// <returns>DataSet</returns>
public DataSet Query(string sql)
{using (DmConnection connection = new DmConnection(connectionString))
{using (DmDataAdapter command = new DmDataAdapter(sql, connection))
{DataSet ds = new DataSet();
connection.Open();
command.Fill(ds, "ds");
return ds;
}
}
}
/// <summary>
/// 执行查问语句,返回 DataSet(可自定义超时工夫)/// </summary>
/// <param name="sql"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public DataSet Query(string sql, int timeout)
{using (DmConnection connection = new DmConnection(connectionString))
{using (DmDataAdapter command = new DmDataAdapter(sql, connection))
{DataSet ds = new DataSet();
connection.Open();
command.SelectCommand.CommandTimeout = timeout;
command.Fill(ds, "ds");
return ds;
}
}
}
#endregion 执行简略 SQL 语句
#region 执行带参数的 SQL 语句
/// <summary>
/// 执行 SQL 语句,返回影响的记录数
/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <param name="paras">SQL 参数数组 </param>
/// <returns> 影响的记录数 </returns>
public int ExecuteSql(string sql, params DmParameter[] paras)
{using (DmConnection connection = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand())
{PrepareCommand(cmd, connection, null, sql, paras);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
}
}
/// <summary>
/// 执行 SQL 语句,返回影响的记录数
/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <param name="paras">SQL 参数数组 </param>
/// <returns> 影响的记录数 </returns>
public async Task<int> ExecuteSqlAsync(string sql, params DmParameter[] paras)
{using (DmConnection connection = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand())
{await PrepareCommandAsync(cmd, connection, null, sql, paras);
int rows = await cmd.ExecuteNonQueryAsync();
cmd.Parameters.Clear();
return rows;
}
}
}
/// <summary>
/// 执行增加 SQL 语句,返回记录的 ID(主动产生的自增主键)/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <param name="parms">SQL 参数 </param>
/// <returns> 记录的 ID</returns>
public int ExecuteAdd(string sql, params DmParameter[] parms)
{
sql = sql + ";Select @@IDENTITY";
using (DmConnection connection = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand())
{PrepareCommand(cmd, connection, null, sql, parms);
int recordID = Int32.Parse(cmd.ExecuteScalar().ToString());
cmd.Parameters.Clear();
return recordID;
}
}
}
/// <summary>
/// 执行增加 SQL 语句,返回记录的 ID(主动产生的自增主键)/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <param name="parms">SQL 参数 </param>
/// <returns> 记录的 ID</returns>
public async Task<int> ExecuteAddAsync(string sql, params DmParameter[] parms)
{
sql = sql + ";select @@identity as newautoid";
using (DmConnection connection = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand())
{await PrepareCommandAsync(cmd, connection, null, sql, parms);
int recordID;
try
{recordID = int.Parse((await cmd.ExecuteScalarAsync()).ToString());
}
catch
{recordID = -1;}
cmd.Parameters.Clear();
return recordID;
}
}
}
/// <summary>
/// 执行多条 SQL 语句,实现数据库事务。/// </summary>
/// <param name="sqlList">SQL 语句的哈希表(key 为 sql 语句,value 是该语句的 DmParameter[])</param>
public void ExecuteSqlTrans(Hashtable sqlList)
{using (DmConnection conn = new DmConnection(connectionString))
{conn.Open();
using (DbTransaction trans = conn.BeginTransaction())
{using (DmCommand cmd = new DmCommand())
{
try
{foreach (DictionaryEntry entry in sqlList)
{var sql = entry.Key.ToString();
var paras = (DmParameter[])entry.Value;
PrepareCommand(cmd, conn, trans, sql, paras);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();}
trans.Commit();}
catch (DmException ex)
{trans.Rollback();
throw ex;
}
}
}
}
}
/// <summary>
/// 执行多条 SQL 语句,实现数据库事务。/// </summary>
/// <param name="sqlList">SQL 语句的哈希表(key 为 sql 语句,value 是该语句的 DmParameter[])</param>
public async Task ExecuteSqlTransAsync(Hashtable sqlList)
{using (DmConnection conn = new DmConnection(connectionString))
{await conn.OpenAsync();
using (DbTransaction trans = conn.BeginTransaction())
{using (DmCommand cmd = new DmCommand())
{
try
{foreach (DictionaryEntry entry in sqlList)
{var sql = entry.Key.ToString();
var paras = (DmParameter[])entry.Value;
await PrepareCommandAsync(cmd, conn, trans, sql, paras);
int val = await cmd.ExecuteNonQueryAsync();
cmd.Parameters.Clear();}
trans.Commit();}
catch (DmException ex)
{trans.Rollback();
throw ex;
}
}
}
}
}
/// <summary>
/// 执行一条计算查问后果语句,返回查问后果。/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <param name="parms">SQL 参数 </param>
/// <returns> 查问后果 </returns>
public object GetSingle(string sql, params DmParameter[] parms)
{using (DmConnection conn = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand())
{PrepareCommand(cmd, conn, null, sql, parms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((object.Equals(obj, null)) || (object.Equals(obj, DBNull.Value)))
{return null;}
else
{return obj;}
}
}
}
/// <summary>
/// 执行一条计算查问后果语句,返回查问后果。/// </summary>
/// <param name="sql">SQL 语句 </param>
/// <param name="parms">SQL 参数 </param>
/// <returns> 查问后果 </returns>
public async Task<object> GetSingleAsync(string sql, params DmParameter[] parms)
{using (DmConnection conn = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand())
{await PrepareCommandAsync(cmd, conn, null, sql, parms);
object obj = await cmd.ExecuteScalarAsync();
cmd.Parameters.Clear();
if ((object.Equals(obj, null)) || (object.Equals(obj, DBNull.Value)))
{return null;}
else
{return obj;}
}
}
}
/// <summary>
/// 执行查问语句,返回 DmDataReader (切记要手工敞开 DmDataReader)
/// </summary>
/// <param name="sql"> 查问语句 </param>
/// <param name="parms">SQL 参数 </param>
/// <returns>DmDataReader</returns>
public DbDataReader ExecuteReader(string sql, params DmParameter[] parms)
{DmConnection connection = new DmConnection(connectionString);
DmCommand cmd = new DmCommand();
PrepareCommand(cmd, connection, null, sql, parms);
DbDataReader myReader = cmd.ExecuteReader();
cmd.Parameters.Clear();
return myReader;
}
/// <summary>
/// 执行查问语句,返回 DmDataReader (切记要手工敞开 DmDataReader)
/// </summary>
/// <param name="sql"> 查问语句 </param>
/// <param name="parms">SQL 参数 </param>
/// <returns>DmDataReader</returns>
public async Task<DbDataReader> ExecuteReaderAsync(string sql, params DmParameter[] parms)
{DmConnection connection = new DmConnection(connectionString);
DmCommand cmd = new DmCommand();
await PrepareCommandAsync(cmd, connection, null, sql, parms);
var myReader = await cmd.ExecuteReaderAsync();
cmd.Parameters.Clear();
return myReader;
}
/// <summary>
/// 执行查问语句,返回 DataSet
/// </summary>
/// <param name="sql"> 查问语句 </param>
/// <param name="paras"> 参数数组 </param>
/// <returns>DataSet</returns>
public DataSet Query(string sql, params DmParameter[] paras)
{using (DmConnection connection = new DmConnection(connectionString))
{using (DmCommand cmd = new DmCommand())
{PrepareCommand(cmd, connection, null, sql, paras);
DataSet ds = new DataSet();
using (DmDataAdapter da = new DmDataAdapter(cmd))
{da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds;
}
}
}
}
/// <summary>
/// 筹备 SQL 查问命令
/// </summary>
/// <param name="cmd">SQL 命令对象 </param>
/// <param name="conn">SQL 连贯对象 </param>
/// <param name="trans">SQL 事务对象 </param>
/// <param name="cmdText">SQL 语句 </param>
/// <param name="paras">SQL 参数数组 </param>
private void PrepareCommand(DmCommand cmd, DmConnection conn, DbTransaction trans, string cmdText, DmParameter[] paras)
{if (conn.State != ConnectionState.Open)
{conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
{cmd.Transaction = trans;}
cmd.CommandType = CommandType.Text;
if (paras != null)
{foreach (DmParameter parameter in paras)
{if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{parameter.Value = DBNull.Value;}
cmd.Parameters.Add(parameter);
}
}
}
/// <summary>
/// 筹备 SQL 查问命令
/// </summary>
/// <param name="cmd">SQL 命令对象 </param>
/// <param name="conn">SQL 连贯对象 </param>
/// <param name="trans">SQL 事务对象 </param>
/// <param name="cmdText">SQL 语句 </param>
/// <param name="paras">SQL 参数数组 </param>
private async Task PrepareCommandAsync(DmCommand cmd, DmConnection conn, DbTransaction trans, string cmdText, DmParameter[] paras)
{if (conn.State != ConnectionState.Open)
{await conn.OpenAsync();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
{cmd.Transaction = trans;}
cmd.CommandType = CommandType.Text;
if (paras != null)
{foreach (DmParameter parameter in paras)
{if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{parameter.Value = DBNull.Value;}
cmd.Parameters.Add(parameter);
}
}
}
#endregion 执行带参数的 SQL 语句
}
应用办法也很简略,传入 SQL 语句和参数即可。这里给出几个增删改查的例子:
public class PersonAdoNetDAL : IPersonDAL
{static readonly DmDbClient _client = new DmDbClient("Server=127.0.0.1; UserId=TESTDB; PWD=1234567");
public int Add(PersonModel model)
{string sql = "insert into Person(Name,City) Values(:Name,:City)";
DmParameter[] paras = new DmParameter[] {new DmParameter(":Name",model.Name),
new DmParameter(":City",model.City)
};
return _client.ExecuteAdd(sql, paras);
}
public bool Update(PersonModel model)
{
string sql = "update Person set City=:City where Id=:Id";
DmParameter[] paras = new DmParameter[] {new DmParameter(":Id",model.Id),
new DmParameter(":City",model.City)
};
return _client.ExecuteSql(sql, paras) > 0 ? true : false;
}
public bool Delete(int id)
{
string sql = "delete from Person where Id=:Id";
DmParameter[] paras = new DmParameter[] {new DmParameter(":Id",id),
};
return _client.ExecuteSql(sql, paras) > 0 ? true : false;
}
public PersonModel Get(int id)
{
string sql = "select Id,Name,City from Person where Id=:Id";
DmParameter[] paras = new DmParameter[] {new DmParameter(":Id",id),
};
PersonModel model = null;
using (var reader = (DmDataReader)_client.ExecuteReader(sql, paras))
{while (reader.Read())
{model = new PersonModel();
model.Id = reader.GetInt32(0);
model.Name = reader.GetString(1);
model.City = reader.GetString(2);
}
}
return model;
}
public List<PersonModel> GetList()
{var list = new List<PersonModel>();
using (var reader = (DmDataReader)_client.ExecuteReader("select Id,Name,City from Person"))
{while (reader.Read())
{var model = new PersonModel();
model.Id = reader.GetInt32(0);
model.Name = reader.GetString(1);
model.City = reader.GetString(2);
list.Add(model);
}
}
return list;
}
}
须要留神达梦数据库的参数是用冒号作为前缀的。另外数据表和字段的名字倡议全副应用大写字母,单词之间应用下划线分隔,也就是蛇形命名法。此时 SQL 语句就不必关怀大小写了,怎么写都行。
Dapper 形式
Dapper 是一个轻量级的 ORM 框架,当初应用的也很宽泛,能够简化代码编写。因为 Dapper 扩大的 IDbConnection,这是 ADO.NET 中的货色,咱们应用的 DmProvider 也是实现了 ADO.NET 相干接口,所以 Dapper 能够通过 DmProvider 操作达梦数据库。
首先定义一个获取数据库连贯对象的工厂类:
public class DmConnectionFactory
{
static string sqlConnString = "Server=127.0.0.1; UserId=TESTDB; PWD=123456";
public static IDbConnection GetConn()
{return new DmConnection(sqlConnString);
}
}
而后就能够应用它执行 SQL 语句了:
public class PersonDapperDAL : IPersonDAL
{public PersonDapperDAL()
{ }
public PersonModel Get(int id)
{
string sql = "select Id,Name,City from Person where Id=:Id";
return DmConnectionFactory.GetConn().QueryFirstOrDefault<PersonModel>(sql, new { Id = id});
}
public List<PersonModel> GetList()
{
string sql = "select Id,Name,City from Person";
return DmConnectionFactory.GetConn().Query<PersonModel>(sql).ToList();}
public int Add(PersonModel model)
{string sql = "insert into Person(Name,City) Values(:Name,:City);Select @@IDENTITY";
return DmConnectionFactory.GetConn().QuerySingle<int>(sql, model);
}
public bool Update(PersonModel model)
{
string sql = "update Person set City=:City where Id=:Id";
int result = DmConnectionFactory.GetConn().Execute(sql, model);
return result > 0;
}
public bool Delete(int id)
{
string sql = "delete from Person where Id=:Id";
int result = DmConnectionFactory.GetConn().Execute(sql, new { Id = id});
return result > 0;
}
}
Query、Execute 这些办法都是 Dapper 定义的,能够看到可能少写很多代码。这里也不必关上连贯、敞开连贯,也不必写 using,因为 Dapper 的这些办法中曾经做了相干解决。
好了,以上就是本文的次要内容。如有错漏欢送斧正。
播种更多架构常识,请关注微信公众号 萤火架构。原创内容,转载请注明出处。