C#數據庫擴展使用
這前共享了我自己的類庫給同事SnowFun使用,後為其寫數據庫Helper,現在代碼記錄如下:
using System;
using EasyDotNet.Data;
public class Database
{
private IDatabaseConnection _conn;
private string _connectionString = String.Empty;
private string _name;
public IDatabaseConnection Conn
{
get
{
if (_conn == null) { _conn = new ConnectionMSSQL(_connectionString); }
return _conn;
}
}
public string Name { get { return _name; } set { _name = value; } }
public string ConnectionSting { get { return _connectionString; } set { _connectionString = value; } }
public Database() { }
public Database(string name, string connectionString)
{
_name = name;
_connectionString = connectionString;
}
}
using System.Collections.Generic;
using System.Configuration;
using EasyDotNet.Data;
public class DataHandler
{
private static List<Database> _list;
public static IDatabaseConnection GetConn(string name)
{
if (_list == null) { _list = new List<Database>(); }
Database result = null;
foreach (Database database in _list)
{
if (database.Name == name) { result = database; break; }
}
if (result == null)
{
switch (name)
{
case "abc":
result = new Database(name, ConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString);
break;
case "Maximo":
result = new Database(name, ConfigurationManager.ConnectionStrings["MaximoCon"].ConnectionString);
break;
}
_list.Add(result);
}
return result.Conn;
}
}
using EasyDotNet.Data;
public class DataHelper
{
public static IDatabaseConnection Maximo = DataHandler.GetConn("Maximo");
}