예제 #1
0
        public static void DbPush(RabbitStatistic rs)
        {
            //_context.Statistic.Add(rs);
            //_context.SaveChanges();
            string connectionString = "Server=(localdb)\\mssqllocaldb;Database=Statistic57;Trusted_Connection=True;MultipleActiveResultSets=true";
            string query            = string.Format(("INSERT INTO Statistic (Action, Client, PageName, Result, TimeStamp, [User]) " +
                                                     "VALUES (@Action, @Client, @PageName, @Result, @TimeStamp, @User)"));

            // create connection and command
            using (SqlConnection cn = new SqlConnection(connectionString))
                using (SqlCommand cmd = new SqlCommand(query, cn))
                {
                    // define parameters and their values
                    //cmd.Parameters.AddWithValue("Action", rs.Action);
                    //cmd.Parameters.AddWithValue("Client", rs.Client);
                    //cmd.Parameters.AddWithValue("PageName", rs.PageName);
                    //cmd.Parameters.AddWithValue("Result", rs.Result);
                    //cmd.Parameters.AddWithValue("TimeStamp", rs.TimeStamp);

                    cmd.Parameters.Add("Action", SqlDbType.NVarChar).Value     = rs.Action;
                    cmd.Parameters.Add("Client", SqlDbType.NVarChar).Value     = rs.Client;
                    cmd.Parameters.Add("PageName", SqlDbType.NVarChar).Value   = rs.PageName;
                    cmd.Parameters.Add("Result", SqlDbType.Bit).Value          = rs.Result;
                    cmd.Parameters.Add("TimeStamp", SqlDbType.DateTime2).Value = rs.TimeStamp;
                    if (rs.User != null)
                    {
                        cmd.Parameters.Add("User", SqlDbType.NVarChar).Value = rs.User;
                    }

                    // open connection, execute INSERT, close connection
                    cn.Open();
                    cmd.ExecuteNonQuery();
                    cn.Close();
                }
        }
예제 #2
0
        public async Task <IActionResult> PutRabbitStatistic([FromRoute] int id, [FromBody] RabbitStatistic rabbitStatistic)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != rabbitStatistic.ID)
            {
                return(BadRequest());
            }

            _context.Entry(rabbitStatistic).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RabbitStatisticExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #3
0
        public async Task <IActionResult> PostRabbitStatistic([FromBody] RabbitStatistic rabbitStatistic)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Statistic.Add(rabbitStatistic);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetRabbitStatistic", new { id = rabbitStatistic.ID }, rabbitStatistic));
        }