Speaking the language of business intelligence with a Microsoft accent

Tuesday, October 4, 2005

SSRS - Dynamic Connection Strings

One of the cool features of SSRS in the September CTP is the ability to change data source connection strings dynamically using functions.

In RS2000 with a little bit of work we were able to give users the ability to repoint a report to a database with a consistent schema on the same server by using dynamic SQL in the data tab and passing the database name as a query parameter like this:

="select ContactName, City, PostalCode from " & Parameters!DBName.value & "..Customers"

Sending the user to a different server was possible but took a bit more work.

The RS team have now solved the issue by adding a function button to the connection string dialogue box when defining the data source for a data set.














Now, instead of stringing dynamic SQL queries together you can simply create a parameter-driven data source string. The DB name, server name and any other part of the connection string you want can change according to your design. There are quite a few ways I can think of using this new functionality. A particular SQL2005 example? Think database snapshots: give your users the ability to report off their choice of a regularly created database snapshot. Simple user-driven point-in-time reporting. Very interesting...

I've created a simple example report which dynamically connects to one of three different databases. In order to set up the databases execute the script in DynamicDBCreate.sql. This will create 3 databases each with one table called MyTestTable with a database specific record in each. Import the Dynamic Data Source.rdl file into a SSRS 2005 solution and run it. There is no need to create a shared data source. Dynamic connection functionality can only be embedded in a report, the connection function button is disabled on shared data sources. Once finished you can clean up the databases by running the DynamicDBDrop.sql script. Download the files here.

Enjoy!

16 comments:

Anonymous said...

i cant't download your Samplecode.

Nick Barclay said...

Strange. What exact problem are you having?

Anonymous said...

In your Blog, you say that it is also possible to make the server connection dynamic in SSRS 2000 although it takes some efort. Do you have an example of how this works?

Nick Barclay said...

In order to achieve this you first need to create a Linked Server on the server you are initially connected to. You then reference this in your dynamic SQL string to connect to the other server. Instead of using the report parameter to dynamically add just the [DatabaseName] part of the string you would be passing in [LinkedServerName].[DatabaseName]. For example the finished string would look something like: select * from MyLinkedServer.Northwind.dbo.Customers instead of the single server alternative select * from Northwind..Customers.

HTH
Nick

Anonymous said...

The exact problem I'm having is that the Samplecode won't download.

Nick Barclay said...

Just checked. The code is definitely still available. There may be something in your environment that is blocking it. If you like email me at nick.barclay[at]gmail.com and I will send you the sample code.

Cheers,
Nick

Anonymous said...

How come I don't see the "Function button" is enabled when creating a shared data source using VS 2005? The trick to dynamically change connection string does not work if this is not allowed. Pls tell me why. Thanks!

Nick Barclay said...

The function button is disabled because you can't dynamically change a shared data source - this is by design. The data source for reports with dynamic connection functionality need to be local (i.e. not shared) to the report itself.

HTH,
Nick

Asmita said...

Okay,

I am new to SSRS and wanted to know how can I create a Local data source for a report.

Also , I want the same report to dynamically fecth the server and Database Name and other connection properties from my connection strings that I store locally as UDL files.

Is ur this post would do the trick ?

Thanks,
Asmita

Anonymous said...

I am getting 'connection string expression are not allowed in shared data sources" error after giving the connection string as "="Data Source=(local); Initial Catalog=" &Parameters!DB.Value". How can I give the dynamic connection string

Nick Barclay said...

You can't have a shared data source with a dynamic connection string. The data source must be local to the report.

HTH,
Nick

Dom said...

That anonymous guy is very rude, what does he want? the moon on a stick!

guy_loving said...

Thanks for the article.

My problem is that I am having a parameter like this..
=Parameters!SQL.Value

While deploying in VS 2005 i am getting the error message
"[rsParameterReference] The ConnectString expression for the data source ‘DS1’ refers to a non-existing report parameter ‘ConnectionString’. ".

What could be the reason.. Could you help me to sort this out?

thanks

Raj

Anonymous said...

hai,
I am trying to a offline report with individual connection can any one help me out

Anonymous said...

i am trying to connect

tushar said...

Hi Nick,

This is related to passing connection string to a ssrs report as parametrs. I can change my server name, database as per the user requiremnt. I am using following code.
="data source=" & Parameters!ServerName.Value & ";initial catalog=" & Parameters!DBName.Value & ";User=" & Parameters!UserN.Value & ";Password=" & Parameters!pass.Value & ";"
And its working fine without any report parameter.

But when i use report parameter in a where clause and try to execute the same it gives me error as "Error during processing ConnectionString expression of dtasource 'DS1'" DS1 is a data source name.

Please let me know if there is any solution for this problem.

Thanks,
Tushar