How to split the data ??

Posted by Laghaterohan under Regular Expressions on 12/13/2010 | Points: 10 | Views : 1839 | Status : [Member] | Replies : 4
Hello friends,

I have following details in my file (it is not a csv file and also it has no special characters)

123456789198765432

like wise i will be having plenty of rows in my file one below the other...

i have 3 columns CODE 1 , CODE 2 AND CODE 3 in my database.

Now what i need to do is insert first 9 (i.e. from 1 to 9 ) in column 1 , then from 1 to 5 in column 2 and from 4 to 2 in column 3.
Likewise i will loop through all the rows and accordingly insert data into the database.

MY QUESTION IS HOW TO SPLIT THIS FILE????? THIS IS NOT A CSV FILE THEN HOW TO DO IT??? PLEASE HELP ME OUT...

I HAVE WRITTEN CODE TO INSERT THE FILE DATA INTO THE SQL SERVER....ONLY THE PROBLEM IS HOW TO SPLIT THE FILE....


PLEASE HELP!


THANKS!

Best Regards,
Rohan Laghate



Responses

Posted by: SheoNarayan on: 12/14/2010 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
User SubString function of C#, something like below

string myString = "abc";

string myString.Substring(1, 2); // will give "bc"


First you need to read the file, I hope you are able to do that or use this article http://www.dotnetfunda.com/articles/article462-different-ways-of-reading-and-writing-text-file-data-in-net-.aspx and use above function to split the string, that will help you to split your string from 1 to 9, 1 to 5 and so on.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 12/14/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Rohan Laghate,

Follow the Sheo Narayan answer.Using SubString method you can achieve your output.But in case string length is different from other means you face the problem.So in your looping concept check the string length.

Cheers :)

Thanks,
T.Saravanan

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/9/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi Rohan Laghate,

Does the file going to be in the database or the file content is going to be saved in database?

if the file to be saved in db and should be read and do the manipulation then follow the link

http://www.motobit.com/tips/detpg_read-write-sql-image-file/

Thanks
Karthik
www.f5Debug.net

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suresh.mekkattil on: 1/9/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi Freind,

Use Substring() method of the system.String class. Please follow as the following.

string yourFileContent = "123456789198765432";
string code1 = string.Empty;
string code2 = string.Empty;


// Use the following code for reading the content from the file.
// Use System.IO.File.ReadAllText to read the file content.
yourFileContent = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");

code1 = yourFileContent.Substring(1, 9);
code2 = yourFileContent.Substring(1, 5);

// proceed with your database insert call...


-----
Suresh M

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response