Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

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

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)
            {
                rows.Add(record);
                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;
                rowObjects.Add(convertedRow);
            }
        }
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial