I have a BIML file inspired by this post .
However, I am getting an error:
Error 0 The name 'fileName' does not exist in the current context. (32,16`)
I have the feeling I am missing something really obvious.
I am running this in VS 2019 + SSDT
and BIML Express
(both latest versions as of this moment)
I am talking to
Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64)
Aug 22 2017 17:04:49
Copyright (C) 2017 Microsoft Corporation
Developer Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor)
This is my BIML code:
<#@ template language="C#" hostspecific="true"#>
<#@ import namespace="System.IO"#>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<#
string Prefix="import";
// the locatie of the csv's'
string path = @"C:\test";
// Put all the filenames with the extension csv in a string array
string[] myFiles = Directory.GetFiles(path, "*.csv");
// string that will be filled with the filename
string filename;
// string array for columnnames extracted from CSV
string[] myColumns;
#>
<Connections>
<OleDbConnection
Name="OLEDB_STG_<#=Prefix#>"
ConnectionString="Data Source=SQLSERVER;Initial Catalog=TEST;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;">
</OleDbConnection>
</Connections>
<Databases>
<Database ConnectionName="OLEDB_STG_<#=Prefix#>" Name="TEST"/>
</Databases>
<Schemas>
<Schema Name="dbo" DatabaseName="TEST" Owner="dbo"/>
</Schemas>
<Tables>
<!-- loop trough the array of files-->
<# foreach(string filePath in myFiles)
{
// extract the filename from the path to use as tablename
fileName = Path.GetFileNameWithoutExtension(filePath);
#>
<Table Name="<#=Prefix#>_<#=fileName#>" SchemaName="TEST.dbo">
<Columns>
<!-- loop trough the file looking for the columnnames-->
<#
// read first row of csv to extract to columnnames
// and split on comma to create an array
StreamReader myFile = new StreamReader(filePath);
myColumns = myFile.ReadLine().Split(',');
// close file after reading first line
myFile.Close();
// Loop through column array
foreach(string myColumn in myColumns)
{
#>
<Column Name="<#=myColumn#>" DataType="String" Length="255"></Column>
<# } #>
</Columns>
</Table>
<# }#>
</Tables>
</Biml>
C# is a case-sensitive language so filename
does not refer to the same variable as fileName
, i.e. these don't match:
string filename;
fileName = Path.GetFileNameWithoutExtension(filePath);