How to preserve Array values and bulk copy to data

Results 1 to 2 of 2

Thread: How to preserve Array values and bulk copy to data

  1. #1
    Join Date
    Dec 1969

    Default How to preserve Array values and bulk copy to data

    Hello,<BR><BR>I am a newbie to C# and was looking into solution for doing something like that stated below:<BR><BR>- Have a delimited text file that needs to be copied to a table in the database. <BR><BR>- To establish this I have the below code:<BR><BR>/******************start code *************************/<BR>FileStream fsConfigFile = new FileStream(feedfile, FileMode.Open);<BR>StreamReader srConfigFile = new StreamReader(fsConfigFile);<BR>string configFileText;<BR>string delimeter = "&#124";<BR>char[] del = delimeter.ToCharArray();<BR> <BR>while ((configFileText = srConfigFile.ReadLine()) != null)<BR>{<BR> string[]feedcolumn = configFileText.Split(del); <BR> <BR> foreach(string i in feedcolumn)<BR> {<BR> Console.WriteLine(i); <BR> <BR> }<BR>}<BR><BR>/******************end code ****************************/<BR><BR>- As the source file is huge I am not in favour of doing a insert into table for every line I read. Need to store all the records in an array and then do a one time opening and writing to the table. <BR><BR>- Since there is nothing like Redim Preserve in C# how do I go about the above, seemingly simple operation through C#<BR><BR>Any lead or to articles that discuss this issue will be greatly appreciated.<BR><BR>Thanks<BR>GI

  2. #2
    Join Date
    Dec 1969

    Default Well, perhaps you aren't aware...

    ...that if you do create a DataTable with DataRow instances in it, and then do an Update on that table via a DataAdapter, then behind the scenes the Update method *actually* simply calls an Insert method (and thus SQL Insert query) in a "for" loop.<BR><BR>In other words, there don real reason for you *not* to do an insert into a table for each line you read! You&#039;ll *probably* actually get better performance.<BR><BR>But having said that...<BR><BR>The .Net Framework way to do this is to create a DataTable and then, for each line of the file:<BR> -- Create a DataRow and fill the DataRow with the fields from one line. <BR> -- Add the DataRow to the Rows collection in the data table.<BR><BR>And then, as I said, use a DataAdapter and its Update method to do the work of inserting each row to the DB. (This works because the act of creating and adding the DataRow to the table marks it as "Added" [see the RowState property of a Row instance] and all "Added" rows invoke the InsertCommand of the adapter.)<BR><BR>If you use a strongly typed DataSet (and, consequently, strongly typed DataTable), then you even get type safety all the way along, except where you take those strings out of the Split and have to trust that they can be converted to the right column types for the DataRow.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts