Create Data Source Using Assembly File
- Last UpdatedJun 10, 2024
- 2 minute read
The following example demonstrates the creation of data source by fetching the connection string from the assembly file. Here the connection string is fetched from the connection class by providing the proper path in the class CreateDataSourceUsingAssembly.
PROCEDURE
Dll REFERENCE
Workflow.NET.NET2.dll
NAMESPACE USED
Workflow.NET
Create a ClassLibrary Project by name ConnectionClass and build it. Use the ConnectionClass.dll in the next Sample code.
using System;
using Workflow.NET.Interfaces;
namespace ClassLibrary1
{
/// <summary>
/// Sample Class to retrieve Connection String
/// </summary>
public class ConnectionClass : IDBConnectionString
{
public string GetConnectionString(string ApplicationName, object Parameter)
{
// Database Name is not required.New database will be created in the below code sample
return "Data Source=localhost;Integrated Security=True;Pooling=False;";
}
}
}
Give the path of the ConnectionClass.dll, created by the above sample code for restarting AVEVA Work Tasks services as a parameter to the Method LoadAssembly () in the below code.
PROCEDURE
Dll REFERENCE
Workflow.NET.NET2.dll
Skelta.Configurations.DB.dll
NAMESPACE USED
Skelta.Configurations.DB.Interfaces
Skelta.Configurations.DB
Workflow.NET
Skelta.FarmManager
Add the reference of the ConnectionClass.dll that is created above.
Try the following code in a console application by adding the above references and name spaces:
using System;
using Skelta.Configurations.DB.Interfaces;
using Skelta.Configurations.DB;
using Workflow.NET;
using Skelta.FarmManager;
namespace CreateDataSource
{
/// <summary>
/// Sample Class to create Data Source using Assembly file.
/// Add Reference ConnectionClass.dll to this code before running it.
/// </summary>
class CreateDataSourceUsingAssembly
{
static void Main(string[] args)
{
Test();
}
static void Test()
{
try
{
string _ClassName = "";
string connectionString = "";
//Provide the new Database name
string dataBase = "SampleDataBase";
//Creating a Data Source Object with name dataSource
DataSource dataSource = new DataSource();
//Provide the new Name for the Data Source
dataSource.Name = "SampleDataSource";
//Check whether the Data Source Name Exists in Farm Database
DataSourceCollection datasourceCollec = new DataSourceCollection();
if (datasourceCollec.Contains(dataSource.Name))
{
datasourceCollec = null;
throw new Exception("Data Source \"" + dataSource.Name + "\" already exists in Farm Database");
}
//Set Description for Data Source
dataSource.Description = "SampleDataSource";
//Set SourceType for Data Source(SQL=SQL Server)
dataSource.SourceType = "SQL Server";
//Get Connection String from an Assembly
//*****Give the proper path of the ConnectionClass.dll for LoadAssembly Method********
\\ Provide the path of the Connection class dll that you created in the third code sample Implementation of IDBConnectionString
System.Reflection.Assembly getAssembly = Workflow.NET.CommonFunctions.LoadAssembly("C:\\ConnectionClass\\bin\\Debug\\ConnectionClass.dll");
foreach (Type counterType in getAssembly.GetTypes())
{
if (counterType.IsClass)
{
if (Workflow.NET.CommonFunctions.IsInterfaceImplementedByType(counterType, typeof(Workflow.NET.Interfaces.IDBConnectionString)))
{
_ClassName = counterType.ToString();
break;
}
}
}
connectionString = Workflow.NET.CommonFunctions.GetConnectionString("Central Config", _ClassName, Environment.CurrentDirectory.ToString()+"\\ConnectionClass.dll", "", "", null);
//Set ConnectionStringXML for the Data Source
dataSource.ConnectionStringXML = "<datasource >" + connectionString + "Database=" + dataBase + "</datasource >";
//Set DataBase handler
IDBCore coreHandler = DBCoreHandler.GetHandler(connectionString, "SQL Server");
//Create a new DataBase with Product Tables
coreHandler.CreateDatabaseWithReadCommittedSnapshot(dataBase);
Console.WriteLine("Database \"" + dataBase + "\" was successfully created");
coreHandler.RunScript(Config.GetConfigFilePath() + "DatabaseScripts\\SQLServer\\Workflow.NET.sql");
//Run the Product Script to create Product Tables in DataBase
coreHandler.RunScript(Config.GetConfigFilePath() + "DatabaseScripts\\SQLServer\\Workflow.NET.sql");
//Create a Database Archive with Product Tables
coreHandler = DBCoreHandler.GetHandler(connectionString, "SQL Server");
coreHandler.CreateDatabaseWithReadCommittedSnapshot(dataBase + "_BI");
Console.WriteLine("Database Archive \"" + (dataBase.ToString() + "_BI") + "\" was successfully created");
//Run the Product Script to create Product Tables in DataBase Archive
coreHandler.RunScript(Config.GetConfigFilePath() + "DatabaseScripts\\SQLServer\\Workflow.NET.sql");
//Save the Data Source
dataSource.Save();
Console.WriteLine("DataSource \"" + dataSource.Name + "\" was successfully created in the Farm Database");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}