C # directly call the SSIS package to import SQL Server Data

Source: Internet
Author: User
Tags ssis

Many people have discussed the data import function of SQL Server by calling the SSIS package on the Internet. After your reference, you have also implemented this function. This function was also used in the previous project. After thinking about it, I decided to paste it to enhance my memory.
1. to directly call the SSIS package, you must reference Microsoft. sqlserver. dtsruntimewrap. dll. The client must install the client component of ms SQL server2005.
2. CallCode

Code

///   <Summary>
/// /Import data to the database through the SSIS package
///   </Summary>
///   <Param name = "dtsxfilename"> Dtsx file name </Param>
///   <Param name = "csvfilename"> CSV file name </Param>
///   <Returns> </returns>
Protected   Bool Filldata2db ( String Dtsxfilename, String Csvfilename)
{
String Dtsxfile = System. appdomain. currentdomain. basedirectory +   " Dtsx \\ "   + Dtsxfilename;

Microsoft. sqlserver. DTS. runtime. wrapper. Application app= NewMicrosoft. sqlserver. DTS. runtime. wrapper. Application ();

// Call the dtsx File
Idtspackage90 package = App. loadpackage (dtsxfile, True , Null ); // Another method is loadfromsqlserver.

Package. Connections [ " File " ]. Connectionstring = Path. Combine (system. appdomain. currentdomain. basedirectory, csvfilename );
Package. Connections [ " Local Connection " ]. Connectionstring = Configurationsettings. deleettings [ " Connstr " ];

Dtsexecresult result = Package. Execute ();
// Get package execution Information
// String message = string. empty;
// If (result. Equals (dtsexecresult. dtser_failure ))
// {
// For (INT I = 0; I <package. errors. Count; I ++)
// {
// Message + = package. errors [I]. description;
// }
// }
// If (! String. isnullorempty (Message ))
// {
// Throw new exception (Message ); // Throw an exception
// }
If (Result. Equals (dtsexecresult. dtser_success ))
Return   True ;
Else
Return   False ;

}

The required configuration section is as follows:

Code

<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< Appsettings >
< Add Key = "Connstr" Value = "Data Source = 10. 10. **. * **; user id = admintest; Password = ***; initial catalog = testdb; provider = sqlncli.1; persist Security info = true ;" />
</ Appsettings >
</ Configuration >

Note: The dtsx template and CSV file must be used in the method. For the simple generation of CSV files, refer to this article. The dtsx template is a duplicate, and you are a newbie and have no right to speak, you can refer to online resources.
Finally, I'm weak enough to ask, isn't this an alleged plagiarism? Non-original, non-original... meditation one hundred times,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.