我尝试连接到处于已挂载状态的数据库并使用此查询“从v $ instance选择状态”查询数据库状态,这是连接为sysdba所需的,连接字符串看起来像这个:
<add name="ORCL" connectionString="DBA Privilege=SYSDBA;user id=sys;password=oracle;data source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=SERVER1)(PORT=1521))(CONNECT_DATA=(UR = A)(SERVICE_NAME=ORCL)));"/>
如果您使用Oracle.DataAccess.Client,您可以执行此操作,但使用具有相同连接字符串的Oracle.ManagedDataAccess.Client会导致“ORA-01219:数据库未打开:仅在固定表/视图上允许查询”连接字符串相同的错误。
这是代码:
public class Repository
{
public static string GetServerVersion(DbConnection dbConn)
{
try
{
string result;
using (dbConn)
{
if (dbConn.State == ConnectionState.Closed)
dbConn.Open();
var dbStatus = GetStatus(dbConn);
result = string.Format("Connected to Oracle Database {0}{1}Database Status {2}", dbConn.ServerVersion, Environment.NewLine, dbStatus);
}
return result;
}
catch (Exception ex)
{
return string.Format("Error : {0}", ex.Message);
}
}
public static string GetStatus(DbConnection con)
{
var command = con.CreateCommand();
command.CommandText = "Select status from v$instance";
return command.ExecuteScalar().ToString();
}
}
ManagedDataAccess使用Oracle.ManagedDataAccess.Client连接调用存储库
using Oracle.ManagedDataAccess.Client;
namespace Oracle.DataAccessTest
{
public class ManagedDataAccess
{
public static string GetServerVersion(string constr)
{
return Repository.GetServerVersion(new OracleConnection(constr));
}
}
}
UnManagedDataAccess使用Oracle.DataAccess.Client连接调用存储库
using Oracle.DataAccess.Client;
namespace Oracle.DataAccessTest
{
public class UnManagedDataAccess
{
public static string GetServerVersion(string constr)
{
return Repository.GetServerVersion(new OracleConnection(constr));
}
}
}
最后:
class Program
{
static void Main(string[] args)
{
var cnn = ConfigurationManager.ConnectionStrings["ORCL"].ConnectionString;
Console.WriteLine("Oracle.DataAccess.Client");
Console.WriteLine(UnManagedDataAccess.GetServerVersion(cnn));
Console.WriteLine();
Console.WriteLine("Oracle.ManagedDataAccess.Client");
Console.WriteLine(ManagedDataAccess.GetServerVersion(cnn));
Console.WriteLine();
Console.ReadKey();
}
}
这就是输出:
Oracle.DataAccess.Client
Connected to Oracle Database 11.2.0.2.0
Database Status MOUNTED
Oracle.ManagedDataAccess.Client
Error : ORA-06550: línea 1, columna 107:
PL/SQL: ORA-01219: base de datos no abierta: sólo se permiten consultas en tablas/vistas fijas
有关解决此问题的任何想法吗?