Posts

Showing posts from September, 2017

PowerShell Script for Export to MS SQL Database - data and file upload

This post explains how to upload data from PowerShell script to MS SQL Database table. Also  it will uploads “Test.xml” data to the database table in binary format along with table data. Function ExportToDB { #Get the file [Byte[]]$file = get-content -Encoding Byte "E:\data\test.xml" #Connect to DB $ConnectionString ="Server=XX-XXXX\XXX,10000; Database=TESTDB;Integrated Security=true" $conn = new-object System.Data.SqlClient.SqlConnection($ConnectionString) Try { $conn.Open() #Build the command and parameters $sqlInsert = "INSERT INTO [SITSCD].[dbo].[ContentClassificationReports] (DateCreated,UserCreated,DateModified,UserModified,BinaryContent,Enviroment) VALUES (@DC,@UC,@DM,@UM,@File,@Env)" $cmd = new-object System.Data.SqlClient.SqlCommand($sqlInsert,$conn) $cmd.CommandType = [System.Data.CommandType]'Text' $cmd.Parameters.Add(“@File”, [System.Data.SqlDbType]'VarBinary') $cmd.Parameters.Add(“@D...