123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MySql.Data.MySqlClient;
- using System.Configuration;
- namespace parkMonitor.DataBase
- {
- public class DBConnection
- {
- /// <summary>
- /// 连接字符串
- /// </summary>
- public static string localStr = "SqlConnectionLocation";
- public static string remoteStr = "SqlConnectionStr";
- public static string localConf, remoteConf;
- public static string localIP, remoteIP;
- public static void Init()
- {
- try
- {
- localConf = ConfigurationManager.AppSettings[localStr];
- remoteConf = ConfigurationManager.AppSettings[remoteStr];
- localIP = ConfigurationManager.AppSettings["localDBIP"];
- remoteIP = ConfigurationManager.AppSettings["remoteDBIP"];
- }
- catch
- {
- Console.WriteLine("配置文件有误");
- }
- }
- /// <summary>
- /// 连接句柄
- /// </summary>
- /// <param name="connectionStr"></param>
- /// <returns></returns>
- //public MySqlConnection getConn(string connectionStr)
- //{
- // MySqlConnection con = null;
- // try
- // {
- // if (connectionStr == localStr)
- // {
- // con = new MySqlConnection(localConf);
- // }
- // else if (connectionStr == remoteStr)
- // {
- // con = new MySqlConnection(remoteConf);
- // }
- // return con;
- // }
- // catch { }
- // return null;
- //}
- /// <summary>
- /// 预处理句柄
- /// </summary>
- /// <param name="sql"></param>
- /// <param name="con"></param>
- /// <returns></returns>
- public MySqlCommand getComm(string sql, MySqlConnection con)
- {
- MySqlCommand cmd = null;
- try
- {
- cmd = new MySqlCommand(sql, con);
- }
- catch (MySqlException)
- {
- Console.WriteLine("数据库连接异常");
- }
- catch
- {
- Console.WriteLine("sql语句异常");
- }
- return cmd;
- }
- }
- }
|