473,806 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Invalid CastException'. 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.DataPumpTas k DT = (DTS.DataPumpTa sk)package.Task s.New
("DTSDataPumpTa sk");

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.Connec tions.New("Micr osoft.Jet.OLEDB .4.0");
oConnection.Nam e ="Connection 1";
oConnection.Dat aSource = "C:\\MySourceDB .MDB";
oConnection.ID = 1;
oConnection.Reu sable = true;
oConnection.Con nectImmediate = false;
oConnection.Con nectionTimeout = 60;
oConnection.Use TrustedConnecti on = false;
oConnection.Use DSL = false;
oConnection = null;
DTS.Connection oConnection2 =
(DTS.Connection )package.Connec tions.New("SQLO LEDB");
oConnection2.Na me = "Connection 2";
oConnection2.ID = 2;
oConnection2.Re usable = true;
oConnection2.Co nnectImmediate= false;
oConnection2.Da taSource= "MyServerNa me";
oConnection2.Us erID = "MyUserID";
oConnection2.Co nnectionTimeout = 60;
oConnection2.Ca talog = "MyDestDB";
oConnection2.Us eTrustedConnect ion = false;
oConnection2.Us eDSL = false;
oConnection2.Pa ssword = "MyPassword ";
oConnection2 = null;
DTS.Step2 oStep = (DTS.Step2)pack age.Steps.New() ;
oStep.Name = "Copying Data from MyTable";
oStep.Descripti on = "Copying Data from MyTable";
oStep.TaskName = "Copying Data from MyTable";
oStep.CommitSuc cess = false;
oStep.RollbackF ailure = false;
oStep.ScriptLan guage = "VBScript";//not sure about this
oStep.AddGlobal Variables = true;
oStep.CloseConn ection = false;
oStep.ExecuteIn MainThread = true;
oStep.IsPackage DSORowset = false;
oStep.JoinTrans actionIfPresent = false;
oStep.DisableSt ep = false;
oStep.FailPacka geOnError = true;
package.Steps.A dd(oStep);
oStep = null;
DTS.Task oTask = (DTS.Task)packa ge.Tasks.New("D TSDataPumpTask" );
oTask.Name = "Copying Data from MyTable";
DTS.CustomTask oCustomTask = oTask.CustomTas k;
oCustomTask.Nam e = "Copying Data from MyTable";
oCustomTask.Des cription = "Copying Data from MyTable to
MyDestDB.MyTabl e";
DTS.DataPumpTas k DT =
(DTS.DataPumpTa sk)package.Task s.New("DTSDataP umpTask");
DT.SourceConnec tionID = 1;
DT.SourceSQLSta tement = "SELECT `TestField` FROM MyTable";
DT.DestinationC onnectionID =2;
DT.DestinationO bjectName = "MyTable";
DT.ProgressRowC ount = 1000;
DT.MaximumError Count = 0;
DT.FetchBufferS ize = 1;
DT.UseFastLoad= true;
DT.InsertCommit Size = 0;
DT.InsertCommit Size = 500000;
DT.ExceptionFil eColumnDelimite r = "|";
DT.ExceptionFil eRowDelimiter = "\r\n";
DT.AllowIdentit yInserts = false;
DT.FirstRow = 0;
DT.LastRow = 0;
DTS.Transformat ion Trans =
(DTS.Transforma tion)package.Ta sks.New("DataPu mpTransformCopy ");
Trans.Name = "DirectCopyXfor m";
Trans.Transform Flags = 63;
Trans.ForceSour ceBlobsBuffered = 0;
Trans.ForceBlob sInMemory = false;
Trans.InMemoryB lobSize = 1048576;
Trans.SourceCol umns.AddColumn( "TestField" ,1);
Trans.Destinati onColumns.AddCo lumn("TestField ",1);
DT.Transformati ons.Add(Trans);
package.Tasks.A dd(oTask);
oCustomTask = null;
oTask = null;

}

Nov 17 '05 #1
2 6530
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************ ***@TheNetResul tsGroup.com> escribió en el mensaje
news:11******** **************@ f14g2000cwb.goo glegroups.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.Invalid CastException'. 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.DataPumpTas k DT = (DTS.DataPumpTa sk)package.Task s.New
("DTSDataPumpTa sk");

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.Connec tions.New("Micr osoft.Jet.OLEDB .4.0");
oConnection.Nam e ="Connection 1";
oConnection.Dat aSource = "C:\\MySourceDB .MDB";
oConnection.ID = 1;
oConnection.Reu sable = true;
oConnection.Con nectImmediate = false;
oConnection.Con nectionTimeout = 60;
oConnection.Use TrustedConnecti on = false;
oConnection.Use DSL = false;
oConnection = null;
DTS.Connection oConnection2 =
(DTS.Connection )package.Connec tions.New("SQLO LEDB");
oConnection2.Na me = "Connection 2";
oConnection2.ID = 2;
oConnection2.Re usable = true;
oConnection2.Co nnectImmediate= false;
oConnection2.Da taSource= "MyServerNa me";
oConnection2.Us erID = "MyUserID";
oConnection2.Co nnectionTimeout = 60;
oConnection2.Ca talog = "MyDestDB";
oConnection2.Us eTrustedConnect ion = false;
oConnection2.Us eDSL = false;
oConnection2.Pa ssword = "MyPassword ";
oConnection2 = null;
DTS.Step2 oStep = (DTS.Step2)pack age.Steps.New() ;
oStep.Name = "Copying Data from MyTable";
oStep.Descripti on = "Copying Data from MyTable";
oStep.TaskName = "Copying Data from MyTable";
oStep.CommitSuc cess = false;
oStep.RollbackF ailure = false;
oStep.ScriptLan guage = "VBScript";//not sure about this
oStep.AddGlobal Variables = true;
oStep.CloseConn ection = false;
oStep.ExecuteIn MainThread = true;
oStep.IsPackage DSORowset = false;
oStep.JoinTrans actionIfPresent = false;
oStep.DisableSt ep = false;
oStep.FailPacka geOnError = true;
package.Steps.A dd(oStep);
oStep = null;
DTS.Task oTask = (DTS.Task)packa ge.Tasks.New("D TSDataPumpTask" );
oTask.Name = "Copying Data from MyTable";
DTS.CustomTask oCustomTask = oTask.CustomTas k;
oCustomTask.Nam e = "Copying Data from MyTable";
oCustomTask.Des cription = "Copying Data from MyTable to
MyDestDB.MyTabl e";
DTS.DataPumpTas k DT =
(DTS.DataPumpTa sk)package.Task s.New("DTSDataP umpTask");
DT.SourceConnec tionID = 1;
DT.SourceSQLSta tement = "SELECT `TestField` FROM MyTable";
DT.DestinationC onnectionID =2;
DT.DestinationO bjectName = "MyTable";
DT.ProgressRowC ount = 1000;
DT.MaximumError Count = 0;
DT.FetchBufferS ize = 1;
DT.UseFastLoad= true;
DT.InsertCommit Size = 0;
DT.InsertCommit Size = 500000;
DT.ExceptionFil eColumnDelimite r = "|";
DT.ExceptionFil eRowDelimiter = "\r\n";
DT.AllowIdentit yInserts = false;
DT.FirstRow = 0;
DT.LastRow = 0;
DTS.Transformat ion Trans =
(DTS.Transforma tion)package.Ta sks.New("DataPu mpTransformCopy ");
Trans.Name = "DirectCopyXfor m";
Trans.Transform Flags = 63;
Trans.ForceSour ceBlobsBuffered = 0;
Trans.ForceBlob sInMemory = false;
Trans.InMemoryB lobSize = 1048576;
Trans.SourceCol umns.AddColumn( "TestField" ,1);
Trans.Destinati onColumns.AddCo lumn("TestField ",1);
DT.Transformati ons.Add(Trans);
package.Tasks.A dd(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 invalidcastexce ption.

DTS.Task oTask = (DTS.Task)packa ge.Tasks.New("D TSDataPumpTask" );
DTS.CustomTask oCustomTask = oTask.CustomTas k;

This was how I corrected it.

DTS.DataPumpTas k2 oCustTask;
DTS.Task oTask;
oTask = pkg.Tasks.New(" DTSDataPumpTask ");
oCustTask = (DTS.DataPumpTa sk2)oTask.Custo mTask;

Here is the entire example:
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using DTS = Microsoft.SQLSe rver.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:\Microsof t.SQLServer.DTS Pkg80.dll
/Keyfile:c:\DTS. KEY
///gacutil.exe -i C:\Microsoft.SQ LServer.DTSPkg8 0.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.Componen tModel.Containe r components = null;
private System.Windows. Forms.Button button1;
public DTS.Package2Cla ss pkg = new DTS.Package2Cla ss();

private void button1_Click(o bject sender, System.EventArg s e)
{
initpackage();
}

public void initpackage()
{
CreateConnectio ns();
CreatePackageSt eps();
DefinTasks(pkg) ;
pkg.Name="MyCSh arpDTSTest";
pkg.Description = "CShart DTS Test";
object MIA=System.Refl ection.Missing. Value;
pkg.SaveToSQLSe rver("MyServerN ame", "MyUserID", "MyPassword ",
DTS.DTSSQLServe rStorageFlags.D TSSQLStgFlag_De fault, "","","",re f
MIA,false);
pkg.Execute();
pkg.UnInitializ e();
pkg = null;
}

public void CreateConnectio ns()
{

DTS.Connection oConnection =
(DTS.Connection )pkg.Connection s.New("Microsof t.Jet.OLEDB.4.0 ");
oConnection.Nam e ="Connection 1";
oConnection.Dat aSource = "C:\\MySoureDB. MDB";
oConnection.ID = 1;
oConnection.Reu sable = true;
oConnection.Con nectImmediate = false;
oConnection.Con nectionTimeout = 60;
oConnection.Use TrustedConnecti on = false;
oConnection.Use DSL = false;
pkg.Connections .Add(oConnectio n);
oConnection = null;
DTS.Connection oConnection2 =
(DTS.Connection )pkg.Connection s.New("SQLOLEDB ");
oConnection2.Na me = "Connection 2";
oConnection2.ID = 2;
oConnection2.Re usable = true;
oConnection2.Co nnectImmediate= false;
oConnection2.Da taSource= "MyServerNa me";
oConnection2.Us erID = "MyUserID";
oConnection2.Co nnectionTimeout = 60;
oConnection2.Ca talog = "MyDestDB";
oConnection2.Us eTrustedConnect ion = false;
oConnection2.Us eDSL = false;
oConnection2.Pa ssword = "MyPassword ";
pkg.Connections .Add(oConnectio n2);
oConnection2 = null;
}

public void CreatePackageSt eps()
{
DTS.Step2 oStep = (DTS.Step2)pkg. Steps.New();
oStep.Name = "Copying Data from myTableName";
oStep.Descripti on = "Copying Data from myTableName";
oStep.TaskName = "Copying Data from myTableName";
oStep.CommitSuc cess = false;
oStep.RollbackF ailure = false;
oStep.ScriptLan guage = "VBScript";
oStep.AddGlobal Variables = true;
oStep.CloseConn ection = false;
oStep.ExecuteIn MainThread = true;
oStep.IsPackage DSORowset = false;
oStep.JoinTrans actionIfPresent = false;
oStep.DisableSt ep = false;
oStep.FailPacka geOnError = true;
pkg.Steps.Add(o Step);
oStep = null;

}

public void DefinTasks(DTS. Package2Class package)
{
DTS.DataPumpTas k2 oCustTask;
DTS.Task oTask;
oTask = pkg.Tasks.New(" DTSDataPumpTask ");
oCustTask = (DTS.DataPumpTa sk2)oTask.Custo mTask;
oCustTask.Name = "Copying Data from myTableName";
oCustTask.Descr iption = "Copying Data from myTableName to
MyDestDB.myTabl eName";
DTS.DataPumpTas k2 oDataPump = (DTS.DataPumpTa sk2)oTask.Custo mTask;
oDataPump.Sourc eConnectionID = 1;
oDataPump.Sourc eSQLStatement = "SELECT `MyField` FROM myTableName";
oDataPump.Desti nationConnectio nID =2;
oDataPump.Desti nationObjectNam e = "myTableNam e";
oDataPump.Progr essRowCount = 1000;
oDataPump.Maxim umErrorCount = 0;
oDataPump.Fetch BufferSize = 1;
oDataPump.UseFa stLoad=true;
oDataPump.Inser tCommitSize = 0;
oDataPump.Inser tCommitSize = 500000;
oDataPump.Excep tionFileColumnD elimiter = "|";
oDataPump.Excep tionFileRowDeli miter = "\n\r";
oDataPump.Allow IdentityInserts = false;
oDataPump.First Row = 0;
oDataPump.LastR ow = 0;
CreateTaskTrans (oDataPump, oCustTask);
pkg.Tasks.Add(o Task);
oCustTask = null;
oTask = null;

}
public void CreateTaskTrans (DTS.DataPumpTa sk DatPump,
DTS.DataPumpTas k2 CustTask)
{
DTS.Transformat ion2 oTransformation ;
oTransformation =
(DTS.Transforma tion2)CustTask. Transformations .New("DTS.DataP umpTransformCop y");
oTransformation .Name = "DirectCopyXfor m";
oTransformation .TransformFlags = 63;
oTransformation .ForceSourceBlo bsBuffered = 0;
oTransformation .ForceBlobsInMe mory = false;
oTransformation .InMemoryBlobSi ze = 1048576;
oTransformation .SourceColumns. AddColumn("MyFi eld",1);
oTransformation .DestinationCol umns.AddColumn( "MyField",1 );
DatPump.Transfo rmations.Add(oT ransformation);

}
}
}

Nov 17 '05 #3

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

Similar topics

2
2726
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 points to online tools and tutorials. Python related news and events will also be reported upon. This podcast series will probably not be of much interest to expert or professional programmers. I am a hobbyist and am not qualified nor capable...
6
6128
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 runtime). Does it indicate a problem with References for example? Thanks Owen PS. replies cc'd by email appreciated. owen.southwood@mantix.com
2
2160
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 before and have no idea how to do it). Now these online forms will be shared by 1200 users. VB and ASP will be used as glueware for the writing/coding. For eg, we will be creating navigation pages in ASP & linking into adobe. Any resource on this...
15
6753
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 javascript or vbscript it works. I will appreciate any help. I do the following (C#):
2
3781
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 ). Now, i want to show an advertisement on top of each of this html page. the ad banner has to be dynamically updated depending on date (period) the advertiser has planned to advertise for. So I thought of creating ASPx pages instead. Is it a good...
2
4574
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 easy, but 1) How do I set about dynamically creating user controls (like TextBox, TextArea) --- simply Declare and initialised (new) the user controls?? How do I "place" it graphically on the form. Ideally, I want them to lay out in a table, one...
0
2137
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 really fine. No problems at all. But, when this control is placed inside a template of an Infragistics UltraWebTab control, somethings stranges happens: 1. When the WebForm is opened, an "Error Creating Control" message is displayed (for container...
12
3180
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 restarting the application. What I did was to create an AppDomain that loaded the plugins and everything was great, until I tried to pass something else that strings between the domains...
15
2838
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. Everything is the default settings I believe. IIS is running under Local System. In IIS the DefaultAppPool is running under Network Service. Annonymous access uses the account IUSR_JASMINE (machine name is Jasmine).
9
2987
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
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10371
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.