Click here to Skip to main content
15,888,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to take a class item and insert it into a MDF file.

I am having trouble equating the "List" classlist to its individual columns.

How do I break classlist down to its individual components?
C#
public class Class1
    {
        public Int32 id { get; set; }
        public string col1 { get; set; }
        public string col2 { get; set; }
        public string col3 { get; set; }
    }


C#
List<Class1> classlist = new List<Class1>();
            classlist.Add(new Class1() { id = 1, col1 = "11", col2 = "22", col3="33" });
            classlist.Add(new Class1() { id = 2,  col1 = "44", col2 = "55", col3="66" });
            classlist.Add(new Class1() { id = 3,  col1 = "77", col2 = "88", col3="99" });
            string connStr = ConfigurationManager.ConnectionStrings["MDFdb"].ConnectionString;
            string cmdStr = "INSERT INTO [Table1] (col1,col2,col3) VALUES (@col1,@col2,@col3);";
            for (int n; n<25; n++)
            {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.Add("@col1", classlist[n,1]);
                    cmd.Parameters.Add("@col2", classlist[n,2]);
                    cmd.Parameters.Add("@col2", classlist[n,3]);
                }
            }
            }
Posted
Updated 26-Sep-14 2:14am
v2
Comments
[no name] 26-Sep-14 8:43am    
classlist[n].col1 does not work for you? Keep in mind that when n > 3, you are going to get an exception.

1 solution

classlist is a list of classes of type Class1 which appear to have properties id, col1, col2, etc.

So, you could access it like this:

C#
classlist[n].id
classlist[n].col1
...


Since classlist[n] will be an instance of Class1. Intellisense should show you this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900