Creating and Deploying a .dtsx package to upload CSV,xcl in Database

by daz 10/5/2009 11:24:00 PM

I had a project where a mail service will get mail status from MTA response, parse the data into a CSV files.

Each files may go up to 400 000 records. Those data need to be in my database to update a table.

To insert all of this row by row and wait for the next file is time consuming. Thus, i create a windows service and an SQL DTSX packages to perform this task. pretty fast and pretty nice.

1. Create the DTSX package in SQL manager studio

2. Create your win services

Here is the code .

 public partial class DZ_DTSService : ServiceBase new ArrayList();

{

Timer _timer;

ArrayList _logFiles =

public DZ_DTSService()

{new Timer(new TimerCallback(OnNextMinute), null , Timeout.Infinite, Timeout.Infinite);

_timer =

InitializeComponent();

}

public void OnNextMinute(object state)

// Read config  

Settings dzSetting;

if (!ReadSettings(out 

dzSetting))  

 return;  

FillStack(dzSetting);

}

void FillStack(Settings settings)

{

try  

{

string[] files = Directory.GetFiles(settings.CsvFolder, @"*.csv"

, SearchOption.TopDirectoryOnly); 

for (int

i = 0; i < files.Length; i++)

{_logFiles.Add(files[i]);

}

RunSSISPackageService(settings);

}

catch (Exception

ex)

{

WriteError(ex);

}

}

protected override void OnStart(string

[] args)

{ Settings dzSetting;

if (!ReadSettings(out

dzSetting))

 

return ;

AppLogger.WriteInformation(

 

"Service has Started "

);

_timer.Change(0, dzSetting.Sleep);

 

//sec + 000 Milisecond every 5 minute run service = 5*60 000

 }

 

protected override void

 

OnStop()

{

AppLogger.WriteInformation(

 

"Service has Stopped "

);

}

//read setting for the application

//this will return all necessary variable stored in the config file for later use

 

bool ReadSettings(out

Settings settings)

{

 

bool success = true;

Settings setting = AppSettings.ReadSettings();

settings = (success)?(setting):(

 

null

); 

 

return

success;

}

 

protected void

RunSSISPackageService(Settings setting)

{

Application app =

 

new

Application();

Package package = 

null ;

string pkgLocation = setting.CsvPackage;  

try

 

{

package = app.LoadPackage(pkgLocation,

 

null);

//load the package file

package.Connections[ 

"DestinationConnectionOLEDB"].ConnectionString = setting.Connection;

//The Database connection string get from app config

foreach (string logFile in

_logFiles)

// Log exist 

if

(!File.Exists(logFile)) 

continue

try

{

// string fileName = logFile.ToString();  

package.Connections[

"SourceConnectionFlatFile"].ConnectionString = logFile;

//change connection string 

//for each files since you may have multiple file waiting  

//Execute DTSX.

DTSExecResult results = package.Execute();

 

// great way to write error from the application 

//log those errors in a log file

 if

(results == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure)

{

 

foreach (Microsoft.SqlServer.Dts.Runtime.DtsError local_DtsError in

 package.Errors)

{

WriteError(setting,

"Error while Excecuting File: "

 

+ logFile +

" Code: "

+ local_DtsError.Description);

}

}

 

else

//delete if update DB //move file

{

 

//Move the file to an archive directory..

 

FileInfo fi =

new

FileInfo(logFile.ToString());

 

string

destination=setting.Archive;

fi.MoveTo(destination + fi.Name);

WriteSuccess(setting, fi.Name);

}

}

catch (Exception

ex)

{

WriteError(setting, ex.Message );

}

}

}

 

 

catch (Exception

ex)

{

WriteError(setting,ex.Message);

}

 

finally

 

{

 

 

if (package != null

)

{

package.Dispose();

package =

 

null;

 

}

}

}

 

Tags: , ,

Comments

11/23/2009 12:31:38 AM

cash loans

Just try to smile for about 2-3 mins then you can get back to work

cash loans United States

12/5/2009 1:58:30 PM

honda dealer nj

Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!


Regards
Desin



honda dealer nj United States

12/5/2009 2:02:44 PM

car dealerships in connecticut

I would like to read more of your blogs and to share my thoughts with you


Regards
Sizal

car dealerships in connecticut United States

12/8/2009 1:33:04 PM

chinese wholesale

wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post


Regards
Oconnor




chinese wholesale United States

12/9/2009 8:45:56 AM

online internet marketing course

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.



Regards
Pontius



online internet marketing course United States

12/10/2009 7:26:29 AM

watch gossip girl online

Generally I do not post on sites, but I would like to say that this article really forced me to do so! Thanks, really nice article.




Regards
Fisher

watch gossip girl online United States

12/10/2009 2:50:17 PM

san miguel allende real estate

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.



Regards
Beaulieu






san miguel allende real estate United States

12/10/2009 2:57:15 PM

replace xbox 360 hard drive

i would love to read more from you on this




Regards
Oliver

replace xbox 360 hard drive United States

12/11/2009 1:01:51 PM

cheapest warcraft gold

Nice post I Like your site very well and continue to do so. I have bookmarked your site.



Regards
Gable

cheapest warcraft gold United States

12/12/2009 8:12:45 AM

Provo Real Estate

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.



Regards
Shoemaker







Provo Real Estate United States

12/14/2009 12:25:00 PM

dog carseat

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.



Regards
Hawk




dog carseat United States

12/14/2009 2:45:59 PM

hip hop reviews

Generally I do not post on sites, but I would like to say that this article really forced me to do so! Thanks, really nice article.




Regards
Sam

hip hop reviews United States

12/18/2009 1:07:18 PM

Watch House Episodes Online

Nice work. Very informative article. I will love to bookmark you for the future preferences. Thanks for sharing.




Regards
Pender


Watch House Episodes Online United States

12/18/2009 1:12:02 PM

car lease companies

Is this true ?


Regards
Finley

car lease companies United States

12/19/2009 5:36:18 AM

Homemade Brownie Recipes

You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

Homemade Brownie Recipes United States

12/21/2009 5:14:06 AM

Eve Online ISK Guide

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up

Eve Online ISK Guide United States

12/21/2009 11:58:01 AM

buy fioricet bluelist

I was wondering what is up with that weird gravatar??? I know 5am is early and I'm not looking my best at that hour, but I hope I don't look like this! I might however make that face if I'm asked to do 100 pushups. lol

buy fioricet bluelist United States

12/21/2009 1:07:00 PM

us online casinos jes

You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

us online casinos jes United States

12/23/2009 3:45:13 AM

cloth diapers

Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post.

cloth diapers United States

12/23/2009 9:01:17 PM

payday loans

Nice resource. rss feed added

payday loans United States

12/26/2009 12:07:04 PM

how to find a good lawyer

i would love to read more from you on this




Regards
Castillo

how to find a good lawyer United States

12/26/2009 12:17:43 PM

how to get a small business loan

Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!


Regards
Basham








how to get a small business loan United States

12/27/2009 7:12:50 PM

mercedes chrome wheels

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine. Did you acquired lots of links and I see lots of trackbacks??

mercedes chrome wheels United States

12/27/2009 11:52:18 PM

link building service

The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!

link building service United States

12/28/2009 3:00:45 PM

online senior dating

This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article

online senior dating United States

12/29/2009 7:23:59 PM

HI Payday Loans

Do you have any more info on this?

HI Payday Loans United States

12/31/2009 9:04:44 PM

Size Genetics

The beauty of these blogging engines and CMS platforms is the lack of limitations and ease of manipulation that allows developers to implement rich content and 'skin' the site in such a way that with very little effort one would never notice what it is making the site tick all without limiting content and effectiveness.

Size Genetics United States

1/4/2010 11:07:36 AM

south wales wedding photographer

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

south wales wedding photographer United States

1/10/2010 8:37:35 PM

fat burning tips

I was wondering what is up with that weird gravatar??? I know 5am is early and I'm not looking my best at that hour, but I hope I don't look like this! I might however make that face if I'm asked to do 100 pushups. lol

fat burning tips United States

1/11/2010 4:54:24 AM

Home Refinancing Rates

I really want to know about blogengine.net more and more. But, I can't understand at all script such your article above.

Home Refinancing Rates United States

1/11/2010 5:03:40 AM

Cheap Car Insurances

Hi David,
Nice ad placement in your post. Compare with mine Laughing

Cheap Car Insurances United States

1/15/2010 1:59:25 AM

cars

thanks for information. ilike your site

cars United States

1/15/2010 10:48:28 PM

starting your own business

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

starting your own business United States

1/21/2010 3:09:46 AM

billigt internet

Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

billigt internet United States

1/23/2010 12:42:27 PM

male enhancement supplements

Late for work already, I was annoyed to find a strange car in my reserved parking space again. After locating a spot far away, I stormed into my office determined, to have the car towed. As the morning wore on, however, my anger cooled, and I decided to give the driver another chance.

During lunchtime, I went outside and left this note on the driver's windshield:

"Please don't take my parking space. If you do, and your car disappears, don't say I never towed you!"




Regards
Muniz




male enhancement supplements United States

1/23/2010 1:16:13 PM

pay day loans

Success is the maximum utilisation of the ability that you have.

pay day loans United States

1/24/2010 9:42:31 PM

roulette systems

Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!

roulette systems United States

1/26/2010 4:16:12 AM

gold coins

Hey - nice blog, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Wordpress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?

gold coins United States

1/27/2010 12:20:54 PM

Seattle plastic surgeon

Get even with a bear who raided your food bag by kicking his favorite stump apart and eating all the ants.

A hot rock placed in your sleeping bag will keep your feet warm. A hot enchilada works almost as well, but the cheese sticks between your toes.

The best backpacks are named for national parks or mountain ranges. Steer clear of those named for landfills.

When camping, always wear a long-sleeved shirt. It gives you something to wipe your nose on.

Take this simple test to see if you qualify for solo camping. Shine a flashlight into one ear. If the beam shines out the other ear, do not go into the woods alone.

A two-man pup tent does not include two men or a pup.

A potato baked in the coals for one hour makes an excellent side dish. A potato baked in the coals for three hours makes an excellent hockey puck.

In emergency situations, you can survive in the wilderness by shooting small game with a slingshot made from the elastic waistband of your underwear.




Regards
Martin

Seattle plastic surgeon United States

1/27/2010 11:10:21 PM

australian coins

Great post! I am just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it?s all a bit new to us. The example above is someth

australian coins United States

1/28/2010 9:22:31 AM

printing in florida

I like how you create.Are you unputdownable in a voice moment author job?



Regards
Lloyd







printing in florida United States

1/28/2010 10:24:02 AM

anti aging book

I would similar to add your diary to my blogroll satisfy swan me what fasten should I use?





Regards
Churchill

anti aging book United States

1/29/2010 8:47:37 AM

radio software

Pretty discriminating instal. I fitting stumbled upon your diary and welcome to say that I possess real enjoyed city your diary posts. Any way I'll be subscribing to your work and I outlook you author again soon.




Regards
Moorehead

radio software United States

1/30/2010 8:08:58 AM

stop snoring solution

Pretty Stimulating position. Couldnt be cursive any surmount. Metropolis this job reminds me of my old assemblage officer! He e'er kept conversation some this. I will low this instal to him. Pretty reliable he give jazz a honorable interpret. Thanks for sharing!





Regards
Russell

stop snoring solution United States

1/30/2010 10:12:00 AM

cheap accommodation bangkok

You really neaten it seem so smooth with your presentation but I conclude this topic to be rattling something which I guess I would never see. It seems too complicated and really clear for me. I am perception saucy for your close spot, I give try to get the fix of it!





Regards
Marroquin

cheap accommodation bangkok United States

About the author

Name of author
DavidZ
.Net Programmer
E-mail me Send mail

Calendar

<<  August 2010  >>
MoTuWeThFrSaSu
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in