Header Ads

Header ADS

Create a console based link in C#

Create a console based  link in C#

Create a console based application that has following features:
1.      Create a class to store student information (First Name, Last Name, Major, Section)
2.      Populate an ArrayList of students class objects for 10 students.
3.      Using LINQ to Objects, retrieve all students belonging to section 5A and display on the console.
4.      Using LINQ to SQL, insert these records into a table in a database.

5.      Verify that they are stored in the database by retrieving these records using LINQ to SQL and displaying on the console.

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data.SqlClient;
using System.Data;

namespace lab_12
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList obj_arr = new ArrayList();
            obj_arr.Add(new cstudent("Shoaib","Malik","Computer",'A'));
            obj_arr.Add(new cstudent("Ahmer","Zaman","Computer",'A'));
            obj_arr.Add(new cstudent("Athar","Tahir","Accounting",'A'));
            obj_arr.Add(new cstudent("Farah","Khan","Computer",'A'));
            obj_arr.Add(new cstudent("Zeeshan","Ahmed","Accounting",'A'));
            obj_arr.Add(new cstudent("Khurram","Riaz","Mathematics",'B'));
            obj_arr.Add(new cstudent("Sara","Husnain","English ",'A'));
            obj_arr.Add(new cstudent("Saad","alam","Mathematics",'B'));
            obj_arr.Add(new cstudent("Behzad","Rasheed","Accounting",'B'));
            obj_arr.Add(new cstudent("Jaweria","Nawaz","Psychology",'A'));
            obj_arr.Add(new cstudent("Madiha","Khan","Mathematics",'B'));
            var query = from cstudent std in obj_arr where std.Section == 'A' select std;
            string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Shoaib\My Documents\Visual Studio 2008\Projects\lab_12\lab_12\db_12.mdf;Integrated Security=True;User Instance=True";
            stdnt ins= new stdnt(con);
            SqlConnection conn = new SqlConnection(con);
            conn.Open();
            foreach(cstudent std in query)
            {
               //SqlConnection con= new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Shoaib\My Documents\Visual Studio 2008\Projects\lab_12\lab_12\db_12.mdf;Integrated Security=True;User Instance=True");
               Console.WriteLine(std.First_Name+"\t"+std.Last_Name+"\t"+std.Major+"\t"+std.Section);
                  
                //db_12DataSet db = new db_12DataSet(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Shoaib\My Documents\Visual Studio 2008\Projects\lab_12\lab_12\db_12.mdf;Integrated Security=True;User Instance=True");
            }
            conn.Close();
           
            SqlCommand com= new SqlCommand("select * from student",conn);
            SqlDataReader dr;
            stdnt obj = new stdnt(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Shoaib\My Documents\Visual Studio 2008\Projects\lab_12\lab_12\db_12.mdf;Integrated Security=True;User Instance=True");
             query = from cstudent std in obj_arr select std;
           
            Console.WriteLine("\n\n");
            conn.Open();
            foreach (cstudent std in query)
            {
               
                Console.WriteLine(std.First_Name + "\t" + std.Last_Name + "\t" + std.Major + "\t" + std.Section);

            }
            conn.Close();
      
        }
        public partial class stdnt : DataContext
        {
            public cstudent std_obj;
            public stdnt(string con) : base(con) { }
          
        }
        public class cstudent
        {
            public string First_Name, Last_Name, Major;
            public char Section;
            public cstudent(string first, string last, string mjr, char sec)
            {
                First_Name = first;
                Last_Name = last;
                Major = mjr;
                Section = sec;
            }
        }
    }

}

Screen Shot

No comments

Powered by Blogger.