Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Linq;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Data.Linq.Mapping;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Final_ChangTimothy
  11. {
  12.     //[Table(Name = "Movie")]
  13.     //public class Movie
  14.     //{
  15.     //    private int _MovieId;
  16.     //    [Column(IsPrimaryKey = true, Storage = "_MovieId")]
  17.     //    public int MovieId
  18.     //    {
  19.     //        get
  20.     //        {
  21.     //            return this._MovieId;
  22.     //        }
  23.     //        set
  24.     //        {
  25.     //            this._MovieId = value;
  26.     //        }
  27.  
  28.     //    }
  29.  
  30.     //    private string _Title;
  31.     //    [Column(Storage = "_Title")]
  32.     //    public string Title
  33.     //    {
  34.     //        get
  35.     //        {
  36.     //            return this._Title;
  37.     //        }
  38.     //        set
  39.     //        {
  40.     //            this._Title = value;
  41.     //        }
  42.     //    }
  43.     //}
  44.  
  45.     [Table(Name = "Movie")]
  46.     class Movie
  47.     {
  48.  
  49.         [Column()]
  50.         public int MovieId { get; set; }
  51.  
  52.         [Column()]
  53.         public string Title { get; set; }
  54.  
  55.         [Column()]
  56.         public int Year { get; set; }
  57.  
  58.         [Column()]
  59.         public int Time { get; set; }
  60.  
  61.         [Column()]
  62.         public string Language { get; set; }
  63.  
  64.         [Column()]
  65.         public int Rating { get; set; }
  66.  
  67.         [Column()]
  68.         public decimal Price { get; set; }
  69.  
  70.  
  71.     }
  72.     class Program
  73.     {
  74.         //private static SqlConnection sqlConn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;" +
  75.         //                                                         "AttachDbFilename=C:\\Users\\timothychang\\source\\repos\\CSharp2\\Assignments\\Week9_10\\Final_ChangTimothy\\Final_ChangTimothy\\CineworldEntertainment_ChangTimothy.mdf;" +
  76.         //                                                         "Integrated Security=True;" +
  77.         //                                                         "MultipleActiveResultSets=True");
  78.  
  79.         //DataContext db2 = new DataContext(@"Data Source=(localdb)\v11.0;
  80.         //                                 Integrated Security=true;
  81.         //                                 AttachDbFileName=C:\\Users\\timothychang\\source\\repos\\CSharp2\\Assignments\\Week9_10\\Final_ChangTimothy\\Final_ChangTimothy\\CineworldEntertainment_ChangTimothy.mdf");
  82.  
  83.         //DataContext db = new DataContext(@"Data Source=(localdb)\MSSQLLocalDB;
  84.         //                                 Integrated Security=true;
  85.         //                                 AttachDbFileName=C:\\databases\\CineworldEntertainment_ChangTimothy.mdf");
  86.  
  87.         static void Main(string[] args)
  88.         {
  89.  
  90.             Console.SetWindowSize(130, 30);
  91.  
  92.             //OpenConnection();
  93.             //PrintTable();
  94.  
  95.             Console.WriteLine();
  96.             PrintTableSorted();
  97.  
  98.             //CloseConnection();
  99.  
  100.             Console.WriteLine("\nPress any key to quit...");
  101.             Console.ReadKey();
  102.  
  103.  
  104.         }
  105.  
  106.         //private static void OpenConnection()
  107.         //{
  108.         //    try
  109.         //    {
  110.         //        sqlConn.Open();
  111.         //        Console.WriteLine("Connection Opened");
  112.         //    }
  113.         //    catch (SqlException ex)
  114.         //    {
  115.         //        Console.WriteLine("Error: " + ex.ToString());
  116.         //    }
  117.         //}
  118.  
  119.         //private static void CloseConnection()
  120.         //{
  121.         //    try
  122.         //    {
  123.         //        sqlConn.Close();
  124.         //        Console.WriteLine("Connection Closed");
  125.         //    }
  126.         //    catch (SqlException ex)
  127.         //    {
  128.         //        Console.WriteLine("Error: " + ex.ToString());
  129.         //    }
  130.         //}
  131.  
  132.         //private static void PrintTable()
  133.         //{
  134.         //    try
  135.         //    {
  136.         //        // query
  137.         //        string sql = "SELECT * FROM Movie";
  138.         //        SqlCommand cmd = new SqlCommand(sql, sqlConn);
  139.         //        SqlDataReader dr = cmd.ExecuteReader();
  140.  
  141.         //        // headers as string
  142.         //        string movieId = "Movie ID";
  143.         //        string title = "Title";
  144.         //        string year = "Year";
  145.         //        string time = "Time";
  146.         //        string language = "Language";
  147.         //        string rating = "Rating";
  148.         //        string price = "Price";
  149.  
  150.         //        Console.WriteLine("Cineworld Entertainment POS System");
  151.         //        Console.WriteLine("{0} | {1} | {2} | {3} | {4} | {5} | {6}",
  152.         //                          movieId.PadRight(9), title.PadRight(40), year.PadRight(5),
  153.         //                          time.PadRight(5), language.PadRight(9), rating.PadRight(7),
  154.         //                          price.PadRight(6));
  155.         //        Console.WriteLine("============================================================================" +
  156.         //                          "====================================");
  157.  
  158.         //        // iterate through Movie table and print results
  159.         //        while (dr.Read())
  160.         //        {
  161.         //            Console.WriteLine("{0} | {1} | {2} | {3} | {4} | {5} | {6}",
  162.         //            dr["MovieId"].ToString().PadRight(9),
  163.         //            dr["Title"].ToString().PadRight(40),
  164.         //            dr["Year"].ToString().PadRight(5),
  165.         //            dr["Time"].ToString().PadRight(5),
  166.         //            dr["Language"].ToString().PadRight(9),
  167.         //            dr["Rating"].ToString().PadRight(7),
  168.         //            Convert.ToDecimal(dr["Price"]).ToString("0.00").PadRight(6));
  169.         //        }
  170.         //        dr.Close();
  171.         //    }
  172.         //    catch (SqlException ex)
  173.         //    {
  174.         //        Console.WriteLine("Error: " + ex.ToString());
  175.         //    }
  176.         //}
  177.  
  178.         // linq
  179.         private static void PrintTableSorted()
  180.         {
  181.             //DataContext db = new DataContext(@"Data Source=(localdb)\MSSQLLocalDB;
  182.             //                             Integrated Security=true;
  183.             //                             AttachDbFileName=C:\\Users\\timothychang\\source\\repos\\CSharp2\\Assignments\\Week9_10\\Final_ChangTimothy\\Final_ChangTimothy\\CineworldEntertainment_ChangTimothy.mdf");
  184.             DataContext db = new DataContext(@"C:\databases\CineworldEntertainment_ChangTimothy.mdf");
  185.  
  186.             //DataContext db = new DataContext(@"Server=DESKTOPD3CL53A\SQLEXPRESS;
  187.             //                    Data Source=(localdb)\\MSSQLLocalDB;
  188.  
  189.             //                             Integrated Security=true;
  190.             //                             AttachDbFileName=C:\\databases\\CineworldEntertainment_ChangTimothy.mdf");
  191.             Table<Movie> Movies = db.GetTable<Movie>();
  192.             var movieQuery =
  193.                 from movie in Movies
  194.                 where movie.Title == "Captain Marvel"
  195.                 select movie;
  196.  
  197.             foreach (var movie in movieQuery)
  198.             {
  199.                 Console.WriteLine("ID={0}, City={1}", movie.MovieId,
  200.                     movie.Title);
  201.             }
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement