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,