我无法使用OpenQuery通过C#代码查询Linked Oracle服务器但是我可以使用4部分名称或OpenQuery从Sql Server本身查询服务器。使用代码时出现以下错误:
System.Data.SqlClient.SqlException:链接服务器“LinkedServer”的OLE DB提供程序“OraOLEDB.Oracle”报告错误。提供商未提供有关错误的任何信息。无法为链接服务器“LinkedServer”初始化OLE DB提供程序“OraOLEDB.Oracle”的数据源对象。位于System.Data.SqlClient.TdsParter.Run的System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)中的System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream) ,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)System.Data.SqlClient.SqlDataReader.ConsumeMetaData()at System.Data.SqlClient.SqlDataReader.get_MetaData()at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString) )System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async)在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result)在System.Data.SqlClient.SqlCommand.RunExecute在TerminateEmployeeForm.FormCode.FormEvents_Loading的System.Data.SqlClient.SqlCommand.ExecuteReader()上的System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String方法)中的Reader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法)(对象发送者,LoadingEventArgs e)
我的代码如下所示:
testCommand = "SELECT * FROM OPENQUERY(LinkedServer,'SELECT * FROM HR.REGION')";
SqlCommand cmdTest = new SqlCommand();
cmdTest.Connection = conn1;
cmdTest.CommandText = testCommand;
cmdTest.CommandType = System.Data.CommandType.Text;
conn1.Open();
SqlDataReader testdr = cmdTest.ExecuteReader();
string values = null;
if (testdr.HasRows == true)
{
while (testdr.Read())
{
values = values + testdr["REGION_FIELD"].ToString();
}
}