I'm trying to use a simple insert query to fill a SQL table. Currently, my script creates the query by concatenating strings but I would like to parameterize the query in order to avoid getting the following error:
Exception calling "ExecuteNonQuery" with "0" argument(s): "String or binary data would be truncated.
Currently I am setting up my connection, like so:
$conn = New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server=$SQLServer;Database=$SQLDatabase;Integrated Security=True;Connect Timeout=0"
$conn.ConnectionString = $ConnectionString
$conn.Open()
and my un-parameterized query looks like this:
$commandText = "INSERT INTO Servers (Name, OS, Notes, OSOwner) VALUES('$myvm', '$operatingSystem', '$desc', '$owner')"
$command = $conn.CreateCommand()
$command.CommandText = $commandText
$command.ExecuteNonQuery()
I've seen people discuss different solutions to this in other posts but I'm not sure what the best approach is.