Esempio n. 1
0
        /// <summary>
        /// Gets data from database with Dapper
        /// </summary>
        /// <returns>Data string</returns>
        public static void GetData()
        {
            try
            {
                var sw = new Stopwatch();

                Console.WriteLine("--Fetching data!");

                sw.Start();

                RandomObjectModel result = null;

                using (var conn = new SqlConnection(ConnectionString))
                {
                    result = conn.Query <RandomObjectModel>(@"SELECT TOP(1) RandomObjectID, RandomString, RandomDateTimeOffset, RandomInt, SeedId
                                                             FROM RandomObject
                                                             ORDER BY RandomDateTimeOffset DESC").FirstOrDefault();
                }

                sw.Stop();

                Console.WriteLine("--Result was: " + result.RandomObjectID + ", " + result.RandomString + ", " + result.RandomDateTimeOffset + ", " + result.RandomInt + ", " + result.RandoSeedId);
                Console.WriteLine("--Time Elapsed: " + sw.Elapsed + "\n");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured: " + ex);
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets data from database with ADO.Net
        /// </summary>
        public static void GetData()
        {
            try
            {
                var sw = new Stopwatch();

                Console.WriteLine("--Fetching data!");

                sw.Start();

                RandomObjectModel result = null;

                using (var conn = new SqlConnection(ConnectionString))
                {
                    using (SqlCommand cmd = new SqlCommand(@"SELECT TOP(1) RandomObjectID, RandomString, RandomDateTimeOffset, RandomInt, SeedId
                                                             FROM RandomObject 
                                                             ORDER BY RandomDateTimeOffset DESC", conn))
                    {
                        conn.Open();
                        SqlDataReader reader = cmd.ExecuteReader();

                        while (reader.Read())
                        {
                            result = new RandomObjectModel
                            {
                                RandomObjectID       = (int)reader["RandomObjectID"],
                                RandomString         = (string)reader["RandomString"],
                                RandomDateTimeOffset = (DateTimeOffset)reader["RandomDateTimeOffset"],
                                RandomInt            = (int)reader["RandomInt"],
                                SeedId = (int)reader["SeedId"]
                            };
                        }
                        conn.Close();
                    }
                }

                sw.Stop();

                Console.WriteLine("--Result was: " + result.RandomObjectID + ", " + result.RandomString + ", " + result.RandomDateTimeOffset + ", " + result.RandomInt + ", " + result.SeedId);
                Console.WriteLine("--Time Elapsed: " + sw.Elapsed + "\n");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured: " + ex);
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
            }
        }
        /// <summary>
        /// Gets data from database with EntityFramework
        /// </summary>
        /// <returns></returns>
        public static void GetData()
        {
            try
            {
                var sw = new Stopwatch();

                Console.WriteLine("--Fetching data!");

                sw.Start();

                RandomObjectModel result = null;

                using (var db = new ThesisEntities())
                {
                    result = (from ro in db.RandomObject
                              orderby ro.RandomDateTimeOffset descending
                              select new RandomObjectModel
                    {
                        RandomObjectID = ro.RandomObjectID,
                        RandomString = ro.RandomString,
                        RandomDateTimeOffset = ro.RandomDateTimeOffset,
                        RandomInt = ro.RandomInt,
                        SeedId = ro.SeedId
                    }).FirstOrDefault();
                }

                sw.Stop();

                Console.WriteLine("--Result was: " + result.RandomObjectID + ", " + result.RandomString + ", " + result.RandomDateTimeOffset + ", " + result.RandomInt + ", " + result.SeedId);
                Console.WriteLine("--Time Elapsed: " + sw.Elapsed + "\n");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured: " + ex);
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
            }
        }