Upload CSV in SQL Database using Linq

by daz 10/20/2009 4:46:00 AM

 In my previous article http://davidz.afriklink.com/post/Creating-and-Deploying-a-dtsx-package-to-upload-CSVxcl-in-Database.aspx 

I encounter a problem where the system generating csv file create some files with empty cell.As you know it is hard inserting empty data to an integer field in a database. I did not want to change my emailId that is Integer in the table to be a varchar for example.

So my solution was to create another function that will get all the files that was rejected by the dtsx package and read them line by line

private void ErrorCorrection(Settings setting) //setting has all my configuration meaning directories for all files

 {

 string[] files = Directory.GetFiles(setting.ErrorFolder, @"*.csv", SearchOption.TopDirectoryOnly); //Loop through all  csv files error s ones   

for (int i = 0; i < files.Length; i++) foreach (string errorFile in _logFilesError) // Log exist

 {

_logFilesError.Add(files[i]);

} 

{

if (!File.Exists(errorFile)) continue 

var fileInfo = new FileInfo(errorFile);

string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileInfo.DirectoryName + ";Extended Properties='Text;HDR=Yes;FMT=CSVDelimited'"; 

OleDbConnection cn = new OleDbConnection(conn);

OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM [" + fileInfo.Name + "]"

, cn); OleDbDataAdapter da = new OleDbDataAdapter(cmd);  

 

DataTable dt = new DataTable();

 

cn.Open(); 

var properties = (from r in

dt.AsEnumerable()  

where r[3].ToString() != string.Empty && r[4].ToString() != string.Empty select r);

foreach (var data in properties) //

{

 TESTCSVdAO dao = new TESTCSVdAO();  

int.Parse(data[2].ToString()), int.Parse(data[3].ToString()),

int.Parse(data[4].ToString()));  

////move to archive folder csv folder

 FileInfo fi = new FileInfo(errorFile.ToString());

string destination = setting.Archive;

}

}

 ###########Linq Action ##########  we can accomplish the sAME with LINQ to CSV

string[] csvlines = File.ReadAllLines(@errorFile); 

var query = from csvline in csvlines let data = csvline.Split(',')

 select new

 

{

type = data[0],dsnStatus = data[1],batchid = data[2],EmailId = data[3],

pBatchid = data[3]};

var result = from q in

query where q.batchid != null || q.pBatchid != null select q;  

foreach (var r in result)  

TESTCSVdAO dao = new TESTCSVdAO();  

int.Parse(r.batchid), int.Parse(r.EmailId), int.Parse(r.pBatchid)); 

 

dao.GetSumaryCampaign(r.type, r.dsnStatus,

 

{

}

 

 

 

dao.GetSumaryCampaign(data[0].ToString(), data[1].ToString(),

 

 

 

da.Fill(dt);

Comments

12/5/2009 11:48:21 AM

gmc dealers indiana

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
Greim

gmc dealers indiana United States

12/8/2009 11:50:06 AM

lexus dealers in ny

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


Regards
Mrin

lexus dealers in ny United States

12/9/2009 12:22:46 PM

watch friends

I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.



Regards
Peng





watch friends United States

12/10/2009 7:28:22 AM

watch one tree hill online

I enjoyed reading it. I need to read more on this topic..Thanks for sharing a nice info.




Regards
Weisberg

watch one tree hill online United States

12/10/2009 9:52:08 AM

web 2.0 internet marketing

I am to submit a report on this niche your post has been very very helpfull

Regards
Guerra

web 2.0 internet marketing United States

12/10/2009 2:45:17 PM

Home loans Colorado

The blog is amzaing, I like the way author instruct the different way of things. Thank you so much for sharing the information wih us.




Regards
Hicks


Home loans Colorado United States

12/11/2009 8:21:30 AM

hunting blind

where do i get more information on this




Regards
Mitchell

hunting blind United States

12/14/2009 8:45:26 AM

chemical free pools

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
Smither








chemical free pools United States

12/14/2009 9:52:55 AM

chemical free pools

The blog is amzaing, I like the way author instruct the different way of things. Thank you so much for sharing the information wih us.




Regards
Woods

chemical free pools United States

12/14/2009 2:42:58 PM

high speed internet connection

this is nice information need to know more




Regards
Smith

high speed internet connection United States

12/15/2009 8:18:26 AM

International Calling

Can i have more info on this ?

Regards
Rucker



International Calling United States

12/15/2009 12:23:51 PM

Photo to Pop Art

That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.




Regards
Matthews



Photo to Pop Art United States

12/15/2009 12:28:23 PM

Prada Handbag

This article gives the light in which we can observe the reality. this is very nice one and gives in depth information. thanks for this nice article Good post.....Valuable information for all.I will recommend my friends to read this for sure



Regards
Goal

Prada Handbag United States

12/16/2009 8:35:31 AM

adirondack folding chairs

i would love to read more from you on this




Regards
Alfaro


adirondack folding chairs United States

12/17/2009 6:55:39 AM

seattle child support attorney

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


Regards
Timothy










seattle child support attorney United States

12/17/2009 12:15:00 PM

Texas Weight Loss Doctors

This seems like the most comprehensive blog on this niche

Regards
Savoy


Texas Weight Loss Doctors United States

12/18/2009 8:08:37 AM

white boards

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of presentation.


Regards
Martinez




white boards United States

12/18/2009 1:21:13 PM

wine clubs

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.


Regards
Dodson

wine clubs United States

12/19/2009 7:24:31 AM

Best Barbecue Recipes

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

Best Barbecue Recipes United States

12/19/2009 11:41:08 PM

contractor accountants

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.

contractor accountants United States

12/21/2009 3:01:07 PM

usa online casinos jes

You may have not intended to do so, but I think you have managed to express the state of mind that a lot of people are in. The sense of wanting to help, but not knowing how or where, is something a lot of us are going through.

usa online casinos jes United States

12/23/2009 2:20:39 AM

cloth diapers

Thank you for the sensible critique. Me & my neighbour were preparing to do some research about that. We got a good book on that matter from our local library and most books where not as influensive as your information. I am very glad to see such information which I was searching for a long time.This made very glad Smile

cloth diapers United States

12/26/2009 10:36:36 AM

personal loans for debt consolidation

Nice Read

Regards
Daigneault

personal loans for debt consolidation United States

12/26/2009 10:41:15 AM

luxury spa hotels

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



Regards
Ray

luxury spa hotels United States

12/26/2009 10:46:29 AM

international auto shipping

i would love to read more from you on this




Regards
Castillo

international auto shipping United States

12/27/2009 3:57:05 PM

motorcycle manual

I am not much of a guy who thinks in so deeply about web design but I think your post had some valid points in it. Like designers are forced to design stuff within the limited code available and not go beyond it, their innovation is somewhat limited but still I think Web Design won't die! I agree that Amazon and other some big sites won't have a blog but now a days it's very important to have some sort of option available so people can quickly communicate their thoughts. I think Amazon if wants to shift it to that, they can get a customized CMS for themselves.

motorcycle manual United States

12/28/2009 10:17:28 AM

adwords management

Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.

adwords management United States

12/29/2009 11:02:48 PM

Stock Broker

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

Stock Broker United States

12/30/2009 7:48:20 PM

kit car insurance quote

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.

kit car insurance quote United States

1/4/2010 12:34:43 PM

Refurbished Garmin

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck!

Refurbished Garmin United States

1/5/2010 9:26:46 PM

portland dui lawyer

Just wanted to give you a shout from the valley of the sun, great information. Much appreciated.

portland dui lawyer United States

1/23/2010 1:10:24 PM

online christian book store

A mechanic was removing the cylinder heads from the motor of a car,when he spotted the world-famous heart surgeon in his shop. The
heart surgeon was standing off to the side, waiting for the service manager to come take a look at his car. The mechanic shouted across the garage, "Hello Doctor, please come on over here for a minute."

The famous surgeon, a bit surprised, walked over to the mechanic where he stood. The mechanic straightened up, wiped his hands on a rag and asked, "So doctor, look at this here. I also open hearts, take valves out, grind 'em, put in new parts, and when I finish this will work just like a new one. So how come you get the really big money, when you and I are doing basically the same work?" The doctor smiled, leaned over, and whispered to the mechanic

"Try doing it with the engine running!"


Regards
Trujillo

online christian book store United States

1/25/2010 10:03:58 AM

dentist escondido

Adam was strolling through the Garden of Eden, and he asked God, "God can you put someone else on this planet with me? It's kind of lonely here?"

So God said, "I will put on earth a woman, "

"'What is this woman?" asked Adam.

"A woman is somebody who will provide companionship and take care of all your needs," explained God.

"Oh holy master, what is the price for this women"' asked Adam.

"The price for her is your left arm and your right eye," said God.

Then Adam replied, "Ehh... what can I get for a rib?"


Regards
Brehm






dentist escondido United States

1/25/2010 1:49:35 PM

Rhinoplasty orange county

A passenger in a taxi tapped the driver on the shoulder to ask him something.

The driver screamed, lost control of the cab, nearly hit a bus, drove up
over the curb and stopped just inches from a large plate glass window.

For a few moments everything was silent in the cab, then the driver said, "Please,
don't ever do that again. You scared the daylights out of me."

The passenger, who was also frightened, apologized and said he didn't
realize that a tap on the shoulder could frighten him so much, to which the
driver replied, "I'm sorry, it's really not your fault at all. Today is my
first day driving a cab, I have been driving a hearse for the last 25 years.



Regards
Schmeltzer

Rhinoplasty orange county United States

1/25/2010 1:51:02 PM

Dental Business Development

"The car won't start," aid a wife to her husband. "I think there's water in the carburettor."

"How do you know?" said the husband scornfully. "You don't even know what the carburettor is."

"I'm telling you," repeated the wife, "I'm sure there's water in the carburettor."

"We'll see," mocked the husband. "Let me check it out. Where's the car?"
"In the swimming pool."




Regards
Kaufman




Dental Business Development United States

1/27/2010 12:23:28 PM

Liposuction Orange County

A waiter approached the man studying the menu carefully at the fancy restaurant. "May I take your order, sir?" he asked.

"Well, I was wondering how you prepare your chickens." The man replied.

"Oh, it's nothing too special, sir," the waiter confided. "We just tell them straight out that they're going to die."


Regards
Hudson




Liposuction Orange County United States

1/27/2010 12:27:37 PM

Breast augmentation san Francisco

An elephant and a crocodile were swimming in the Amazon, when the elephant spots a turtle sunning himself on a rock. The elephant walks over to the turtle, picks him up in his trunk and hurls him far into the jungle.

"What did you do that for?" asks the crocodile.

The elephant answers, "That turtle was the one that bit me almost fifty years ago."
The crocodile says, "And you remembered him after all these years? Boy, you sure do have a good memory."

"Yep," says the elephant. "Turtle recall."



Regards
Hart



Breast augmentation san Francisco United States

1/28/2010 8:31:51 AM

liposuction michigan

Please let me hump if you are fascinated to impact as article illustrator for me? I can engage $10/article.




Regards
Martin

liposuction michigan United States

1/28/2010 9:25:05 AM

plastic surgery oklahoma city

Wonderful illustrated substance. I convey you around that. No question it will be very serviceable for my forthcoming projects. Would same to see any remaining posts on the similar matter!




Regards
Rosario


plastic surgery oklahoma city United States

1/28/2010 2:53:30 PM

kauai vacation rental homes

There is obviously a lot to hump roughly this. I cogitate you made some groovy points in Features also.



Regards
Gibbs

kauai vacation rental homes United States

1/29/2010 9:36:20 AM

adirondack australia

I conceive you make to improve a bit the programme and usability of your blog.



Regards and respect
Saunders








adirondack australia United States

1/29/2010 1:16:49 PM

Boulder mortgage

There is apparently a lot to undergo roughly this. I judge you made some good points in Features also.






Regards
Zuniga


Boulder mortgage United States

1/30/2010 9:32:54 AM

cheapest warcraft gold

Easily, the station is real the superlative on this laudable topic. I happen with your conclusions and instrument thirstily seem guardant to your tense updates. Speech thanks testament not retributory be decent, for the fantasti c lucidity in your activity. I leave now touch your rss give to edict jakes of any updates. Sound operate and often success in your activity enterprize!




Regards
Bowman




cheapest warcraft gold United States

1/30/2010 9:35:43 AM

Buenos Aires rentals

As a Newbie, I am forever searching online for articles that can assist me. Convey you





Regards
Kelly




Buenos Aires rentals United States

1/30/2010 12:50:40 PM

Telephone Answering Service

Never seen much chill airman. I show it all the way to the end. Stay them upcoming.




Regards
Jones



Telephone Answering Service United States

1/30/2010 2:28:27 PM

swimming pool builders

I institute your site from cuil and it is astounding. Impart you very more for sharing such an informative job!







Regards
Williams


swimming pool builders 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