Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:35:37+00:00 2026-06-08T20:35:37+00:00

I have an assignment where I need to read a text file and then

  • 0

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();



    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:35:39+00:00Added an answer on June 8, 2026 at 8:35 pm

    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

    File.ReadAllLines
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello is have a question for a school assignment i need to : Read
I have a homework assignment where I need to take input from a file
I need to write a function that can read a file, and add all
I have an assignment in which I need to create a function that tells
I have a large text file I need to sort in Java. The format
I am dealing with some HTML text that I basically have read access to.
I have created an algorithm that converts text formatted in SVG into HTML, the
Hello I have a homework assignment where I need to read two matrix .txt
My assignment is to get images read into pixmaps which you will then convert
I have a text file, that is formatted somewhat like this: 1 3 4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.