1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
Function Sql_Query([string] $sqServer, [string] $sqBase, [string] $sqQuery)
{
Clear
#Etablit la connexion avec la base SQL.
$Connection = New-Object System.Data.SqlClient.SqlConnection
$Connection.ConnectionString = "Server = $sqServer; Database = $sqBase; Integrated Security = True"
#Crée une commande SQL.
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandTimeout = 30
$sqlCmd.CommandText = $sqQuery
$sqlCmd.Connection = $Connection
#Exécute et récupère le résultat.
$DataSet = New-Object System.Data.DataSet
$Adapter = New-Object System.Data.SqlClient.SqlDataAdapter
$Adapter.SelectCommand = $sqlCmd
$Adapter.Fill($DataSet)
#Ferme la connexion avec la base SQL.
$Connection.Close()
#Renvoie le résultat.
return $DataSet.Tables[0]
}
#Paramètres de connexion.
$SQLSERVER = "OPQSESB01\MS_QSE_SSDS"
$SQLBASE = "1-transfert"
$SqlQuery = "SET NOCOUNT ON select count(*) from [1-transfert].[dbo].[CATEGORIE]"
#Exécute la requête.
$retour = Sql_Query $SQLSERVER $SQLBASE $SqlQuery
foreach ($raw in $retour)
{
Try { write-host $raw[0] } catch {}
}
write-host "Exécution terminé."
#Fin. |
Partager