-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path02b_Generate_CSharpClasses.ps1
More file actions
58 lines (47 loc) · 2.27 KB
/
Copy path02b_Generate_CSharpClasses.ps1
File metadata and controls
58 lines (47 loc) · 2.27 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
##-----------------------------------------------------------------------
##
#******************* Second attempt *****************************
##
##-----------------------------------------------------------------------
#Cleanup the old files
Get-Item $ClassGenOutputLocation\*.cs | Remove-Item
# We have moved up everything that can change into variables!
#-------------------------------------------------------------
# Change the below to point to where you have the ModelGenerator.sql file
[string]$GeneratorLocation = 'C:\1Presentations\Practical_PowerShell\02_Generate_CSharpClasses\'
[string]$ServerInstance = 'localhost'
[string]$DatabaseName = 'DataStudio4'
[string]$GeneratorSQLFile = "$GeneratorLocation\ModelGenerator.sql"
[string]$TableListSQL = 'SELECT name FROM sys.tables'
[string]$OutputFolder = $ClassGenOutputLocation
[string]$Namespace = 'MyCompany.Business'
[string]$PlaceHolderSchema = '&Schema'
[string]$PlaceHolderTableName = '&TableName'
[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
}