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

  • SEARCH
  • Home
  • 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 512115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:17:12+00:00 2026-05-13T07:17:12+00:00

I have a bunch of 2 line (with header row) ‘|’ delimited text files.

  • 0

I have a bunch of 2 line (with header row) ‘|’ delimited text files. I need to import this into a specific SQL table and I’m having a hard time with the command.

string sqltable = ("dbo.SLT_C" + "60" + "Staging");
string[] importfiles= Directory.GetFiles(@"K:\jl\load\dest", "*.txt")
SqlConnection con = new SqlConnection("Data Source=" + "Cove" + ";Initial Catalog=" + "GS_Ava_MCase"+ ";Integrated Security=" + "SSPI");
con.Open();
foreach (string importfile in importfiles)
{

}

or maybe I am going about this the whole wrong way.

  • 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-05-13T07:17:13+00:00Added an answer on May 13, 2026 at 7:17 am

    You could look at a ready-made solution, like FileHelpers. This FREE library allows you to define the structure of your file by means of a class describing the fields in your file, and then you can easily load the whole file into an array of that class type.

    Once that’s done, just simply iterate through the objects, and save them to your SQL Server.

    Or check out the SQL Bulkcopy options:

    • bcp command line utility
    • SqlBulkCopy class in ADO.NET – also see this article at SQL Team

    If you want to do it in “straight” ADO.NET, use something like this approach:

    string sqltable = "dbo.SLT_C60Staging";
    
    string[] importfiles = Directory.GetFiles(@"K:\jl\load\dest", "*.txt");
    
    // try to wrap your ADO.NET stuff into using() statements to automatically 
    // dispose of the SqlConnection after you're done with it
    using(SqlConnection con = new SqlConnection("Data Source=Cove;Initial Catalog=GS_Ava_MCase;Integrated Security=SSPI"))
    {
       // define the SQL insert statement and use parameters
       string sqlStatement = 
          "INSERT INTO dbo.YourTable(DateField, TimeField, TextField) VALUES(@Date, @Time, @Text)";
    
       // define the SqlCommmand to do the insert - use the using() approach again  
       using(SqlCommand cmd = new SqlCommand(sqlStatement, con))
       {
          // define the parameters for the SqlCommand 
          cmd.Parameters.Add("@Date", SqlDbType.DateTime);
          cmd.Parameters.Add("@Time", SqlDbType.DateTime);
          cmd.Parameters.Add("@Text", SqlDbType.VarChar, 1000);
    
          // loop through all files found
          foreach (string importfile in importfiles)
          {
             // read the lines from the text file
             string[] allLines = File.ReadAllLines(importfile);
    
             con.Open();
    
             // start counting from index = 1 --> skipping the header (index=0)
             for (int index = 1; index < allLines.Length; index++)
             {
                // split up the data line into its parts, using "|" as separator
                // items[0] = date
                // items[1] = time
                // items[2] = text
                string[] items = allLines[index].Split(new char[] { '|' });
    
                cmd.Parameters["@Date"].Value = items[0];
                cmd.Parameters["@Time"].Value = items[1];
                cmd.Parameters["@Text"].Value = items[2];
    
                cmd.ExecuteNonQuery();
             }
    
             con.Close();
          }
       }
    }
    

    That should work – you’re question was too vague to know exactly what data will be in the lines, and what kind of SQL insert statement you’d need…

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

Sidebar

Related Questions

I have a bunch of text files where I want to add a line
I have bunch of files and very file has a header of 5 lines.
I have a bunch of files in a directory, each with one line of
I have a bunch of files. Some are Unix line endings, many are DOS.
I have a bunch of gzipped files in a folder where each line of
I have a bunch of files I need to clean. I need to delete
I have a bunch of files that I need to look at. All lines
I have a bunch of files that are incomplete: the last line is missing
I have a bunch of CSS files that get loaded on every page (header,
I have a bunch (hundreds) of files that are supposed to have Unix line

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.