Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

I need help with powershell and sql server for generate file base on row value

$
0
0

Hello Dears,
first sorry I new bee to powershell and I have to realize this script for my job.
on a sql server database, I have a table with 3 columns ( ClientId,ReportId,Details). I have to generate for each rown of my table a file containing the value of the column Details,the file generate has to be name with the value of ReportId and store in a local folder having for name the value of ClientId.
Exple my table ClientID,ReportId,Details
58001 1040 {\rtf1\ans} I will on my C:\RootFolder\58001\1040.rtf for exple..
ps: that column Details store native RTF coding, therefore i need to generate my file with rtf extension.
and my table has over 200’000 rows..
Please help.
here is what i have started,, but really difficult to understand it all..

################################ Script That I started ###############################################

$ServerConnectionString = "Data Source=UK138032\VASCO;Initial Catalog=CMTLite;trusted_connection=true;"
$ServerConnection = new-object system.data.SqlClient.SqlConnection($ServerConnectionString);

$dataSet = new-object "System.Data.DataSet" "ClientId"
$query = "SELECT DISTINCT [ClientId] FROM [CompanyDelta].[dbo].[Orders]"

$dataAdapter = new-object "System.Data.SqlClient.SqlDataAdapter" ($query, $ServerConnection)
$dataAdapter.Fill($dataSet) | Out-Null # if i'm not mistaking at this level, I have in my dataset all my distinct clientId

##############

############## From here I will need for each ClientId , to generate it's respective Details, ( maybe one or multiple details). and they need to be generate with filename being the respective reportid.rtf

######### a friend gave me the bottom part of this, but not working, and I don't know what to do #########

$ServerAConnection.Close()

$dataTable = new-object "System.Data.DataSet" "ReportId"
$dataTable = $dataSet.Tables[0]

$ServerConnectionString = "Data Source=UK138032\VASCO;Initial Catalog=CMTLite;trusted_connection=true;"
$ServerConnection = new-object system.data.SqlClient.SqlConnection($ServerConnectionString);
$ServerConnection.Open()

$dataTable | FOREACH-OBJECT {
$cmd = new-object System.Data.SQLClient.SQLCommand
$cmd.CommandText = "SELECT [Details] FROM [CompanyDelta].[dbo].[Orders] where ClientId= @ClientId"
$cmd.Connection = $ServerConnection
$cmd.Parameters.AddWithValue("@ClientId", $_.Clientid)
$rowsUpdated = $cmd.ExecuteNonQuery()

}

$ServerBConnection.Close()


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>