asp.net|oracle| Tips
/*
Get Oracle Service Name
1. Querying the registry for Oracle installation root directory
such as: Hkey_local_machine\software\oracle\oracle_home REG_SZ E:\ORACLE\ORA92
Learned Oracle Service name file location: root directory \network\admin\tnsnames.ora
2. Resolves the file, which is structured like
# ------------------------------------------------
PORTAL =
(DESCRIPTION =
(Address_list =
(address = (PROTOCOL = TCP) (HOST = 134.104.52.6) (PORT = 1521))
)
(Connect_data =
(service_name = portal)
)
)
3. Analytical Essentials:
Row by row get again parse
Skip the comment line with #
After filtering out the spaces, the line between the first character and the a~z contains the Oracle service name
Intercepts the string to the left of the first "=" number on the line, which is the Oracle service name after the trim process
*/
public static string[] Getoracletnsnames ()
{
Try
{
Querying the registry for Oracle service file paths
RegistryKey key = Registry.LocalMachine.OpenSubKey ("SOFTWARE"). OpenSubKey ("ORACLE");
String home = (string) key. GetValue ("Oracle_home");
string file = home + @ "\network\admin\tnsnames.ora";
Parsing files
String line;
ArrayList arr = new ArrayList ();
StreamReader sr = new StreamReader (file);
while (line = Sr. ReadLine ())!= null)
{
line = line. Trim ();
if (line!= "")
{
char C = line[0];
if (c>= ' A ' && c<= ' z ')
Arr. ADD (line. Substring (0, line. IndexOf (")");
}
}
Sr. Close ();
Returns an array of strings
Return (string[]) arr. ToArray (typeof (String));
}
catch (Exception ex)
{
return null;
}
}