源码分享:链接:链接:https://pan.baidu.com/s/1ggurNSv 密码:n46m
第一步:new Connect(“mySqlConnnect”)对象
//调用connect.config文件的mySqlConnnect下的配置,方便调用多个数据源和自定义数据源。
但是:四种数据源配置必须包含固定的字段:
oracle数据库 节点必须含有:oracleConnnect
sqlserver数据库 节点必须含有:sqsConnnect
access数据库 节点必须含有:accessConnnect
mySql数据库 节点必须含有:mySqlConnnect
Connect con = new Connect(“mySqlConnnect”);
//DbConnection =con.GetDbConnect(),支持oracle OR sqs or access
//DbConnection mycon = con.GetDbConnect();
//MySqlConnection =con.getMySqlConnect(),支持mysql
MySqlConnection mycon = con.getMySqlConnect();
第二部配置:
connect.config文件的mySqlConnnect节点或自定义下的配置的数据库连接
< mySqlConnnect>
< add key=”Provide” value=”MySql.Data.MySqlClient”>< /add>
< add key=”ServerIP” value=”localhost”>< /add>
< add key=”port” value=”3306”>< /add>
< add key=”DataBase” value=”test”>< /add>
< add key=”user” value=”root”>< /add>
< add key=”password” value=”111111”>< /add>
< /mySqlConnnect>
第三部:正常作为connect使用
if (mycon.State == ConnectionState.Closed)
{
mycon.Open();//”数据库连接成功”;
}
//使用结束后关闭数据源连接
if (mycon.State == ConnectionState.Open)
{
mycon.Close();
}
1、读取自定义config下的配置文件(SaveConfig未调用,起方法能改变节点内容,只是用了ReadConfig)
class Tools
{
public static string ReadConfig(string configName,string targNode,string key)
{
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
DirectoryInfo rootDir = Directory.GetParent(Environment.CurrentDirectory);
string strFileName = rootDir.Parent.FullName +"\\"+configName;
//加载Xml
doc.Load(strFileName);
//找出名称为“appSettings”的所有元素=targNode
XmlNodeList nodes = doc.GetElementsByTagName(targNode);
for (int i = 0; i < nodes[0].ChildNodes.Count; i++)
{
var node = nodes[0].ChildNodes[i];
if (node.Attributes["key"].Value.ToString().Equals(key))
{
return node.Attributes["value"].Value;
}
}
return string.Empty;
}
}
2.自动生成connect的配置文件:
class Connect
{
//可自定义文件名称
static string configName= “Connect.config”;
// targNode为读取的节点名 :mySqlConnnect
static string targNode;
public Connect(string TargNode) {
targNode = TargNode;
}
public DbConnection GetDbConnect() {
string provide=null;
// provide为connect的驱动
provide = Tools.ReadConfig(configName, targNode, "Provide");
if (provide != null && provide.Length == 0) {
if (targNode.Contains("oracleConnnect")) {
provide = "System.Data.OracleClient";
}
else if (targNode.Contains("sqsConnnect"))
{
provide = "System.Data.SqlClient";
}
else if (targNode.Contains("accessConnnect"))
{
provide = "System.Data.OleDb";
}
else if (targNode.Contains("mySqlConnnect"))
{
throw new ApplicationException("GetDbConnect 不支持mysql数据库,请改用getMySqlConnect");
}
else
{
throw new ApplicationException("未初始化Provide,且为自定义的数据库连接");
}
}
DbProviderFactory provider = DbProviderFactories.GetFactory(provide);
DbConnection conn = provider.CreateConnection();
string ServerIP = Tools.ReadConfig(configName, targNode, "ServerIP");
string port = Tools.ReadConfig(configName, targNode, "port");
string DataBase = Tools.ReadConfig(configName, targNode, "DataBase");
string user = Tools.ReadConfig(configName, targNode, "user");
string password = Tools.ReadConfig(configName, targNode, "password");
var sb = new StringBuilder();
if (targNode.Contains("oracleConnnect"))
{
sb.AppendFormat("password={0};User ID={1};Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={2})(PORT={3})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={4})))",
password, user, ServerIP, port, DataBase);
}
else if(targNode.Contains("sqsConnnect"))
{
sb.AppendFormat("Data Source={0},{1};Initial Catalog={2};User Id={3};Password={4};", ServerIP, port, DataBase, user, password);
}
else if (targNode.Contains("accessConnnect"))
{
sb.AppendFormat("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}",DataBase);
}
//sever = 服务器名; database = 数据库名; User ID = 用户; Password = 密码
conn.ConnectionString = sb.ToString();
return conn;
}
public MySqlConnection getMySqlConnect() {
MySqlConnection mysqlConnection = new MySqlConnection();
var sb = new StringBuilder();
string ServerIP = Tools.ReadConfig(configName, targNode, "ServerIP");
string port = Tools.ReadConfig(configName, targNode, "port");
string DataBase = Tools.ReadConfig(configName, targNode, "DataBase");
string user = Tools.ReadConfig(configName, targNode, "user");
string password = Tools.ReadConfig(configName, targNode, "password");
sb.AppendFormat("server ={0};Port={1};database={2};user id={3};password={4};pooling=true;CharSet=utf8;Allow Zero Datetime=true", ServerIP, port, DataBase, user, password);
mysqlConnection.ConnectionString = sb.ToString();
return mysqlConnection;
}
}