Welcome to MSDN Blogs Sign in | Join | Help

here is a small sample that show how to run an existing package from a .Net program.

This sample show how to replace target and source connection string and give a value to a package variable.

It show too how to register package event.

using Runtime = Microsoft.SqlServer.Dts.Runtime;

private PackageEvents packageEvents;

public event PackageEventEventHandler packageEvent = null;

public event PackageProgressEventHandler packageProgress = null;

 

packageEvents = new PackageEvents();

packageEvents.packageEvent += new PackageEventEventHandler(packageEvents_packageEvent);

packageEvents.packageProgress += new PackageProgressEventHandler(packageEvents_packageProgress);

 

public bool Execute(string packagePath, string sourceConnectionString, string targetConnectionString, string packageVariable)

{

Runtime.Application app = new Runtime.Application();

Runtime.Package package = app.LoadPackage(packagePath, null);

package.InteractiveMode = false;

// Target is the name of the target connection in the SSIS package

package.Connections["Target"].ConnectionString = targetConnectionString;

package.Connections["Source"].ConnectionString = sourceConnectionString;

// The package have a variable, we give it the value

package.Variables["myVar"].Value = packageVariable;

Runtime.DTSExecResult executionResult = package.Execute(null, null, packageEvents, null, null);

if (executionResult == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure)

{

return false;

}

return true;

}

void packageEvents_packageProgress(int percentComplete)

{

if (packageProgress != null)

packageProgress(percentComplete);

}

void packageEvents_packageEvent(EventType type, string message)

{

if (packageEvent != null)

packageEvent(type, message);

}

I was very suprise to don't find a tool to execute XMLA Script from the command line. Then I have decided to write it and it have been simple.

Let do it !

1) Use the useful namespace

using Microsoft.AnalysisServices.AdomdClient;

using System.Data;

using System.IO;

using System.Xml;

 

2) Start by connecting to Analysis Services 2005 using Adomd.

strConnectionString = "Data Source=" + ServerID + ";";

objConnection = new AdomdConnection();

objConnection.ConnectionString = strConnectionString;

objConnection.Open();

 

3) And just execute the Command

AdomdCommand objCommand;

XmlReader objReader;

FileStream objStream;

 

objCommand = new AdomdCommand();

objCommand.Connection = objConnection;

objStream = new FileStream(objScript, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

objCommand.CommandStream = objStream;

objReader = objCommand.ExecuteXmlReader();

objReader.MoveToContent();

objReader.Read ();

Console.WriteLine (objReader.ReadInnerXml());

 

That's All !

Just go here : GO
You can get SQL Server 2005 CTP June and Visual Studio 2005 beta 2. Enjoy :)
0 Comments
Filed under:

Once you are connected to SQL Server 2005 with a SMO connection, you can start manage and create object. Here is an example on how you can create a stored procedure, we imagine that we have the code of a stored procedure in an embeded resources file. (this sample is written in C# and use a beta version of SQL Server 2005 and of the .Net Framework 2.0).

For this sample you need to use the following name space :

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Smo.Agent;

using Microsoft.SqlServer.Management.Common;

 

And here is the code of the sample :

StoredProcedure mySP;

string myResourceFile;

mySP = new StoredProcedure(sqlserver.database, "createNewYear");

myResourceFile = "MyAssemblieName.StoredProc.sql";

StreamReader sr = new StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(myResourceFile), System.Text.Encoding.Default);

mySP.TextBody = sr.ReadToEnd();

mySP.Create();

2 Comments
Filed under:

When you have an SMO connection, it is very easy to make an ADO.Net connection as the connection string is exactly the same. Then you just have to reuse it.

Here is a sample that supposed that the object ServerConn is an SMO ServerConnection.

 

SqlConnection mySQLConnection;

 

mySQLConnection = new SqlConnection(ServerConn.ConnectionString);

mySQLConnection.Open();

1 Comments
Filed under: ,

Once you are connected to SQL Server 2005 with a SMO connection, you can start manage and create object. Here is an example on how you can create a table with a promary key (this sample is written in C# and use a beta version of SQL Server 2005 and of the .Net Framework 2.0).

The Object sqlserver, is the object created after the connection in this post about smo connection

Of course, In order to connect to SQL Server using SMO you have to reference the following namespace (and the corresponding assembly):

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Smo.Agent;

using Microsoft.SqlServer.Management.Common;

 

And the code to create the table is :

Table tbl;

Column col;

Index idx;

tbl = new Table(sqlserver.database, "MyTable");

col = new Column(tbl, "MyCol1", DataType.Int);

tbl.Columns.Add(col);

col.Nullable = false;

// Add the primary key index

idx = new Index(tbl, "PK_MyTable");

tbl.Indexes.Add(idx);

idx.IndexedColumns.Add(new IndexedColumn(idx, col.Name));

idx.IsClustered = true;

idx.IsUnique = true;

idx.IndexKeyType = IndexKeyType.DriPrimaryKey;

col = new Column(tbl, "myCol2", DataType.SmallInt);

tbl.Columns.Add(col);

col.Nullable = false;

// Create the table

tbl.Create();

0 Comments
Filed under:

This sample is written in C# and use SQL Server 2005 beta 2 and a beta version of the .Net Framework 2.0. It show how, with SMO you can Connect to SQL Server 2005

In order to connect to SQL Server using SMO you have to reference the following namespace (and the corresponding assembly):

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Smo.Agent;

using Microsoft.SqlServer.Management.Common;

 

1) The following code allow you to connect with the intergrated security :

ServerConnection conn;

Server sqlserver;

conn = new ServerConnection();

conn.ServerInstance = "My Server"

conn.LoginSecure = true;

conn.Connect();

sqlserver = new Server(conn);

The object sqlserver is the object on with youe are going to work on each SMO project (to have to list of database for example).

 

2) The following code allow you to connect to SQL Server with the SQL Server authentification :

ServerConnection conn;

Server sqlserver;

conn = new ServerConnection();

conn.ServerInstance = "My Server";

conn.Login = "My login";

conn.Password = "My Password";

conn.LoginSecure = true;

conn.Connect();

sqlserver = new Server(conn);

 

0 Comments
Filed under:

This sample is written in C# and use SQL Server 2005 beta 2 and a beta version of the .Net Framework 2.0. It show how, with ADOMD.Net, you can retreive KPI that are defined in Analysis Services 2005.

1) reference the namespace for ADOMD:

using Microsoft.AnalysisServices.AdomdClient;

2) Build your connection string and connect to Analysis Services

string myConnectionString;

AdomdConnection myKPIConnection;

CubeDef myCubeDef;

 

myConnectionString = "Data Source=" + @myOlapServer + ";Catalog=\"" + @myOlapDatabase + "\"";

myKPIConnection = new AdomdConnection(myConnectionString);

myKPIConnection.Open();

myCubeDef = myKPIConnection.Cubes[myCube];

3) Build the command and query the Olap database :

AdomdCommand myKPICommand;

foreach (Kpi k in myCubeDef.Kpis)

{

   myKPICommand = new AdomdCommand();

   myKPICommand.Connection = myKPIConnection;

//build the MDX query that return the KPI Value

   myKPICommand.CommandText = "SELECT { strtomember(@Value), strtomember(@Goal), strtomember(@Status), strtomember(@Trend) } ON COLUMNS FROM [" +myCubeDef.Name + "]";

   myKPICommand.Parameters.Clear();

   myKPICommand.Parameters.Add(new AdomdParameter("Value", "KPIValue([" + k.Name + "])"));

   myKPICommand.Parameters.Add(new AdomdParameter("Goal", "KPIGoal([" + k.Name + "])"));

   myKPICommand.Parameters.Add(new AdomdParameter("Status", "KPIStatus([" + k.Name + "])"));

   myKPICommand.Parameters.Add(new AdomdParameter("Trend", "KPITrend([" + k.Name + "])"));

   // Execute query

   CellSet cellset = myKPICommand.ExecuteCellSet();

   // Get values for KPIs

   string kpiName = k.Name;

   try

      {

      string kpiValue = cellset.Cells[0].FormattedValue;

      string kpiGoal = cellset.Cells[1].FormattedValue;

      //Display the Result

      Response.Write("<td><font face=arial size=3><img src=\"images\\kpi_icon.gif\"></td>");

      Response.Write("<td align=right><font face=arial size=3>" + kpiValue + "</td>");

      Response.Write("<td align=right><font face=arial size=3>" + kpiGoal + "</td>");

      Response.Write("<td><center><img src=" + myGraphicFileInfo.GetKpiImage(k.StatusGraphic, Convert.ToDouble(cellset.Cells[2].Value)) + "></center></td>");

      //Show the description of the KPI      

      if (k.Description != null && k.Description != "")

         Response.Write("<td><center><img src=images\\info.gif title=\"" + k.Description + "\"></center></td>");

      else

      Response.Write("<td></td>");

      Response.Write("</tr>");

   }

catch (AdomdErrorResponseException myException)

   {

   }

 

4) the query return a status graphic, and a value, now you have to retreive which graphic you should display, this is the goals of the method myGraphicFileInfo.GetKpiImage(k.StatusGraphic, Convert.ToDouble(cellset.Cells[2].Value), and this is the source of this methode :

using System;

using System.Collections;

/// <summary>

/// Summary description for GraphicFileInfo

/// </summary>

public class GraphicFileInfo

{

public GraphicFileInfo()

{

   InitializeGraphicFileInfo();

}

/// <summary>

/// private class to manage the KPI Icon

/// </summary>

private class GraphicFileInformation

{

   public string FileName;

   public int LastFileNumber;

   public GraphicFileInformation(string fileName, int LastFileNumber)

{

this.FileName = fileName;

this.LastFileNumber = LastFileNumber;

}

}

 

private Hashtable graphicFiles;

/// <summary>

/// Initiatlize the hashtable that contains the icons

/// </summary>

private void InitializeGraphicFileInfo()

{

graphicFiles = new Hashtable();

graphicFiles.Add("Standard Arrow", new GraphicFileInformation("Arrow_Beveled", 4));

graphicFiles.Add("XP Arrow", new GraphicFileInformation("Arrow_XP", 4));

graphicFiles.Add("Status Arrow - Ascending", new GraphicFileInformation("Arrow_Status_Asc", 4));

graphicFiles.Add("Status Arrow - Descending", new GraphicFileInformation("Arrow_Status_Desc", 4));

graphicFiles.Add("Traffic Light - Single", new GraphicFileInformation("Stoplight_Single", 2));

graphicFiles.Add("Traffic Light - Multiple", new GraphicFileInformation("Stoplight_Multiple", 2));

graphicFiles.Add("Road Signs", new GraphicFileInformation("Road", 2));

graphicFiles.Add("Gauge - Ascending", new GraphicFileInformation("Gauge_Asc", 4));

graphicFiles.Add("Gauge - Descending", new GraphicFileInformation("Gauge_Desc", 4));

graphicFiles.Add("Thermometer", new GraphicFileInformation("Therm", 2));

graphicFiles.Add("Cylinder", new GraphicFileInformation("Cylinder", 2));

graphicFiles.Add("Smiley Face", new GraphicFileInformation("Smiley", 2));

}

 

/// <summary>

/// Return the icon to display

/// </summary>

/// <param name="graphicName">Name of the KPI Icon</param>

/// <param name="value">value of the KPI</param>

/// <returns>The Icon name to display</returns>

public string GetKpiImage(string graphicName, double value)

{

GraphicFileInformation graphicFile = (GraphicFileInformation)graphicFiles[graphicName];

int fileNumber = (int)Math.Round(graphicFile.LastFileNumber * (value + 1.0) / 2.0);

if (fileNumber < 0)

{

fileNumber = 0;

}

else if (fileNumber > graphicFile.LastFileNumber)

{

fileNumber = graphicFile.LastFileNumber;

}

string fileName = "Images/" + graphicFile.FileName + fileNumber + ".gif";

return fileName;

}

 

}

 

5 Comments
Filed under:

Hi,

I am Olivier PIERI, a Microsoft consultant specialized in database technologies.

The goals of this blog is discuss about database and Data access technologies offered by Microsoft.Net  Framwork such as ADO.Net, ADOMD.Net and SMO.

I will put some sample code about new possibilities offer by the .Net framework 2.0 and SQL Server 2005.

Have fun.

Olivier

 
Page view tracker