-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path02c_Generate_CSharpClasses.ps1
More file actions
67 lines (52 loc) · 2.72 KB
/
02c_Generate_CSharpClasses.ps1
File metadata and controls
67 lines (52 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
##-----------------------------------------------------------------------
##
#******************* Third attempt *****************************
##
##-----------------------------------------------------------------------
#Cleanup the old files
#Get-Item $GeneratorLocation\Output\*.cs | Remove-Item
# We have changed everything that can change into a script parameter!
# Change the file locations to match where you downloaded the samples to
#-------------------------------------------------------------
param(
[Parameter(Mandatory=$false)][string]$ServerInstance = 'localhost',
[Parameter(Mandatory=$true)][string]$DatabaseName,
[Parameter(Mandatory=$false)][string]$GeneratorSQLFile = 'C:\1Presentations\Practical_PowerShell\02_Generate_CSharpClasses\ModelGenerator.sql',
[Parameter(Mandatory=$false)][string]$TableListSQL = 'SELECT name FROM sys.tables',
[Parameter(Mandatory=$false)][string]$OutputFolder = 'C:\1Presentations\Practical_PowerShell\02_Generate_CSharpClasses\Output',
[Parameter(Mandatory=$false)][string]$Namespace = 'MyCompany.Business',
[Parameter(Mandatory=$false)][string]$PlaceHolderSchema = '&Schema',
[Parameter(Mandatory=$false)][string]$PlaceHolderTableName = '&TableName',
[Parameter(Mandatory=$false)][string]$PlaceHolderNamespace = '&Namespace'
)
#Get the list of tables in the database to generate c# models for
$tables = Invoke-Sqlcmd2 `
-ServerInstance $ServerInstance `
-Database $DatabaseName `
-Query $TableListSQL `
-As DataRow `
-Verbose
foreach ($table in $tables)
{
$tableName = $table[0]
$outputFile = "$OutputFolder\$tableName.cs"
Write-Verbose "Generating for $tableName to file $outputFile"
#Replace variables with values (returns an array that we convert to a string to use as query)
$GeneratorSQLFileWSubstitutions = (Get-Content $GeneratorSQLFile).
Replace($PlaceHolderSchema,'dbo').
Replace($PlaceHolderTableName, $tableName).
Replace($PlaceHolderNamespace, $Namespace) | Out-String
"Ouputing for $tableName to $outputFile"
#The command generates .cs file content for model using "PRINT" statements which then gets written to verbose output (stream 4)
# ...capture the verbose output and redirect to a file
(Invoke-Sqlcmd2 `
-ServerInstance $ServerInstance `
-Database $DatabaseName `
-Query $GeneratorSQLFileWSubstitutions `
-Verbose) 4> $outputFile
}
#
#Show the output generated and delete the files
#
#Get-Item $GeneratorLocation\Output\*.cs | Remove-Item