36、在C#项目中使用Dapper
来源:互联网 发布:电竞椅 知乎 编辑:程序博客网 时间:2024/06/05 02:24
第一部分:项目使用dapper流程;
1)、首先需要在网上下载dapper.dll放到lib中(笔者全路径:svn\Src\Wind.BlackList\dev\lib),作为项目引用的dll,然后在需要用到的地方引用命名空间:using Dapper;
2)、以连接oracle数据库为例:oracle数据库的连接需要用到:Oracle.ManagedDataAccess.dll,并在要用到的地方引用命名空间:using Oracle.ManagedDataAccess.Client;(注意这里是引用.client),数据库的连接使用IDbConnection conn = new OracleConnection(connectionString),事务方式使用IDbTransaction tran = conn.BeginTransaction()
说明:Oracle.ManagedDataAccess.dll的位置:在正确安装了oracle客户端之后,在客户端目录下可以找到,笔者的全路径为:D:\app\szxu\product\12.1.0\client_2\odp.net\managed\common\Oracle.ManagedDataAccess.dll,可以直接使用,无需再次复制dll到lib中
3)、直接使用
第二部分:dapper和数据库的交互
1)、Dapper和Oracle数据库交互(注意sql中参数的前导符是分号)
using System.Data;using System.Data.SqlClient;using Dapper;string connectionString = "DATA SOURCE=ORCL;USER ID=crm;PASSWORD=crm"; using (IDbConnection connection = new OracleConnection(connectionString)) { string sql = @"select * from crm.Tb_Cus_Customer where customerid = :CustomerID"; //OracleConnection:赋值参数只能使用“:CustomerID” connection.Open(); var result = connection.Query<CustomerEntity>(sql, new { CustomerID = 38839 }); connection.Close(); }2)、Dapper和SQL server数据库交互(注意sql中参数的前导符是@号)
using System.Data;using System.Data.SqlClient;using Dapper;string connectionString = "DATA SOURCE=eipdbtest;database=WindEmployee;USER ID=windtechw1;pwd=windtechw1;Connection Timeout=360"; using (IDbConnection connection = new SqlConnection(connectionString)) { string sql = @"select * from crm.Tb_Cus_Customer where customerid = @CustomerID"; //SqlConnection:赋值参数只能使用“@CustomerID” connection.Open(); var result = connection.Query<CustomerEntity>(sql, new { CustomerID = 38839 }); connection.Close(); }
第三部分:示例
DACustomer.cs文件:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Wind.BlackList.Model;using Dapper;using System.Data;using Oracle.ManagedDataAccess.Client;namespace Wind.BlackList.DataAccess{ public partial class DACustomer { private static DACustomer _instance; public static DACustomer Instance { get { if (_instance == null) { _instance = new DACustomer(); } return _instance; } } /// <summary> /// 通过代码或名称获取客户信息 /// </summary> /// <param name="customerKey"></param> /// <returns></returns> public void GetCustomerByCodeOrName(string customerKey) { string connectionString = "DATA SOURCE=ORCL;USER ID=xxx;PASSWORD=xxx"; string sql = @"select * from crm.Tb_Cus_Customer where customercode = :customercode"; using (IDbConnection conn = new OracleConnection(connectionString)) { conn.Open(); var result = conn.Query<Customer>(sql, new { customercode = customerKey }); conn.Close(); } } /// <summary> /// 更新客户地址信息 /// </summary> public bool UpdateCustomerAddress(int customerID, string address) { string connectionString = "DATA SOURCE=ORCL;USER ID=xxx;PASSWORD=xxx"; //事务方式(注意使用事务方式时,必须使用open将数据库连接打开;如果不使用事务方式,则open不是必须的) using (IDbConnection conn = new OracleConnection(connectionString)) { conn.Open(); using (IDbTransaction tran = conn.BeginTransaction()) { try { string sql = @"update xxx.Tb_Cus_Customer set address = :address where customerid = :customerid"; conn.Execute(sql, new { address = address, customerid = customerID }, tran); tran.Commit(); } catch(Exception ex) { tran.Rollback(); throw new Exception(ex.Message); } } conn.Close(); } return true; } }}主文件:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Wind.BlackList.DataAccess;namespace Wind.BlackList{ class Program { static void Main(string[] args) { //Dapper使用示例 DACustomer customer = DACustomer.Instance; string customerKey = "0001"; string address = "xxx市xxx区xxx路2000号"; int customerID = 38839; //通过代码或名称获取客户信息 customer.GetCustomerByCodeOrName(customerKey); //更新客户地址信息 customer.UpdateCustomerAddress(customerID, address); } }}
阅读全文
0 0
- 36、在C#项目中使用Dapper
- C# Dapper
- c# log4net在项目中使用实例
- c# log4net在项目中使用实例
- C# 枚举在项目中使用心得
- C# 枚举在项目中使用心得
- Dapper使用
- C#中OutlookBar的使用以及在项目中的引用
- sqllite (3) - c# .net 使用 dapper 操作 sqllite 数据库
- C# Dapper 基本使用 增删改查事务等
- c# dapper 动态参数
- 在Dapper连接数据库中出现的一些问题
- Dapper的基本使用
- Dapper的基本使用
- Dapper 基本使用
- Dapper 基本使用
- Dapper.NET使用
- Dapper的基本使用
- HDU
- [HNOI2008]水平可见直线
- UVa11542
- KMP算法
- SSD算法的MXNet实现
- 36、在C#项目中使用Dapper
- C语言查看BMP格式图片头(bmpHeader.c)
- 为什么hive中show tables没有表和在mysql中use hive后再show tables却能看见表?
- 【c基础知识】#ifdef 条件编译的巧妙用法
- 并发登录人数控制——shiro
- UnicodeEncodeError: 'latin-1' codec can't encode characters in position 44-46: ordinal not in range(
- 10.10 笔记-JavaScript 02 绑定事件,对象类型的转换
- 破解百度云限速下载教程
- DOM中常用的方法