473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Creating DTS in C#

I am trying to create a DTS package using C#. I have done this in VB6
without any problems. However, when I convert everything over to C# I
get the 'System.InvalidCastException'. I am running windows 2000, SQL
2000 sp3a. I have also followed the instructions in using DTS provided
by "http://sqldev.net/DTS/dotnetcookbook.htm".

The error is occurs at this line:

DTS.DataPumpTask DT = (DTS.DataPumpTask)package.Tasks.New
("DTSDataPumpTask");

Does anyone know what causes this and is there a fix for it?

Below is a copy of the code.


public void createPackag()
{
DTS.Connection oConnection =
(DTS.Connection)package.Connections.New("Microsoft .Jet.OLEDB.4.0");
oConnection.Name ="Connection 1";
oConnection.DataSource = "C:\\MySourceDB.MDB";
oConnection.ID = 1;
oConnection.Reusable = true;
oConnection.ConnectImmediate = false;
oConnection.ConnectionTimeout = 60;
oConnection.UseTrustedConnection = false;
oConnection.UseDSL = false;
oConnection = null;
DTS.Connection oConnection2 =
(DTS.Connection)package.Connections.New("SQLOLEDB" );
oConnection2.Name = "Connection 2";
oConnection2.ID = 2;
oConnection2.Reusable = true;
oConnection2.ConnectImmediate= false;
oConnection2.DataSource= "MyServerName";
oConnection2.UserID = "MyUserID";
oConnection2.ConnectionTimeout = 60;
oConnection2.Catalog = "MyDestDB";
oConnection2.UseTrustedConnection = false;
oConnection2.UseDSL = false;
oConnection2.Password = "MyPassword";
oConnection2 = null;
DTS.Step2 oStep = (DTS.Step2)package.Steps.New();
oStep.Name = "Copying Data from MyTable";
oStep.Description = "Copying Data from MyTable";
oStep.TaskName = "Copying Data from MyTable";
oStep.CommitSuccess = false;
oStep.RollbackFailure = false;
oStep.ScriptLanguage = "VBScript";//not sure about this
oStep.AddGlobalVariables = true;
oStep.CloseConnection = false;
oStep.ExecuteInMainThread = true;
oStep.IsPackageDSORowset = false;
oStep.JoinTransactionIfPresent = false;
oStep.DisableStep = false;
oStep.FailPackageOnError = true;
package.Steps.Add(oStep);
oStep = null;
DTS.Task oTask = (DTS.Task)package.Tasks.New("DTSDataPumpTask");
oTask.Name = "Copying Data from MyTable";
DTS.CustomTask oCustomTask = oTask.CustomTask;
oCustomTask.Name = "Copying Data from MyTable";
oCustomTask.Description = "Copying Data from MyTable to
MyDestDB.MyTable";
DTS.DataPumpTask DT =
(DTS.DataPumpTask)package.Tasks.New("DTSDataPumpTa sk");
DT.SourceConnectionID = 1;
DT.SourceSQLStatement = "SELECT `TestField` FROM MyTable";
DT.DestinationConnectionID =2;
DT.DestinationObjectName = "MyTable";
DT.ProgressRowCount = 1000;
DT.MaximumErrorCount = 0;
DT.FetchBufferSize = 1;
DT.UseFastLoad=true;
DT.InsertCommitSize = 0;
DT.InsertCommitSize = 500000;
DT.ExceptionFileColumnDelimiter = "|";
DT.ExceptionFileRowDelimiter = "\r\n";
DT.AllowIdentityInserts = false;
DT.FirstRow = 0;
DT.LastRow = 0;
DTS.Transformation Trans =
(DTS.Transformation)package.Tasks.New("DataPumpTra nsformCopy");
Trans.Name = "DirectCopyXform";
Trans.TransformFlags = 63;
Trans.ForceSourceBlobsBuffered = 0;
Trans.ForceBlobsInMemory = false;
Trans.InMemoryBlobSize = 1048576;
Trans.SourceColumns.AddColumn("TestField",1);
Trans.DestinationColumns.AddColumn("TestField",1);
DT.Transformations.Add(Trans);
package.Tasks.Add(oTask);
oCustomTask = null;
oTask = null;

}

Nov 17 '05 #1
2 6508
Hope this helps you...

http://www.15seconds.com/issue/030909.htm
http://www.c-sharpcorner.com/Code/2002/Sept/DTSnCS.asp
http://www.c-sharpcorner.com/Code/20...TSCustTask.asp
Regards,

--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
http://www.consein.com

<Mi***************@TheNetResultsGroup.com> escribió en el mensaje
news:11**********************@f14g2000cwb.googlegr oups.com...
I am trying to create a DTS package using C#. I have done this in VB6
without any problems. However, when I convert everything over to C# I
get the 'System.InvalidCastException'. I am running windows 2000, SQL
2000 sp3a. I have also followed the instructions in using DTS provided
by "http://sqldev.net/DTS/dotnetcookbook.htm".

The error is occurs at this line:

DTS.DataPumpTask DT = (DTS.DataPumpTask)package.Tasks.New
("DTSDataPumpTask");

Does anyone know what causes this and is there a fix for it?

Below is a copy of the code.


public void createPackag()
{
DTS.Connection oConnection =
(DTS.Connection)package.Connections.New("Microsoft .Jet.OLEDB.4.0");
oConnection.Name ="Connection 1";
oConnection.DataSource = "C:\\MySourceDB.MDB";
oConnection.ID = 1;
oConnection.Reusable = true;
oConnection.ConnectImmediate = false;
oConnection.ConnectionTimeout = 60;
oConnection.UseTrustedConnection = false;
oConnection.UseDSL = false;
oConnection = null;
DTS.Connection oConnection2 =
(DTS.Connection)package.Connections.New("SQLOLEDB" );
oConnection2.Name = "Connection 2";
oConnection2.ID = 2;
oConnection2.Reusable = true;
oConnection2.ConnectImmediate= false;
oConnection2.DataSource= "MyServerName";
oConnection2.UserID = "MyUserID";
oConnection2.ConnectionTimeout = 60;
oConnection2.Catalog = "MyDestDB";
oConnection2.UseTrustedConnection = false;
oConnection2.UseDSL = false;
oConnection2.Password = "MyPassword";
oConnection2 = null;
DTS.Step2 oStep = (DTS.Step2)package.Steps.New();
oStep.Name = "Copying Data from MyTable";
oStep.Description = "Copying Data from MyTable";
oStep.TaskName = "Copying Data from MyTable";
oStep.CommitSuccess = false;
oStep.RollbackFailure = false;
oStep.ScriptLanguage = "VBScript";//not sure about this
oStep.AddGlobalVariables = true;
oStep.CloseConnection = false;
oStep.ExecuteInMainThread = true;
oStep.IsPackageDSORowset = false;
oStep.JoinTransactionIfPresent = false;
oStep.DisableStep = false;
oStep.FailPackageOnError = true;
package.Steps.Add(oStep);
oStep = null;
DTS.Task oTask = (DTS.Task)package.Tasks.New("DTSDataPumpTask");
oTask.Name = "Copying Data from MyTable";
DTS.CustomTask oCustomTask = oTask.CustomTask;
oCustomTask.Name = "Copying Data from MyTable";
oCustomTask.Description = "Copying Data from MyTable to
MyDestDB.MyTable";
DTS.DataPumpTask DT =
(DTS.DataPumpTask)package.Tasks.New("DTSDataPumpTa sk");
DT.SourceConnectionID = 1;
DT.SourceSQLStatement = "SELECT `TestField` FROM MyTable";
DT.DestinationConnectionID =2;
DT.DestinationObjectName = "MyTable";
DT.ProgressRowCount = 1000;
DT.MaximumErrorCount = 0;
DT.FetchBufferSize = 1;
DT.UseFastLoad=true;
DT.InsertCommitSize = 0;
DT.InsertCommitSize = 500000;
DT.ExceptionFileColumnDelimiter = "|";
DT.ExceptionFileRowDelimiter = "\r\n";
DT.AllowIdentityInserts = false;
DT.FirstRow = 0;
DT.LastRow = 0;
DTS.Transformation Trans =
(DTS.Transformation)package.Tasks.New("DataPumpTra nsformCopy");
Trans.Name = "DirectCopyXform";
Trans.TransformFlags = 63;
Trans.ForceSourceBlobsBuffered = 0;
Trans.ForceBlobsInMemory = false;
Trans.InMemoryBlobSize = 1048576;
Trans.SourceColumns.AddColumn("TestField",1);
Trans.DestinationColumns.AddColumn("TestField",1);
DT.Transformations.Add(Trans);
package.Tasks.Add(oTask);
oCustomTask = null;
oTask = null;

}

Nov 17 '05 #2


I was able to resolve this issue and thought I would post the solution
so that others may benefit from it. The following example creates,
saves, and executes a DTS package in C#. This example is setup to only
DTS one field from one table but it can easily be modified to do a
complete database.
This particular section of code gave me the most trouble so I thought I
would point it out as well. My original code looked like this and
would cause an invalidcastexception.

DTS.Task oTask = (DTS.Task)package.Tasks.New("DTSDataPumpTask");
DTS.CustomTask oCustomTask = oTask.CustomTask;

This was how I corrected it.

DTS.DataPumpTask2 oCustTask;
DTS.Task oTask;
oTask = pkg.Tasks.New("DTSDataPumpTask");
oCustTask = (DTS.DataPumpTask2)oTask.CustomTask;

Here is the entire example:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using DTS = Microsoft.SQLServer.DTSPkg80;

namespace MyDTSTest
{
/// <summary>
/// Summary description for Form1.
///This is assuming that all steps have been taken in the following
document:
///http://SQLDEV.NET/DTS/DotNetCookBook.htm
///SN.EXE -K c:\DTS.KEY
///tlbimp.exe "C:\program files\microsoft SQL
Sever\80\Tools\Bin\dtspkg.dll" /out:c:\Microsoft.SQLServer.DTSPkg80.dll
/Keyfile:c:\DTS.KEY
///gacutil.exe -i C:\Microsoft.SQLServer.DTSPkg80.dll
///These steps are needed for interop with dtspkg.dll
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
public DTS.Package2Class pkg = new DTS.Package2Class();

private void button1_Click(object sender, System.EventArgs e)
{
initpackage();
}

public void initpackage()
{
CreateConnections();
CreatePackageSteps();
DefinTasks(pkg);
pkg.Name="MyCSharpDTSTest";
pkg.Description = "CShart DTS Test";
object MIA=System.Reflection.Missing.Value;
pkg.SaveToSQLServer("MyServerName", "MyUserID", "MyPassword",
DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default , "","","",ref
MIA,false);
pkg.Execute();
pkg.UnInitialize();
pkg = null;
}

public void CreateConnections()
{

DTS.Connection oConnection =
(DTS.Connection)pkg.Connections.New("Microsoft.Jet .OLEDB.4.0");
oConnection.Name ="Connection 1";
oConnection.DataSource = "C:\\MySoureDB.MDB";
oConnection.ID = 1;
oConnection.Reusable = true;
oConnection.ConnectImmediate = false;
oConnection.ConnectionTimeout = 60;
oConnection.UseTrustedConnection = false;
oConnection.UseDSL = false;
pkg.Connections.Add(oConnection);
oConnection = null;
DTS.Connection oConnection2 =
(DTS.Connection)pkg.Connections.New("SQLOLEDB");
oConnection2.Name = "Connection 2";
oConnection2.ID = 2;
oConnection2.Reusable = true;
oConnection2.ConnectImmediate= false;
oConnection2.DataSource= "MyServerName";
oConnection2.UserID = "MyUserID";
oConnection2.ConnectionTimeout = 60;
oConnection2.Catalog = "MyDestDB";
oConnection2.UseTrustedConnection = false;
oConnection2.UseDSL = false;
oConnection2.Password = "MyPassword";
pkg.Connections.Add(oConnection2);
oConnection2 = null;
}

public void CreatePackageSteps()
{
DTS.Step2 oStep = (DTS.Step2)pkg.Steps.New();
oStep.Name = "Copying Data from myTableName";
oStep.Description = "Copying Data from myTableName";
oStep.TaskName = "Copying Data from myTableName";
oStep.CommitSuccess = false;
oStep.RollbackFailure = false;
oStep.ScriptLanguage = "VBScript";
oStep.AddGlobalVariables = true;
oStep.CloseConnection = false;
oStep.ExecuteInMainThread = true;
oStep.IsPackageDSORowset = false;
oStep.JoinTransactionIfPresent = false;
oStep.DisableStep = false;
oStep.FailPackageOnError = true;
pkg.Steps.Add(oStep);
oStep = null;

}

public void DefinTasks(DTS.Package2Class package)
{
DTS.DataPumpTask2 oCustTask;
DTS.Task oTask;
oTask = pkg.Tasks.New("DTSDataPumpTask");
oCustTask = (DTS.DataPumpTask2)oTask.CustomTask;
oCustTask.Name = "Copying Data from myTableName";
oCustTask.Description = "Copying Data from myTableName to
MyDestDB.myTableName";
DTS.DataPumpTask2 oDataPump = (DTS.DataPumpTask2)oTask.CustomTask;
oDataPump.SourceConnectionID = 1;
oDataPump.SourceSQLStatement = "SELECT `MyField` FROM myTableName";
oDataPump.DestinationConnectionID =2;
oDataPump.DestinationObjectName = "myTableName";
oDataPump.ProgressRowCount = 1000;
oDataPump.MaximumErrorCount = 0;
oDataPump.FetchBufferSize = 1;
oDataPump.UseFastLoad=true;
oDataPump.InsertCommitSize = 0;
oDataPump.InsertCommitSize = 500000;
oDataPump.ExceptionFileColumnDelimiter = "|";
oDataPump.ExceptionFileRowDelimiter = "\n\r";
oDataPump.AllowIdentityInserts = false;
oDataPump.FirstRow = 0;
oDataPump.LastRow = 0;
CreateTaskTrans(oDataPump, oCustTask);
pkg.Tasks.Add(oTask);
oCustTask = null;
oTask = null;

}
public void CreateTaskTrans(DTS.DataPumpTask DatPump,
DTS.DataPumpTask2 CustTask)
{
DTS.Transformation2 oTransformation;
oTransformation =
(DTS.Transformation2)CustTask.Transformations.New( "DTS.DataPumpTransformCopy");
oTransformation.Name = "DirectCopyXform";
oTransformation.TransformFlags = 63;
oTransformation.ForceSourceBlobsBuffered = 0;
oTransformation.ForceBlobsInMemory = false;
oTransformation.InMemoryBlobSize = 1048576;
oTransformation.SourceColumns.AddColumn("MyField", 1);
oTransformation.DestinationColumns.AddColumn("MyFi eld",1);
DatPump.Transformations.Add(oTransformation);

}
}
}

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rdsteph | last post by:
Python411 is a series of podcasts about Python, aimed at hobbyists and others who are learning Python. Each episode focuses on one aspect of learning Python, or one kind of Python programming, and...
6
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at...
2
by: Pawan | last post by:
Hi Guys, I have this current assignment where I have to develop online forms for local municipal authorities. I have to use adobe acrobat to create online forms from PDFs (which I have never done...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
2
by: LIN | last post by:
Hello, Greetings. I am creating a web site which will contain lot of articles. I had been planning to create simple HTML page on the server everytime i posted a article (eg. article12.html )....
2
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is...
0
by: Ravi Ambros Wallau | last post by:
Hi: I've created a custom control - a grid that uses Infragistics to display some filters, the grid itself, and some buttons. Well, when using this control directly on WebForm, everything works...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
15
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. ...
9
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.