//初始化连接有用户表的数据库 public static void ConnUserInit() { if (!isSqlOk) //access { string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=../../database/user.mdb;Persist Security Info=False"; try { ConnUser = new OleDbConnection(strCon); ConnUser.Open(); } catch { ConnUser = null; MessageBox.Show("连接数据库失败", "error"); return; } } else //sql { //要改数据库!!! strConn = @"server=" + mySystem.Parameter.IP_port + ";database=user;MultipleActiveResultSets=true;Uid=" + Parameter.sql_user + ";Pwd=" + Parameter.sql_pwd; isOk = false; ConnUser = connToServer(strConn); while (!isOk) { MessageBox.Show("连接数据库失败", "error"); Connect2SqlForm con2sql = new Connect2SqlForm(); con2sql.IPChange += new Connect2SqlForm.DelegateIPChange(IPChanged); con2sql.ShowDialog(); ConnUser = connToServer(strConn); } } }
//初始化连接有用户表的数据库 public static void InitConnUser() { if (!isSqlOk) { string strsql = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=../../database/user.mdb;Persist Security Info=False"; isOk = false; connOleUser = connToServerOle(strsql); while (!isOk) { MessageBox.Show("连接数据库失败", "error"); return; } } else { //connUser = Init(connUser); strConn = "server=" + IP_port + ";database=user;MultipleActiveResultSets=true;Uid=" + Parameter.sql_user + ";Pwd=" + Parameter.sql_pwd; //MessageBox.Show(IP_port); isOk = false; // 显示提示框,说明正在连接服务器IP connUser = connToServer(strConn); if (!isOk) { //taskbarNotifier1.Show("提示", "连接失败,正在自动检测服务器地址,请稍候", 500, 2000, 500); // ping name of server and get correct of ip AppSettingsSection appSettings = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath).AppSettings; String ServerPCName = null; if (appSettings.Settings["ServerPCName"] != null) { ServerPCName = appSettings.Settings["ServerPCName"].Value; } else { MessageBox.Show("获取服务器名称失败."); throw new Exception("获取服务器名称失败"); } System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(ServerPCName); String ip = ""; foreach (System.Net.IPAddress ipp in host.AddressList) { if (ipp.ToString().Contains(":")) { continue; } else { //taskbarNotifier1.Show("提示", String.Format("检测服务器地址为:{0},再次尝试连接", ip), 500, 300, 500); ip = ipp.ToString(); appSettings.Settings["ip"].Value = ip; appSettings.CurrentConfiguration.Save(); ip = appSettings.Settings["ip"].Value; Parameter.IP_port = ip + "," + appSettings.Settings["port"].Value; break; } } if (ip.Equals("")) { MessageBox.Show("连接服务器失败."); } // 第二次尝试 strConn = "server=" + IP_port + ";database=user;MultipleActiveResultSets=true;Uid=" + Parameter.sql_user + ";Pwd=" + Parameter.sql_pwd; isOk = false; connUser = connToServer(strConn); if (!isOk) { Connect2SqlForm con2sql = new Connect2SqlForm(); con2sql.IPChange += new Connect2SqlForm.DelegateIPChange(IPChanged); con2sql.ShowDialog(); //strConn = "server=" + IP_port + ";database=user;MultipleActiveResultSets=true;Uid=" + Parameter.sql_user + ";Pwd=" + Parameter.sql_pwd; //connUser = connToServer(strConn); Application.Exit(); } } } }