Click to See Complete Forum and Search --> : Reading Strings from Text File problem


gian.sobhani
May 19th, 2009, 01:54 AM
I'm reading a text file and inserting it into database, using following code, which contains few strings like this

DAV|FIN-ABC;0;0;363.03;|*
DAV|SIN-CTF;0;0;299.46;|*
DAV|POS-PTF;100;135;299.46;|*
DAV|DON-ATF;200;150;363.03;|*
DAV|DSS-ATF;100;135;297.46;|*

All the values are inserted into table, problem is first column which is separated by 'pipe separator'. [FONT='Verdana','sans-serif']Can anyone amend the following code with liffle effort to separate first column with pipe and than others with semi colon which is working but problem is to separate first column with pipe separator.

using
System;
using
System.IO;
using
System.Collections.Generic;
using
System.Text;
using
System.Data;
using
System.Data.SqlClient;
namespace
Reading_File
{
class ReadingFile
{
static void Main(string[] args)
{
string str = string.Empty;
string[] storage = null;
string strClientBates = string.Empty;
string strFilePath = string.Empty;
char[] pipe ={ '|' };
char[] semi ={ ';' };
// string record_identifier;
/* Variables to store values of Text FileTemporarily */
string etf_symbol;
string volume_traded;
string etf_last_traded_price;
string nav;
/* Reading Text File for the values */
using (FileStream file = new FileStream("Text.txt", FileMode.Open, FileAccess.ReadWrite))
{
using (StreamReader reader = new StreamReader(file))
{
while ((str = reader.ReadLine()) != null)
{
/* Separating values with 'semi-colon separator */
storage = str.Split(semi);
if (storage != null)
{
etf_symbol = storage[0];
volume_traded = storage[1];
etf_last_traded_price = storage[2];
nav = storage[3];
/* Opening Connection */
string sConn = "server=SomeDatabase; uid=usergian; pwd=somepassword; database=hia; Connect Timeout=10000";
SqlConnection ObjCon = new SqlConnection(sConn);
/* Inserting values into Table */
string sQuery = "Insert into etf values('','" + etf_symbol + "','" + volume_traded + "','" + etf_last_traded_price + "','" + nav + "')";
SqlCommand ObjCmd = new SqlCommand(sQuery, ObjCon);
ObjCon.Open();
int a = ObjCmd.ExecuteNonQuery();
ObjCon.Close();
Console.WriteLine("Records inserted successfully");
}
}
}
}
}
}
}

memeloo
May 19th, 2009, 03:40 AM
please use code tags...

and why don't you do it with regex?

MNovy
May 19th, 2009, 04:54 AM
you can use the usual String-Separator method and put all string chunks into an array.
It is just one line of code.

eclipsed4utoo
May 19th, 2009, 06:03 AM
either of these two will work....

1. split on the semi-colon, then split the first element of that array by the pipe.
2. split on the pipe, then split the second element of that array by the semi-colon.

You have all of the code there. You just have to put it together.

MNovy
May 19th, 2009, 06:46 AM
look at this code, it is very simple.
It takes a text line and splits the words into an array.
The separator can hold many characters. So the split is done in only one line of code.



using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char[] sep = { '|', ';' };
string[] words;
string readTextLine = "DAV|FIN-ABC;0;0;363.03;|*";

words = readTextLine.Split(sep);


foreach (string singleWord in words)
{
Console.WriteLine(singleWord);
}
}
}
}





Console output will be:

DAV
FIN-ABC
0
0
363.03
*