Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requieres MySQL Connector NET 8.3.0
- [void][system.reflection.Assembly]::LoadFrom("C:\Program Files (x86)\MySQL\MySQL Connector NET 8.3.0\MySql.Data.dll")
- Function New-insertSQLQuery ($connectionProperties, $CustomerName, $ContactName, $Address, $City, $PostalCode, $Country) {
- $Connection = New-Object MySql.Data.MySqlClient.MySqlConnection
- $Connection.ConnectionString="server=$($connectionProperties.mysqlHost);uid=$($connectionProperties.mysqlUser);pwd=$($connectionProperties.mysqlPass);database=$($connectionProperties.mysqlDatabase)"
- $Connection.Open()
- $command = New-Object MySql.Data.MySqlClient.MySqlCommand
- $command.Connection = $Connection
- $command.CommandText = "INSERT INTO customers (CustomerName,ContactName,Address,City,PostalCode,Country) VALUES (@CustomerName,@ContactName,@Address,@City,@PostalCode,@Country)"
- $command.Parameters.AddWithValue("@CustomerName", $CustomerName) | Out-Null
- $command.Parameters.AddWithValue("@ContactName", $ContactName) | Out-Null
- $command.Parameters.AddWithValue("@Address", $Address) | Out-Null
- $command.Parameters.AddWithValue("@City", $City) | Out-Null
- $command.Parameters.AddWithValue("@PostalCode", $PostalCode) | Out-Null
- $command.Parameters.AddWithValue("@Country", $Country) | Out-Null
- $command.ExecuteNonQuery() | Out-Null
- $Connection.Close()
- }
- $connectionProperties = New-Object PSObject
- $connectionProperties | Add-Member Noteproperty "mysqlHost" "localhost"
- $connectionProperties | Add-Member Noteproperty "mysqlUser" "myuser"
- $connectionProperties | Add-Member Noteproperty "mysqlPass" "mypswd"
- $connectionProperties | Add-Member Noteproperty "mysqlDatabase" "mydp"
- $CustomerName = "Combreal"
- $ContactName = "Tony Montana"
- $Address = "5, rue des champs Élysées"
- $City = "Paris"
- $PostalCode = "75008"
- $Country = "France"
- New-insertSQLQuery $connectionProperties $CustomerName $ContactName $Address $City $PostalCode $Country
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement