用c#的,在微软的网站上找到的:
http://download.microsoft.com/download/1/9/8/198f94e8-a101-4132-a309-ff1c297193a5/WCD111802.exe 是一个pptusing System;
using Microsoft.SQLServer.DTSPkg80;
namespace DTSSample
{
class App
{
static void Main()
{
Package2Class package = new Package2Class();
object pVarPersistStgOfHost = null;
package.LoadFromSQLServer("YOUR_SERVER_NAME", null, null, DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, null,
null, null, "YOUR_PACKAGE_NAME", ref pVarPersistStgOfHost); package.Execute();
package.UnInitialize();
package = null; }
}
}
用VB的,在SQL Server的联机丛书(帮助)中找到的The following code example shows a DTS package using an ExecutePackageTask, through the Execute and UnInitialize methods:Private WithEvents mobjPkgEvents As DTS.Package
. . .
Private Sub RunPackage()
'Run the package stored in file C:\DTS_UE\TestPkg\VarPubsFields.dts.
Dim objPackage As DTS.Package2
Dim objStep As DTS.Step
Dim objTask As DTS.Task
Dim objExecPkg As DTS.ExecutePackageTaskOn Error GoTo PackageError
Set objPackage = New DTS.Package
Set mobjPkgEvents = objPackage
objPackage.FailOnError = True'Create the step and task. Specify the package to be run, and link the step to the task.
Set objStep = objPackage.Steps.New
Set objTask = objPackage.Tasks.New("DTSExecutePackageTask")
Set objExecPkg = objTask.CustomTask
With objExecPkg
.PackagePassword = "user"
.FileName = "C:\DTS_UE\TestPkg\VarPubsFields.dts"
.Name = "ExecPkgTask"
End With
With objStep
.TaskName = objExecPkg.Name
.Name = "ExecPkgStep"
.ExecuteInMainThread = True
End With
objPackage.Steps.Add objStep
objPackage.Tasks.Add objTask'Run the package and release references.
objPackage.ExecuteSet objExecPkg = Nothing
Set objTask = Nothing
Set objStep = Nothing
Set mobjPkgEvents = NothingobjPackage.UnInitialize
End Sub
我也在搞这个问题,按照上述的办法都试过了,不知道哪里出了问题,还没弄出来。
http://download.microsoft.com/download/1/9/8/198f94e8-a101-4132-a309-ff1c297193a5/WCD111802.exe 是一个pptusing System;
using Microsoft.SQLServer.DTSPkg80;
namespace DTSSample
{
class App
{
static void Main()
{
Package2Class package = new Package2Class();
object pVarPersistStgOfHost = null;
package.LoadFromSQLServer("YOUR_SERVER_NAME", null, null, DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, null,
null, null, "YOUR_PACKAGE_NAME", ref pVarPersistStgOfHost); package.Execute();
package.UnInitialize();
package = null; }
}
}
用VB的,在SQL Server的联机丛书(帮助)中找到的The following code example shows a DTS package using an ExecutePackageTask, through the Execute and UnInitialize methods:Private WithEvents mobjPkgEvents As DTS.Package
. . .
Private Sub RunPackage()
'Run the package stored in file C:\DTS_UE\TestPkg\VarPubsFields.dts.
Dim objPackage As DTS.Package2
Dim objStep As DTS.Step
Dim objTask As DTS.Task
Dim objExecPkg As DTS.ExecutePackageTaskOn Error GoTo PackageError
Set objPackage = New DTS.Package
Set mobjPkgEvents = objPackage
objPackage.FailOnError = True'Create the step and task. Specify the package to be run, and link the step to the task.
Set objStep = objPackage.Steps.New
Set objTask = objPackage.Tasks.New("DTSExecutePackageTask")
Set objExecPkg = objTask.CustomTask
With objExecPkg
.PackagePassword = "user"
.FileName = "C:\DTS_UE\TestPkg\VarPubsFields.dts"
.Name = "ExecPkgTask"
End With
With objStep
.TaskName = objExecPkg.Name
.Name = "ExecPkgStep"
.ExecuteInMainThread = True
End With
objPackage.Steps.Add objStep
objPackage.Tasks.Add objTask'Run the package and release references.
objPackage.ExecuteSet objExecPkg = Nothing
Set objTask = Nothing
Set objStep = Nothing
Set mobjPkgEvents = NothingobjPackage.UnInitialize
End Sub
我也在搞这个问题,按照上述的办法都试过了,不知道哪里出了问题,还没弄出来。
/// 调用DTS包,执行DTS包,导出CSV格式的文件
/// </summary>
/// <param name="strPackageName">要调用的DTS包的名称</param>
/// <param name="strOutputFileName">CSV格式文件的文件名,比如"abc.csv"</param>
private void ExecuteDTSPackage(string strPackageName, string strOutputFileName)
{
DTS.Package2Class package; string strSqlName = ConfigurationSettings.AppSettings["sqlServerName"];
string strCatalog = ConfigurationSettings.AppSettings["sqlCatalog"];
string strUserId = ConfigurationSettings.AppSettings["sqlUserId"];
string strPassword = ConfigurationSettings.AppSettings["sqlPassword"];
string strFilePath = ConfigurationSettings.AppSettings["csvFilePath"]; try
{
package = new Package2Class();
object pVarPersistStgOfHost = null;
package.LoadFromSQLServer(strSqlName, strUserId, strPassword, DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, null,
null, null, strPackageName, ref pVarPersistStgOfHost); foreach(GlobalVariable global in package.GlobalVariables)
{
if(global.Name == "sqlServerName")
package.GlobalVariables.Remove(global.Name);
if(global.Name == "sqlCatalog")
package.GlobalVariables.Remove(global.Name);
if(global.Name == "sqlUserId")
package.GlobalVariables.Remove(global.Name);
if(global.Name == "sqlPassword")
package.GlobalVariables.Remove(global.Name);
if(global.Name == "csvFilePath")
package.GlobalVariables.Remove(global.Name);
} package.GlobalVariables.AddGlobalVariable("sqlServerName",strSqlName);
package.GlobalVariables.AddGlobalVariable("sqlCatalog",strCatalog);
package.GlobalVariables.AddGlobalVariable("sqlUserId",strUserId);
package.GlobalVariables.AddGlobalVariable("sqlPassword",strPassword);
package.GlobalVariables.AddGlobalVariable("csvFilePath",strFilePath + strOutputFileName); package.Execute();
package.UnInitialize();
package = null;
}
catch
{
this.PopUpWindow("数据导出失败!");
}
}后面的一些代码是给DTS包传全局变量用的,DTS包里面加了一个ActiveX脚本任务,用来给数据库连接设置参数,参数来自全局变量。