Hope you are well
I'm going to explain my situation
I need to get the values from a Excel Sheet (in a shared folder) to export it and then create a SQL Table in my instance. After of this I will execute a Stored Procedure (I already created) in order to create extended properties for a specific schema on tables,columns and rows.
By the moment I was able to get the data from excel sheet (using powershell) and saved in a variable but the problem is that I needed to add one column called 'SchemaName' because the Stored Procedure need to finds only values with the same schema name. (In the table will exist many values with differences schema names ) So in order to move forward this. I added a column with the schema name required but even if the output of the variable is correct when I try to create the table pop up an error or just import data without the column 'SchemaName'
Here is my code:
$DataSource= '\\servername\temp\test.xlsx' -sharedfolder path
$cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source=c:\temp\test.xlsx;Mode=Share Deny Write;Jet OLEDB:Engine Type=37;";
$cnStr
$cn = New-Object System.Data.OleDb.OleDbConnection $cnStr;
$cn.Open()
$cmd = $cn.CreateCommand()
$cmd.CommandText = "SELECT * FROM [Sheet1$]"
$rdr = $cmd.ExecuteReader();
$dt = new-object System.Data.DataTable
$dt.Load($rdr)
$Database_Name = 'DBTest'
$Table_Name = 'Tbltest'
$Schema_Name = 'Schematest'
$dt | add-member -membertype noteproperty "Schema_Name" -Value $Schema_Name
$dt | write-dbadbtabledata -sqlinstance 'servername' -databasename $Database_Name -table $table_name -autocreatetable
Example of how looks the sheet
| Rol | Member | Description |
|---|---|---|
| Admin | name + email | Description of the rol |
| Data Lead | name + email | Description of the rol |
| Arquitect | name + email | Description of the rol |
| DBA | name + email | Description of the rol |
| QA | name + email | Description of the rol |
Also I tried with other functions o methods to create the table but the mainly reason that I believe is the columns seems that is added to the variable but in reality not.
Thanks in advance
NotePropertytype when datatable member properties arePropertytype? Why not just use$dt.Columns.Add('Schema_Name')?