Powershell使用ODBC DSN代替连接字符串从数据库中读取 [英] Powershell to read from database using ODBC DSN instead of connection string
本文介绍了Powershell使用ODBC DSN代替连接字符串从数据库中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何使用连接字符串从数据库中读取值,即
I know how to read value from database using connectionstring, i.e.
$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "Server=10.10.10.10;Initial Catalog=database_name;User Id=$username;Password=$password;"
$SQL = "..."
$conn.Open()
# Create and execute the SQL Query
$cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn)
$count=0
do{
try{
$rdr = $cmd.ExecuteReader()
while ($rdr.read()){
$sql_output += ,@($rdr.GetValue(0), $rdr.GetValue(1))
$count=$count + 1
}
$transactionComplete = $true
}
catch{
$transactionComplete = $false
}
}until ($transactionComplete)
# Close the database connection
$conn.Close()
如何使用ODBC完成相同的操作,即在服务器上设置了DSN(数据源名称)?
How can I accomplish the same thing with ODBC, i.e I have DSN (data source name) set up on the server?
推荐答案
根据 https://www .connectionstrings.com/odbc-dsn/您将使用类似...
According to https://www.connectionstrings.com/odbc-dsn/ you would use something like...
DSN=myDsn;Uid=myUsername;Pwd=;
如果不需要凭据,可以直接使用DSN=...
.
Can probably just go with DSN=...
if creds not required.
这篇关于Powershell使用ODBC DSN代替连接字符串从数据库中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文