public void WriteData(List <Name> sortedNames)
        {
            try
            {
                string sql = $"Insert Into SortedName ([FirstName], [LastName]) values";

                foreach (Name name in sortedNames)
                {
                    sql += $" ('{name._firstName}' , '{name._lastName}'),";
                }
                sql  = sql.TrimEnd(',');
                sql += ";";

                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.InsertCommand = _personDataContext.ExecuteQuery(sql);
                adapter.InsertCommand.ExecuteNonQuery();

                _personDataContext.CloseContext();
                adapter.Dispose();
            }

            catch (Exception e)
            {
                Console.WriteLine($"{e}");
            }
        }
Beispiel #2
0
        public void ReadData(List <Name> unsortedNames)
        {
            try
            {
                string        sql        = "Select FirstName, LastName from UnsortedName";
                SqlDataReader dataReader = _personDataContext.ExecuteQuery(sql).ExecuteReader();
                while (dataReader.Read())
                {
                    unsortedNames.Add(new Name(dataReader.GetString(0), dataReader.GetString(1)));
                }

                _personDataContext.CloseContext();
                dataReader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }