C#数据库连接封装类
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Configuration;
namespace ponents.DBUtility
{
public class ConnetionLine
{
public static SqlHelper SystemManageCn
{
get
{
try
{
string connectionString =
ConfigurationManager.ConnectionStrings["SystemManageConnectionString"].ConnectionString;
SqlHelper sqlHelper = GetConnetion(connectionString);
return sqlHelper;
}
catch (Exception ex)
{
throw ex;
}
}
}
public static SqlHelper JIEJINERPReportCn
{
get
{
try
{
string connectionString =
ConfigurationManager.ConnectionStrings["JIEJINERPReportConnectionString"].ConnectionString ;
SqlHelper sqlHelper = GetConnetion(connectionString);
return sqlHelper;
}
catch (Exception ex)
{
throw ex;
}
}
}
private static SqlHelper GetConnetion(String connectionString)
{
try
{
int stratNo = connectionString.IndexOf("Server") + 7;
int endNo = connectionString.IndexOf(";");
string serverIP = connectionString.Substring(stratNo, endNo - stratNo);
//string pingrst = CmdPing(serverIP);
string pingrst = "连接";
if (pingrst != "连接")
{
throw new Exception(pingrst + "\n" + "無法連線到服務器,請檢查您的網絡連接狀態!!");
}
SqlHelper cn = new SqlHelper(connectionString);
return cn;
}
catch (Exception ex)
{
throw ex;
}
}
private static string CmdPing(string strIp)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
eShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string pingrst;
p.Start();
p.StandardInput.WriteLine("ping -n 1 " + strIp);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
if (strRst.IndexOf("(0% loss)") != -1 || strRst.IndexOf("(0% 遺失)") != -1 || strRst.IndexOf("(0% 遗失)") != -1)
pingrst = "连接";
else if (strRst.IndexOf("Destination host unreachable.") != -1)
pingrst = "无法到达目的主机";
else if (strRst.IndexOf("Request timed out.") != -1)
pingrst = "超时";
else if (strRst.IndexOf("Unknown host") != -1)
pingrst = "无法解析主机";
else
pingrst = strRst;
p.Close();
return pingrst;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;