How would you insert records into a SQL Server 2005 table from a List using C# with VS2010?

Posted on 2013-09-06
Medium Priority
Last Modified: 2013-09-11
How would you modify the following method to insert each record into a SQL Server 2005 table titled tbl_SMR_Bank_Credits_A that is comprised of the fields f1 through f14 that are
of type nvarchar(255)?

For ex:

f1 =   convertedRow[0]
f2 =   convertedRow[1]
f14 = convertedRow[13]

protected void UploadToDB(string fileName)
            FileInfo info = new FileInfo(fileName);
            string tableName = info.Name;

            List<string> rows = new List<string>();

            StreamReader reader = new StreamReader(fileName);
            string record = reader.ReadLine();
            while (record != null)
                record = reader.ReadLine();

            List<string[]> rowObjects = new List<string[]>();

            int maxColsCount = 0;
            foreach (string s in rows)
                string[] convertedRow = s.Split(new char[] { ',' });
                if (convertedRow.Length > maxColsCount)
                    maxColsCount = convertedRow.Length;
Question by:zimmer9
1 Comment
LVL 41

Accepted Solution

Kyle Abrahams earned 2000 total points
ID: 39470508
I would create a stored proc the does the insert,

create a SqlConnection
loop over the list

create a SqlCommand
add each field as a parameter to the command
Set the Command To the connection
open the connection
close the connection
next record.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
SingleRun is a tool that ensures that only one instance of an application is started, running it again brings the application to focus.
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question