Sunday, January 29, 2006

Debugging Visual Studio 2005 AddIns

Recently I have been working on a Visual Studio AddIn and came across a problem.

When I first created the AddIn project, a file was created in my AddIns directory (My Documents\Visual Studio 2005\Addins) called "[addin name] - For Testing.AddIn". This is used when debugging.

I created a release version of the program and deleted this file so that I could deploy the release version. Later I made a change and wanted to debug the program only to find it no longer appeared in the visual studio tools menu. The "for testing.addin" file had not been recreated by visual studio as I had assumed it would.

Fortunately the problem was easy to resolve:
  1. Copy the "[project name].AddIn" file from your project root to the AddIns directory (My Documents\Visual Studio 2005\Addins)
  2. Rename the file to "[project name] - For Testing.AddIn".
  3. Open the file in a text editor and alter the path where it references your dll (<assembly>) to point to your debug dll.

You should now be able to debug your project and have the addin display in your tools menu.

Sunday, January 08, 2006

Nesting Partial Classes in the Solution Explorer

If you have started using the new Partial Classes feature of .net 2, you will no doubt have wondered why your partial class is not displayed nested beneath the main class.

If you create a winforms project, you will notice that the form1.designer.cs file is displayed beneath the form1.cs file, so why can't your custom partial class be displayed like this too?

Fortunately it can, but unfortunately there doesn't seem to be a way to do it through the Visual Studio 2005 IDE.

To get the class to display correctly, open up your project file in a text editor. Scroll down to the <itemgroup> node containing your class within a <compile include="[class file]"> node.

If you are looking at a winforms project, you will notice that any form.designer.cs classes contains a <dependentupon> node containing the name of the master file.

Add <dependentupon>[master class file]</dependentupon> as a subnode of the <compile> node of your partial class.

When you refresh the solution explorer, your class should appear nested beneath it.

Wednesday, January 04, 2006

Executing an Integration Service Using c#

Today I decided to create a program that would execute some SQL Server 2005 Integration Services (formerly DTS) in preparation for an upcoming project.

Unfortunately due to the product being so new, there was limited information on the web and it took me a little while to figure it out.

So that other developers don't have to hunt around like I did, I thought I would create a blog and post an example. Hopefully I will find other useful things to post here in the future.

I found this link which was written for Sql Server 2000. Unfortunately the class library has changed for 2005, but I still found it helpful as a starting point.

You can see the whole Integration Service class library defined here. Even though DTS has changed its name in Sql Server 2005, you will still find it mentioned in the class library methods.

Before starting, you will need to provide a reference to Microsoft.SQLServer.DTSRuntime.Wrapper in your project. Also add the following using declaration:


using Microsoft.SqlServer.Dts.Runtime.Wrapper;


Here is the code I used to get a list of the packages on the server:


private void GetPackageList()
{
Microsoft.SqlServer.Dts.Runtime.Wrapper.Application app;
app = new Microsoft.SqlServer.Dts.Runtime.Wrapper.Application();
//get package info object collection from the Integration Server
IDTSPackageInfos90 infos = app.GetDtsServerPackageInfos("[directory ie. 'file system']", "[your server name]");
foreach (IDTSPackageInfo90 info in infos)
{
MessageBox.Show(info.Name);
}
}


Executing a package can be done like so:


private void ExecuteCustomerImport()
{
Microsoft.SqlServer.Dts.Runtime.Wrapper.Application app;
app = new Microsoft.SqlServer.Dts.Runtime.Wrapper.Application();
IDTSPackage90 package = app.LoadFromDtsServer(@"[directory]\[package name]", "[your server name]", true, null);

package.Execute();
}



I have shown how to access a service stored in the file system here, but if you want to access Sql Server stored packages, just use the LoadFromSQLServer method instead of LoadFromDtsServer.

Obviously due to the time it can potentially take to run an Integration Service, you will need to use multi-threading to allow your program to run efficiently. There are many resources on the web showing how to do this in c#, but if you would like me to write a post on it, let me know.

I hope you found this useful. Please feel free to post any comments.