/// <summary> /// Insert data to table with ADO.Net /// </summary> /// <param name="seed">Randomnumber generators seed</param> public static void InsertData(int seedID, int rows) { try { var sw = new Stopwatch(); Console.WriteLine("--Inserting data!"); sw.Start(); var seed = 0; var result = false; var randomObjectList = new List <RandomObjectModel>(); using (var conn = new SqlConnection(ConnectionString)) { try { using (SqlCommand cmd = new SqlCommand("SELECT SeedValue FROM Seed WHERE SeedID = @ID", conn)) { cmd.Parameters.Add("@ID", SqlDbType.Int).Value = seedID; conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { seed = (int)reader["SeedValue"]; } conn.Close(); } } catch (Exception ex) { Console.WriteLine("Error when fetching Seed!"); throw ex; } Console.WriteLine("--Seed is : " + seed); var gen = new Random(seed); var i = 0; while (i < rows) { using (SqlCommand cmd = new SqlCommand(@"INSERT INTO RandomObject([RandomObjectID], [RandomString], [RandomDateTimeOffset], [RandomInt], [SeedId]) Values(@RandomObjectID, @RandomString, @RandomDateTimeOffset, @RandomInt, @SeedId)", conn)) { cmd.Parameters.Add("@RandomObjectID", SqlDbType.Int).Value = i; cmd.Parameters.Add("@RandomString", SqlDbType.NVarChar).Value = RandomStringGenerator.RandomString(gen, 15); cmd.Parameters.Add("@RandomDateTimeOffset", SqlDbType.DateTimeOffset).Value = RandomDateTimeOffsetGenerator.RandomDateTimeOffset(gen); cmd.Parameters.Add("@RandomInt", SqlDbType.Int).Value = gen.Next(); cmd.Parameters.Add("@SeedId", SqlDbType.Int).Value = seedID; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); i++; } } result = true; sw.Stop(); } Console.WriteLine("--Result was: " + result); 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> /// Insert data to table with Dapper /// </summary> /// <param name="seed">Randomnumber generators seed</param> public static void InsertData(int seedID, int rows) { try { var sw = new Stopwatch(); Console.WriteLine("--Inserting data!"); sw.Start(); var i = 0; var result = false; var seed = 0; using (var conn = new SqlConnection(ConnectionString)) { try { seed = conn.Query <int>("SELECT SeedValue FROM Seed WHERE SeedID = @ID", new { ID = seedID }).FirstOrDefault(); } catch (Exception ex) { Console.WriteLine("Error when fetching Seed!"); throw ex; } Console.WriteLine("--Seed is : " + seed); var gen = new Random(seed); while (i < rows) { conn.Execute(@"INSERT INTO RandomObject([RandomObjectID], [RandomString], [RandomDateTimeOffset], [RandomInt], [SeedId]) Values(@RandomObjectID, @RandomString, @RandomDateTimeOffset, @RandomInt, @SeedId)", new { RandomObjectID = i, RandomString = RandomStringGenerator.RandomString(gen, 15), RandomDateTimeOffset = RandomDateTimeOffsetGenerator.RandomDateTimeOffset(gen), RandomInt = gen.Next(), SeedId = seedID }); i++; } result = true; sw.Stop(); } Console.WriteLine("--Result was: " + result); Console.WriteLine("--Time Elapsed: " + sw.Elapsed + "\n"); } catch (Exception ex) { Console.WriteLine("Exception occured: " + ex); Console.WriteLine("Press any key to continue."); Console.ReadKey(); } }