Advertisement
Combreal

insertSQLQuery.ps1

Mar 24th, 2024 (edited)
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requieres MySQL Connector NET 8.3.0
  2. [void][system.reflection.Assembly]::LoadFrom("C:\Program Files (x86)\MySQL\MySQL Connector NET 8.3.0\MySql.Data.dll")
  3.  
  4. Function New-insertSQLQuery ($connectionProperties, $CustomerName, $ContactName, $Address, $City, $PostalCode, $Country) {
  5.     $Connection = New-Object  MySql.Data.MySqlClient.MySqlConnection
  6.     $Connection.ConnectionString="server=$($connectionProperties.mysqlHost);uid=$($connectionProperties.mysqlUser);pwd=$($connectionProperties.mysqlPass);database=$($connectionProperties.mysqlDatabase)"
  7.     $Connection.Open()
  8.    
  9.     $command = New-Object MySql.Data.MySqlClient.MySqlCommand
  10.     $command.Connection = $Connection
  11.     $command.CommandText = "INSERT INTO customers (CustomerName,ContactName,Address,City,PostalCode,Country) VALUES (@CustomerName,@ContactName,@Address,@City,@PostalCode,@Country)"
  12.     $command.Parameters.AddWithValue("@CustomerName", $CustomerName) | Out-Null
  13.     $command.Parameters.AddWithValue("@ContactName", $ContactName) | Out-Null
  14.     $command.Parameters.AddWithValue("@Address", $Address) | Out-Null
  15.     $command.Parameters.AddWithValue("@City", $City) | Out-Null
  16.     $command.Parameters.AddWithValue("@PostalCode", $PostalCode) | Out-Null
  17.     $command.Parameters.AddWithValue("@Country", $Country) | Out-Null
  18.     $command.ExecuteNonQuery() | Out-Null
  19.      
  20.     $Connection.Close()
  21. }
  22.  
  23. $connectionProperties = New-Object PSObject
  24. $connectionProperties | Add-Member Noteproperty "mysqlHost" "localhost"
  25. $connectionProperties | Add-Member Noteproperty "mysqlUser" "myuser"
  26. $connectionProperties | Add-Member Noteproperty "mysqlPass" "mypswd"
  27. $connectionProperties | Add-Member Noteproperty "mysqlDatabase" "mydp"
  28.  
  29. $CustomerName = "Combreal"
  30. $ContactName = "Tony Montana"
  31. $Address = "5, rue des champs Élysées"
  32. $City = "Paris"
  33. $PostalCode = "75008"
  34. $Country = "France"
  35.  
  36. New-insertSQLQuery $connectionProperties $CustomerName $ContactName $Address $City $PostalCode $Country
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement