Read Flat File and insert each line to database

The task is to read a flat file which is pipe delimited (first row has column names). Insert all data fields into the database (MS SQL; using sqlclient). The code is in c#. I have here the code to read line and split fields. Please help me include the sqlparemeters in the foreach line loop so each line can be inserted into the table.
I haven't been able to assign values of the parameters inside the loop while reading each line. Help me out. Thanks
string strLine;
string[] strArray;
char[] charArray = new char[] { '|' };
FileStream aFile = new FileStream(pathToFile, FileMode.Open);
StreamReader sr = new StreamReader(aFile);
strLine = sr.ReadLine();
string sqlcmd=@"Insert into table value1, value2, value3, value4 values (@value1,@value2,@value3,@value4)"; 
strArray = strLine.Split(charArray);
//read column names
for (int x = 0; x <= strArray.GetUpperBound(0); x++)
  {
     strArray[x].Trim();
  }
strLine = sr.ReadLine();
while (strLine != null)
  {
     strArray = strLine.Split(charArray);
     for (int i = 0; i <= strArray.GetUpperBound(0); i++)
     {
       strArray[i].Trim();
       // here add code to add SqlParameters 
       // cmd.Parameters.Add(new SqlParameter("@value1", null));
       //cmd.Parameters["value1].Value = strArray[1];
     }
      strLine = sr.ReadLine();
      sqlcmd.ExecuteNonQuery();
   }

Open in new window

darthvader747Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
Try: (Replace lines 17-26 with:)
     strArray = strLine.Split(charArray);
     for (int i = 0; i <= strArray.GetUpperBound(0); i++)
     {
       strArray[i].Trim();
       // here add code to add SqlParameters
       SqlParameter param  = new SqlParameter();
       param.ParameterName = "@Value" + i.ToString();
       param.Value         = strArray[i];
       cmd.Parameters.Add(param);
     }
      sqlcmd.ExecuteNonQuery();
      strLine = sr.ReadLine();

check:
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
http://msdn.microsoft.com/en-us/library/yy6y35y8(VS.80).aspx

Note: Be aware that you database table needs to have value1, ... , valuen fields defined else code will not work. If you require further help please provide sample of flat file as well as table definition.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
darthvader747Author Commented:
This method works. Just implemented and tested it. Thank You so much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.