linq mysql database row to array

Solutions on MaxInterview for linq mysql database row to array by the best coders in the world

showing results for - "linq mysql database row to array"
Neele
07 Jul 2016
1First make one class having similar attribute as you database table for eg:
2        public class Student
3        {
4            public int ID { get; set; }
5            public string Name { get; set; }
6            public DateTime DateOfBirth { get; set; }
7
8        }
9after that
10            List<Student> student = new List<Student>();
11            SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Prac\TestAssembly\ForumsProjectCSharp\App_Data\Studetnt.mdf;Integrated Security=True;User Instance=True");
12            SqlCommand cmd = new SqlCommand("select * from Student", conn);
13            SqlDataReader dr;
14            try
15            {
16                conn.Open();
17                dr = cmd.ExecuteReader();
18                while (dr.Read())
19                {
20                    student.Add(new Student()
21                    {
22                        ID = dr.GetInt32(dr.GetOrdinal("ID")),
23                        Name = dr.GetString(dr.GetOrdinal("Name")),
24                        DateOfBirth = dr.GetDateTime(dr.GetOrdinal("DateOfBirth"))
25                    });
26                    
27                }
28                dr.Close();
29            }
30            catch (Exception exp)
31            {
32
33                throw;
34            }
35            finally
36            {
37                
38                conn.Close();
39            }
similar questions
queries leading to this page
linq mysql database row to array