// 获得 SQL Server 信息 // return: // -1 出错 // 0 放弃 // 1 成功 int GetSqlServerInfo( string strSqlServerName, out OracleSqlServerInfo info, out string strError) { strError = ""; int nRet = 0; info = new OracleSqlServerInfo(); SystemLoginDialog dlg = new SystemLoginDialog(); GuiUtil.AutoSetDefaultFont(dlg); dlg.SqlServerName = strSqlServerName; dlg.StartPosition = FormStartPosition.CenterScreen; REDO_INPUT: dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) { return(0); } info.ServerName = strSqlServerName; info.SqlUserName = dlg.SqlUserName; info.SqlUserPassword = dlg.SqlPassword; string strConnection = @"Persist Security Info=False;" + "User ID=" + info.SqlUserName + ";" //帐户和密码 + "Password="******";" + "Data Source=" + strSqlServerName + ";" + "Connect Timeout=30"; OracleConnection connection = null; try { connection = new OracleConnection(strConnection); } catch (Exception ex) { strError = "建立连接时出错:" + ex.Message + " 类型:" + ex.GetType().ToString(); return(-1); } try { connection.Open(); } catch (OracleException sqlEx) { // ex.Number == 12154 // ORA-12154: TNS: 无法解析指定的连接标识符 strError = "连接SQL服务器出错:" + sqlEx.Message + "。"; int nError = sqlEx.ErrorCode; MessageBox.Show(this, strError); dlg.Comment = "登录错误: " + strError + "\r\n请重新登录"; goto REDO_INPUT; return(-1); } catch (Exception ex) { strError = "连接SQL服务器出错:" + ex.Message + " 类型:" + ex.GetType().ToString(); return(-1); } try { string strCommand = ""; OracleCommand command = null; strCommand = "select username,default_tablespace from dba_users"; command = new OracleCommand(strCommand, connection); try { OracleDataReader reader = command.ExecuteReader(); while (reader.Read() == true) { DbaUser user = new DbaUser(); user.Name = reader.GetString(0); user.TableSpace = reader.GetString(1); info.DbaUsers.Add(user); } } catch (Exception ex) { strError = "执行命令 " + strCommand + " 出错:" + ex.Message + " 类型:" + ex.GetType().ToString(); return(-1); } } finally { connection.Close(); } return(1); }
// 获得 SQL Server 信息 // return: // -1 出错 // 0 放弃 // 1 成功 int GetSqlServerInfo( string strSqlServerName, out OracleSqlServerInfo info, out string strError) { strError = ""; int nRet = 0; info = new OracleSqlServerInfo(); SystemLoginDialog dlg = new SystemLoginDialog(); GuiUtil.AutoSetDefaultFont(dlg); dlg.SqlServerName = strSqlServerName; dlg.StartPosition = FormStartPosition.CenterScreen; REDO_INPUT: dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return 0; info.ServerName = strSqlServerName; info.SqlUserName = dlg.SqlUserName; info.SqlUserPassword = dlg.SqlPassword; string strConnection = @"Persist Security Info=False;" + "User ID=" + info.SqlUserName + ";" //帐户和密码 + "Password="******";" + "Data Source=" + strSqlServerName + ";" + "Connect Timeout=30"; OracleConnection connection = null; try { connection = new OracleConnection(strConnection); } catch (Exception ex) { strError = "建立连接时出错:" + ex.Message + " 类型:" + ex.GetType().ToString(); return -1; } try { connection.Open(); } catch (OracleException sqlEx) { // ex.Number == 12154 // ORA-12154: TNS: 无法解析指定的连接标识符 strError = "连接SQL服务器出错:" + sqlEx.Message + "。"; int nError = sqlEx.ErrorCode; MessageBox.Show(this, strError); dlg.Comment = "登录错误: " + strError + "\r\n请重新登录"; goto REDO_INPUT; return -1; } catch (Exception ex) { strError = "连接SQL服务器出错:" + ex.Message + " 类型:" + ex.GetType().ToString(); return -1; } try { string strCommand = ""; OracleCommand command = null; strCommand = "select username,default_tablespace from dba_users"; command = new OracleCommand(strCommand, connection); try { OracleDataReader reader = command.ExecuteReader(); while (reader.Read() == true) { DbaUser user = new DbaUser(); user.Name = reader.GetString(0); user.TableSpace = reader.GetString(1); info.DbaUsers.Add(user); } } catch (Exception ex) { strError = "执行命令 " + strCommand + " 出错:" + ex.Message + " 类型:" + ex.GetType().ToString(); return -1; } } finally { connection.Close(); } return 1; }