使用Powershell脚本连接到Oracle数据库。连接时出错

时间:2020-05-27 07:38:31

标签: sql powershell

我正在尝试通过Powershell脚本使用主机名和SID值连接到数据库。

我的代码:

$DataSource ='HOSTNAME:PORT/INSTANCE'
    $Username='Username'
    $Password='Password'

    #List
    $UserDataDT = New-Object System.Data.DataTable

    $SQLConnection = New-Object System.Data.SqlClient.SqlConnection
    $SQLConnection.ConnectionString = "Data Source=$DataSource;User Id=$Username;Password=$Password;"   
    $SQLCmd= New-Object System.Data.SqlClient.SqlCommand
    $SQLConnection.open();
    $SQLCmd.Connection = $SQLConnection
    $SqlCmd.CommandTimeout = 600

我遇到以下错误:

catch Exception...
System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establish
ing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is corre
ct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could n
ot open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): The filename, directory na
me, or volume label syntax is incorrect
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString conn
ectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Bool
ean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionP
ool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderMan
ager)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poo
lKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOpti
ons)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningOb
ject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOption
s, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userO
ptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObje
ctsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal
& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 ret
ry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSourc
e`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnecti
onFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at CallSite.Target(Closure , CallSite , Object )
ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:123,State:0,Class:20

我还尝试将$DataSource的值保持为:

$DataSource ='(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(Host=hostname)(Port=0000)))(CONNECT_DATA=(SID=abc)))'

有人能帮我问问我的脚本是什么问题。

预先感谢

1 个答案:

答案 0 :(得分:0)

使用Oracle数据库连接。进行了相应的更改,并且有效,使用Oracleclient条目代替了SQLClient。

 $OracleConnection = New-Object System.Data.OracleClient.OracleConnection