Skip to content

Help Text File

giulioo felix

Asked Jun 26, 2006, 8:44 AM EDT2 answers
Share
LinkedInX
Hi,
i have this text file

GENUARDI ADOLFO

90100

04800530828

VIA ORETO139

MARCHESE SALVATORE

90100

00464940824

IAZZA CARMINE 40

I must read 4 line each time  and put into a database so till the end of the document,
i wrote this code but is not right

try

{

StreamReader sr=new StreamReader("\\Program Files\\SIP\\clienti.txt");

string[] After;

ArrayList lines = new ArrayList();

string line;

int lineb=0;

int conta=0;

while ((line = sr.ReadLine()) != null)

lines.Add(line);

if(conta % 4==0)

{

string a = (string) lines[0];

string b = (string) lines[1];

string c = (string) lines[2];

string d = (string) lines[3];

SqlCeConnection cn = new SqlCeConnection("Data Source=\\Program Files\\SIP\\data.sdf");

cn.Open();

string gg="INSERT INTO clienti(nome,CAP,piva,Indirizzo) VALUES('"+a.ToString()+"','"+b.ToString()+"','"+c.ToString()+"','"+d.ToString()+"')";

SqlCeCommand cmd = new SqlCeCommand(gg,cn);

cmd.ExecuteNonQuery();

cn.Close();

cont++;

}

}

catch(SqlCeException ex)

{

MessageBox.Show("Errore"+ex.Message+ex.Errors);

}
Please help me Bye

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Why are you store the information to a sdf file. is the SQl server running.
    0
  • 0

    Hello would just split each line ti the string array

    FileStream s=new FileStream("D:\\filename.txt",FIleMode.Open);
    StreamReader r=new StreamREader(s);
    string fileStream=r.ReadtoEnd();
    string []lines=fileStream.Split(new char[]{'\n'});
    foeach(string l in lines)
    {
    SQLCommand c=new sqlCommand("insert into table (cols) values(l));
    command.ExecuteReader();
    }

    0