2

I want to insert values into database when the biml code is ran and the package has completed expansion is this possible using BIML or c#?

I have a table called BIML expansion created in my DB and I have test.biml which loads the package test.dtsx whenever the BIML expansion is completed a record should be inserted into my table that expansion has been completed.

Let me know if you have any questions or needs any additional info.

From comments

I tried your code

string connectionString = "Data Source=hq-dev-sqldw01;Initial Catalog=IM_Stage;Integrated Security=SSPI;Provider=SQLNCLI11.1"; 
string SrcTablequery=@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES (@audit_id, @Package,@audit_Logtime)"; 
DataTable dt = ExternalDataAccess.GetDataTable(connectionString,SrcTablequery);

It has an error below must declare the scalar variable audit_id can you let me know the issue behind it?

billinkc
  • 59,250
  • 9
  • 102
  • 159
Sqldev_91
  • 81
  • 4

1 Answers1

2

In it's simplest form, you'd have content like this in your Biml script

// Define the connection string to our database 
string connectionStringSource = @"Server=localhost\dev2012;Initial Catalog=AdventureWorksDW2012;Integrated Security=SSPI;Provider=SQLNCLI11.1";

// Define the query to be run after *ish* expansion
string SrcTableQuery = @"INSERT INTO dbo.MyTable (BuildDate) SELECT GETDATE()";

// Run our query, nothing populates the data table
DataTable dt = ExternalDataAccess.GetDataTable(connectionStringSource, SrcTableQuery);

Plenty of different ways to do this - you could have spun up your own OLE/ADO connection manager and used the class methods. You could have pulled the connection string from the Biml Connections collection (depending on the tier this is executed in), etc.

Caveats

Depending on the product (BimlStudio vs BimlExpress), there may be a background process compiling your BimlScript to ensure all the metadata is ready for intellisense to pick it up. You might need to stash that logic into a very high tiered Biml file to ensure it's only called when you're ready for it. e.g.

<#@ template tier="999" #>
<#
// Define the connection string to our database 
string connectionStringSource = @"Server=localhost\dev2012;Initial Catalog=AdventureWorksDW2012;Integrated Security=SSPI;Provider=SQLNCLI11.1";

// Define the query to be run after *ish* expansion
string SrcTableQuery = @"INSERT INTO dbo.MyTable (BuildDate) SELECT GETDATE()";
// Run our query, nothing populates the data table
DataTable dt = ExternalDataAccess.GetDataTable(connectionStringSource, SrcTableQuery);
#>

Is that the problem you're trying to solve?

Addressing comment/questions

Given the query of

string SrcTablequery=@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES (@audit_id, @Package,@audit_Logtime)"; 

it errors out due to @audit_id not being specified. Which makes sense - this query specifies it will provide three variables and none are provided.

Option 1 - the lazy way

The quickest resolution would be to redefine your query in a manner like this

string SrcTablequery=string.Format(@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES ({0}, '{1}', '{2})'", 123, "MyPackageName", DateTime.Now); 

I use the string library's Format method to inject the actual values into the placeholders. I assume that audit_id is a number and the other two are strings thus the tick marks surrounding 1 and 2 there. You'd need to define a value for your audit id but I stubbed in 123 as an example. If I were generating packages, I'd likely have a variable for my packageName so I'd reference that in my statement as well.

Option 2 - the better way

Replace the third line with .NET library usage much as you see in heikofritz on using parameters inserting data into access database.

1) Create a database Connection 2) Open connection 3) Create a command object and associate with the connection 4) Specify your statement (use ? as your ordinal marker instead of named parameters since this is oledb) 5) Create an Parameter list and associate with values

Many, many examples out there beyond the referenced but it was the first hit. Just ignore the Access connection string and use your original value.

billinkc
  • 59,250
  • 9
  • 102
  • 159
  • I tried your code string connectionString = "Data Source=hq-dev-sqldw01;Initial Catalog=IM_Stage;Integrated Security=SSPI;Provider=SQLNCLI11.1"; string SrcTablequery=@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES (@audit_id, @Package,@audit_Logtime)"; DataTable dt = ExternalDataAccess.GetDataTable(connectionString,SrcTablequery); It has an error below must declare the scalar variabl audit_id can you let me know the issue behind it. – Sqldev_91 Jun 20 '19 at 18:46
  • Thanks for your help and I have the code above but it has some issues that the (@audit_id) has to be declared.Can you let me know how to fix it. – Sqldev_91 Jun 20 '19 at 18:51
  • I have tried the other method string connectionString = "Data Source=HQ-DEV-SQLDW01;Initial Catalog=IM_STAGE;Integrated Security=SSPI;Auto Translate=False;Provider=System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;"; SqlConnection con = new SqlConnection(connectionString); con.open(); SqlCommand cmd = new SqlCommand("INSERT INTO audit.BIML_audit (Package,audit_Logtime) VALUES ( 'xyz', GETDATE())", con); cmd.ExecuteNonQuery(); – Sqldev_91 Jun 21 '19 at 13:52
  • The above code produces following error 'SqlConnection' does not contain a definition for 'open' and no extension method 'open' accepting a first argument of type 'SqlConnection' could be found (are you missing a using directive or an assembly reference? – Sqldev_91 Jun 21 '19 at 13:54
  • 1
    Methods in C# are case sensitive thus `con.Open` – billinkc Jun 21 '19 at 14:25
  • Thanks for all your guidance it is working fine now.Also do you suggest any book to start writing BIML and get hand on. – Sqldev_91 Jun 24 '19 at 19:06
  • [The Biml Book](https://www.apress.com/us/book/9781484231340) is the only book on the market and it covers everything that was available at the end of 2017 – billinkc Jun 24 '19 at 22:07