-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabular_CheckDataSources.cs
More file actions
49 lines (45 loc) · 1.93 KB
/
Tabular_CheckDataSources.cs
File metadata and controls
49 lines (45 loc) · 1.93 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
using Microsoft.AnalysisServices.Tabular;
/*Installation instructions: https://docs.microsoft.com/en-us/sql/analysis-services/tabular-model-programming-compatibility-level-1200/install-distribute-and-reference-the-tabular-object-model?view=sql-analysis-services-2017
*/
string serverName = Dts.Variables["$Project::SSAS_ServerName"].Value.ToString(); // SSIS project parameter
string connectionString = "DataSource=" + serverName;
string oldServerName = "";
int counter = 0;
using (Server server = new Server())
{
server.Connect(connectionString);
// Loop over all databases
foreach(Database db in server.Databases)
{
counter = 0;
foreach (ProviderDataSource ds in db.Model.DataSources) // use ProviderDataSource and not the DataSource object
{
// only check the connections for specific connection. Remove if not necessary
if(ds.Name == "conn1" || ds.Name == "conn2")
{
if(!ds.ConnectionString.ToUpper().Contains(serverName.ToUpper()))
{
string conn = ds.ConnectionString;
// Example of connection string: "Provider=SQLNCLI11;Data Source=localhost;Integrated..."
// We need to find the old servername. Split up the connection string using ; as delimiter, find the data source and extract the servername.
char delimiter = ';';
string[] args = conn.Split(delimiter);
foreach (string s in args)
{
// we need the connection property that starts with "Data Source="
if(s.StartsWith("Data Source="))
{
oldServerName = s.Replace("Data Source=", ""); // fetch the server name by getting rid of everything else in the property
}
}
ds.ConnectionString = conn.Replace(oldServerName, serverName);
counter++;
}
}
}
if (counter > 0) // one or more connection strings have been updated
{
db.Update(Microsoft.AnalysisServices.UpdateOptions.ExpandFull); // this only seems to work if ExpandFull is used
}
}
}