/// <summary> /// 测试数据库连接是否正常 /// </summary> /// <param name="conPath">XML路径文件名</param> /// <param name="strMessage">弹出错误信息,指明是哪个数据连接出错</param> /// <returns></returns> public bool IsConnect(string conPath, string strMessage) { string testsql = string.Empty; DataBaseinfo info = XMLHelper.ReadDataBase(conPath); if (info.databasetype == "0") { testsql = "select 1 from dual"; } else { testsql = "select 1"; } bool istrue = false; //测试链接 bool connetctionStatus = false; bool timeOut = false; TimeoutChecker timeout = new TimeoutChecker( delegate { ISqlMapper mapper = null; try { //获取ISqlMapper尝试连接 mapper = mapperobj.NewMapper(info); DataSet ds = new DataSet(); mapper.OpenConnection(); IDbCommand command = mapper.LocalSession.CreateCommand(CommandType.Text); command.CommandText = testsql; //"SELECT 1 FROM DA_CONFIG"; mapper.LocalSession.CreateDataAdapter(command).Fill(ds); connetctionStatus = true; //ShowMessageHelper.ShowBoxMsg("数据库连接成功!"); istrue = true; } catch { //如果没有超时 if (!timeOut) { connetctionStatus = true; ShowMessageHelper.ShowBoxMsg(string.Format("{0}连接失败,数据库不存在或用户名、密码错误,请检查数据库配置!", strMessage)); } } }, delegate { //如果没有链接上 if (!connetctionStatus) { timeOut = true; ShowMessageHelper.ShowBoxMsg(string.Format("{0}连接超时,请检查数据库配置!", strMessage)); } }); timeout.Wait(5000); return(istrue); }
public bool IsConnect(DataBaseinfo info, string testsql) { bool istrue = false; //测试链接 bool connetctionStatus = false; bool timeOut = false; TimeoutChecker timeout = new TimeoutChecker( delegate { ISqlMapper mapper = null; try { //获取ISqlMapper尝试连接 mapper = mapperobj.NewMapper(info); DataSet ds = new DataSet(); mapper.OpenConnection(); IDbCommand command = mapper.LocalSession.CreateCommand(CommandType.Text); command.CommandText = testsql; mapper.LocalSession.CreateDataAdapter(command).Fill(ds); connetctionStatus = true; ShowMessageHelper.ShowBoxMsg("数据库连接成功!"); istrue = true; } catch (Exception ex) { WriteLog(ex.ToString()); //如果没有超时 if (!timeOut) { connetctionStatus = true; ShowMessageHelper.ShowBoxMsg(ex.Message + "数据库不存在或用户名、密码错误,请重新输入!"); } } }, delegate { //如果没有链接上 if (!connetctionStatus) { timeOut = true; ShowMessageHelper.ShowBoxMsg("连接超时"); } }); timeout.Wait(5000); return(istrue); }