コード例 #1
0
        public void NomalTest()
        {
            var dt = new DbTable
            {
                Columns = new[] { "Id", "Name", "CreateTime" },
                Rows    = new List <Object[]>
                {
                    new Object[] { 123, "Stone", DateTime.Now },
                    new Object[] { 456, "NewLife", DateTime.Today }
                }
            };

            Assert.Equal(123, dt.Get <Int32>(0, "Id"));
            Assert.Equal(456, dt.Get <Int32>(1, "ID"));

            Assert.Equal("NewLife", dt.Get <String>(1, "Name"));
            Assert.Equal(DateTime.Today, dt.Get <DateTime>(1, "CreateTime"));

            // 不存在的字段
            Assert.Equal(DateTime.MinValue, dt.Get <DateTime>(0, "Time"));

            Assert.False(dt.TryGet <DateTime>(1, "Time", out var time));

            var idx = dt.GetColumn("Name");

            Assert.Equal(1, idx);

            idx = dt.GetColumn("Time");
            Assert.Equal(-1, idx);

            // 迭代
            var i = 0;

            foreach (var row in dt)
            {
                if (i == 0)
                {
                    Assert.Equal(123, row["ID"]);
                    Assert.Equal("Stone", row["name"]);
                }
                else if (i == 1)
                {
                    Assert.Equal(456, row["ID"]);
                    Assert.Equal("NewLife", row["name"]);
                    Assert.Equal(DateTime.Today, row["CreateTime"]);
                }
                i++;
            }
        }
コード例 #2
0
        protected override HttpResponseMessage DoGet(int id)
        {
            HttpResponseMessage response = null;

            try
            {
                var             record = tblData.Get(id);
                ApiDataResponse res    = new ApiDataResponse();
                res.data            = record;
                res.recordsaffected = 1;
                response            = Request.CreateResponse <ApiDataResponse>(HttpStatusCode.OK, res);
            }
            catch (Exception ex)
            {
                response = ExceptionHandler(ex);
            }

            return(response);
        }
コード例 #3
0
        /// <summary>
        /// Executa um GET (SELECT) por ID do registo
        /// </summary>
        /// <typeparam name="T">Modelo da Tabela</typeparam>
        /// <param name="id">ID do registo</param>
        /// <returns>HTTP Response com o registo</returns>
        public virtual HttpResponseMessage GenericGet <T>(int id) where T : class
        {
            DbTable <T> tblData = new DbTable <T>(DBContext, UserID);

            try
            {
                var             record = tblData.Get(id);
                ApiDataResponse res    = new ApiDataResponse();
                res.data            = record;
                res.recordsaffected = 1;
                tblData             = null;

                return(Controller.Request.CreateResponse <ApiDataResponse>(HttpStatusCode.OK, res));
            }
            catch (Exception ex)
            {
                tblData = null;
                return(DefaultControllerException(Controller.Request, ex));
            }
        }
コード例 #4
0
        /// <summary>
        /// Apaga um registo da BD com base no id do registo
        /// </summary>
        /// <typeparam name="T">Modelo da tabela</typeparam>
        /// <param name="Id">ID do registo</param>
        /// <returns></returns>
        public HttpResponseMessage GenericDelete <T>(int Id) where T : class
        {
            HttpResponseMessage response;

            DbTable <T> tblData = new DbTable <T>(DBContext, UserID);

            try
            {
                var item = tblData.Get(Id);
                tblData.Delete(item);
                response = Controller.Request.CreateResponse(HttpStatusCode.NoContent);
            }
            catch (Exception ex)
            {
                response = DefaultControllerException(Controller.Request, ex);
            }

            tblData = null;
            return(response);
        }