QQ技术交流群号:601122412
废话不多说,直接粘代码:
Oracle数据库
引用:DataAccess.dll(v4.0.30319)
这是一个帮助类。服务端Glob类里 ,程序启动加载的类。(数据库链接)
类名:DataBaseConfig.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Zhuantou.Class.Glob
{
public class DataBaseConfig
{
//数据库:连接地址
private static readonly string serverIP = ConfigurationManager.AppSettings["DBServerIP"];
//数据库:服务名
private static readonly string serverName = ConfigurationManager.AppSettings["DBServerName"];
//数据库:登录名
private static readonly string dbUserName = ConfigurationManager.AppSettings["DBUid"];
//数据库:密码
private static readonly string dbPwd = ConfigurationManager.AppSettings["DBPwd"];
//数据库:端口
private static readonly string dbPort = ConfigurationManager.AppSettings["DBPort"];
// 类型
private static readonly string dbType = ConfigurationManager.AppSettings["DBType"];
///
/// 数据库:连接地址
///
public static string ServerIP
{
get { return serverIP; }
}
///
/// 数据库:数据库名
///
public static string ServerName
{
get { return serverName; }
}
///
/// 数据库:登录名
///
public static string DBUserName
{
get { return dbUserName; }
}
///
/// 数据库:密码
///
public static string DBPwd
{
get { return dbPwd; }
}
///
/// 数据库:端口
///
public static string DBPort
{
get { return dbPort; }
}
///
/// 数据库类型
///
public static string DBType
{
get { return dbType==null?"oracle": dbType; }
}
///
/// 获得连接字符串
///
///
public static string GetConnectionString()
{
try
{
if (DBType.ToUpper() == "ORACLE")
{
var db = new StringBuilder();
db.Append(@"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=");
db.Append(ServerIP);
db.Append(@")(PORT=" + DBPort + "))(CONNECT_DATA =(SERVICE_NAME = " + ServerName + ")))");
db.Append(@";User Id=");
db.Append(DBUserName);
db.Append(@";Password=");
db.Append(DBPwd);
return db.ToString();
}
else
{
var db = string.Format(@"server ={0}; Database ={1}; Integrated Security = False; Pooling = True; user ={2}; pwd ={3}", ServerIP, ServerName, DBUserName, DBPwd);
return db;
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
//这两个不建议写一个类里面。建议分开
//这个可以写在数据库访问的类里面(Dapper.cs)
//要在程序加载(比如在Program.cs里面等其他)的时候调用Init()。
public static void Init()
{
DBConfigCache.Instance.ConStr = DataBaseConfig.GetConnectionString();//调取上面链接穿的方法。把数据库链接第一次加载写入缓存。
DBConfigCache.Instance.DBType = DataBaseType.Oracle;
DataBaseFactory.IsEnableLogEvent = false; // 是否打印日志
}
程序中的用法
using (DataBase db = DataBaseFactory.NewConnection)//new一个新的链接穿直接
{}