예제 #1
0
        static void Main(string[] args)
        {
            var connStr = ConfigurationManager.ConnectionStrings["CustomMapperConnStr"];

            using (var conn = new SqlConnection(connStr.ConnectionString))
            {
                conn.Open();

                var unit1 = new Unit { Name = "USD" };
                new UnitMapper(conn).Save(unit1);

                var unit2 = new Unit { Name = "РУБ", Description = "рубли" };
                new UnitMapper(conn).Save(unit2);

                var allUnits = new UnitMapper(conn).GetAll();

                var unit = new UnitMapper(conn).Get(unit1.Id);
                unit.Description = "$";
                new UnitMapper(conn).Save(unit);

                new UnitMapper(conn).Delete(new Unit {Id = unit2.Id});

                var indicator1 = new Indicator {Title = "Ind1"};
                new IndicatorMapper(conn).Save(indicator1);

                var indicator2 = new Indicator { Title = "Ind2", UnitId = unit1.Id};
                new IndicatorMapper(conn).Save(indicator2);

                try
                {
                    var indicator3 = new Indicator { Title = "Ind3", UnitId = 1000 };
                    new IndicatorMapper(conn).Save(indicator3);
                }
                catch (Exception)
                {
                    //
                }

                var allIndicators = new IndicatorMapper(conn).GetAll();
                var i1 = new IndicatorMapper(conn).Get(indicator1.Id);

                try
                {
                    new UnitMapper(conn).Delete(unit1);
                }
                catch (Exception)
                {
                }

                new IndicatorMapper(conn).Delete(indicator2);
                new UnitMapper(conn).Delete(unit1);

                conn.Close();
            }
        }
예제 #2
0
 public void Save(Unit unit)
 {
     if (unit.Id > 0)
     {
         Update(unit);
     }
     else
     {
         Insert(unit);
     }
 }
예제 #3
0
        static void Main(string[] args)
        {
            var connStr = ConfigurationManager.ConnectionStrings["CustomMapperConnStr"];

            using (var conn = new SqlConnection(connStr.ConnectionString))
            {
                conn.Open();

                var unit1 = new Unit { Name = "USD" };
                new UnitMapper(conn).Save(unit1);

                var unit2 = new Unit { Name = "РУБ", Description = "рубли" };
                new UnitMapper(conn).Save(unit2);

                var allUnits = new UnitMapper(conn).GetAll();

                var unit1_1 = new UnitMapper(conn).Get(unit1.Id);
                var unit1_2 = new UnitMapper(conn).Get(unit1.Id);

                unit1_1.Description = "$";
                new UnitMapper(conn).Save(unit1_1);

                try
                {
                    unit1_2.Description = "$";
                    new UnitMapper(conn).Save(unit1_2);
                }
                catch (Exception ex)
                {
                    //
                }

                new UnitMapper(conn).Delete(unit1_1);

                try
                {
                    new UnitMapper(conn).Delete(new Unit {Id = unit1_2.Id, TimeStamp = unit1_2.TimeStamp});
                }
                catch (Exception ex)
                {
                    //
                }

                conn.Close();
            }
        }
예제 #4
0
        private void Insert(Unit unit)
        {
            using (var cmd = _connection.CreateCommand())
            {
                cmd.CommandText = "insert into Units (Name, Description) values (@Name, @Description);select Id, TimeStamp FROM Units WHERE @@ROWCOUNT > 0 AND Id = scope_identity()";
                addDataParams(unit, cmd);

                using (var reader = cmd.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        throw new Exception("Insert error");
                    }

                    unit.Id = reader.GetInt32(0);
                    unit.TimeStamp = (byte[])reader["Timestamp"];

                    reader.Close();
                }
            }
        }
예제 #5
0
        private void Update(Unit unit)
        {
            using (var cmd = _connection.CreateCommand())
            {
                cmd.CommandText = "update Units set Name = @Name, Description = @Description where Id = @Id and TimeStamp = @TimeStamp;select TimeStamp FROM Units WHERE @@ROWCOUNT > 0 AND Id = @Id";
                addPKParam(unit.Id, cmd);
                addDataParams(unit, cmd);
                addTimeStampParam(unit.TimeStamp, cmd);

                using (var reader = cmd.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        throw new Exception("Update error");
                    }

                    unit.TimeStamp = (byte[])reader["Timestamp"];

                    reader.Close();
                }
            }
        }
예제 #6
0
        private static Unit getDataFromReader(SqlDataReader reader)
        {
            var u = new Unit();

            u.Id = reader.GetInt32(0);
            u.Name = (string)reader["Name"];
            u.Description = reader.IsDBNull(2) ? null : reader.GetString(2);

            u.TimeStamp = (byte[])reader["TimeStamp"];

            return u;
        }
예제 #7
0
 private static void addDataParams(Unit unit, SqlCommand cmd)
 {
     cmd.Parameters.AddWithValue("@Name", unit.Name);
     cmd.Parameters.AddWithValue("@Description", string.IsNullOrEmpty(unit.Description) ? DBNull.Value : (object)unit.Description);
 }
예제 #8
0
        public void Delete(Unit unit)
        {
            using (var cmd = _connection.CreateCommand())
            {
                cmd.CommandText = "delete from Units where Id = @Id and TimeStamp = @Timestamp";
                addPKParam(unit.Id, cmd);
                addTimeStampParam(unit.TimeStamp, cmd);

                if (cmd.ExecuteNonQuery() != 1)
                {
                    throw new Exception("Object not found");
                }
            }
        }