I have an assignment where I need to read a text file and then breakdown each line into columns then I need to insert that into database.
What’s the best approach for this? Any help will be appreciate it and if you could provide code will be even better.
This is what I have so far
string filename = Server.MapPath("~/Text_File_4.txt");
StreamReader sr = new StreamReader(filename);
string styl;
string colr;
string sdim;
string size;
string qty;
string line;
string sprice;
string sretail;
while ((line = sr.ReadLine()) != null)
{
styl = line.Substring(0, 6);
colr = line.Substring(6, 2);
sdim = line.Substring(8, 1);
size = line.Substring(14, 3);
qty = line.Substring(19, 5);
sprice = line.Substring(27, 6);
sretail = line.Substring(38, 4);
con.Open();
cmd = new SqlCommand("insert into ststyl00(ststyl, stcolr, stsdim, stszcd, stprq, strprq) values(@ststyl, @stcolr, @stsdim, @stszcd, @stprq, @strprq)", con);
cmd.Parameters.Add("@ststyl", SqlDbType.VarChar, 15).Value = styl;
cmd.Parameters.Add("@stcolr", SqlDbType.VarChar, 3).Value = colr;
cmd.Parameters.Add("@stsdim", SqlDbType.VarChar, 8).Value = sdim;
cmd.Parameters.Add("@stszcd", SqlDbType.VarChar, 3).Value = size;
cmd.Parameters.Add("@stprq", SqlDbType.VarChar, 8).Value = sprice;
cmd.Parameters.Add("@strprq", SqlDbType.VarChar, 8).Value = sretail;
cmd.ExecuteNonQuery();
con.Close();
}
Input is a CSV
If your input files are CSV files, I strongly recommend using the CSV Reader class available at
http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader
Input is Fixed-Width
If your input is fixed-width, just read all of the lines in and parse each individual line into an appropriate structure to store in the database (more on that in a moment).
If you have just a little text to read (perhaps a few megabytes or less), just use
http://msdn.microsoft.com/en-us/library/s2tte0y1
to read in all of the lines of the file into a string[].
Writing to the DB
You now have a capability to read in the file. Now, you need to write it out to the database. Presumably there is a DB table with a given schema that matches the data in the file. Have a look at ADO.Net to understand how to write to a database and ask specific questions as needed.
http://msdn.microsoft.com/en-us/library/h43ks021(v=vs.100).aspx