Supports all versions of SQL Server.
Do you need to connect to the database? Here's a piece of code that shows how to query and get SQL data, simply configure your account information, server address, and SQL statements very simply and correctly:
Copy Code code as follows:
$Database = ' Name_of_sqldatabase '
$Server = ' 192.168.100.200 '
$UserName = ' DatabaseUserName '
$Password = ' Secretpassword '
$SqlQuery = ' Select * from TestTable '
# Accessing Data Base
$SqlConnection = New-object-typename System.Data.SqlClient.SqlConnection
$SqlConnection. ConnectionString = "Data source= $Server; Initial catalog= $Database; user id= $UserName;p wd= $Password"
$SQLCMD = New-object System.Data.SqlClient.SqlCommand
$SqlCmd. CommandText = $SqlQuery
$SqlCmd. Connection = $SqlConnection
$SqlAdapter = New-object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter. SelectCommand = $SQLCMD
$set = New-object Data.dataset
# Filling Dataset
$SqlAdapter. Fill ($set)
# Consuming Data
$Path = "$env: Temp\report.hta"
$set. Tables[0] | convertto-html | Out-file-filepath $Path
Invoke-item-path $Path