示例#1
0
        public HttpResponseMessage GetChildById([FromUri] int id, ChildAddRequest payload)
        {
            ChildService childSvc = new ChildService();
            Child        child    = childSvc.GetChildById(id);

            return(Request.CreateResponse(HttpStatusCode.OK, child));
        } //GetChildById
示例#2
0
        public HttpResponseMessage CreateChild(ChildAddRequest payload)
        {
            ItemResponse <int> response = new ItemResponse <int>();
            ChildService       childSvc = new ChildService();

            response.Item = childSvc.CreateChild(payload);
            return(Request.CreateResponse(response));
        } //CreateChild
示例#3
0
        public int CreateChild(ChildAddRequest payload)
        {
            /*
             * 1. You need a connection
             * 2. You need a command (a function)
             *  a. Name
             *  b. Parameters
             * 3. You need to execute that command
             *  a. ExecuteNonQuery ==> public void C#
             *  b. ExeuteReader ==> public List<T> C#
             */
            int id = 0;

            // setting connection string to a variable
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            //establish connection
            using (SqlConnection sqlConn = new SqlConnection(connString))
            {
                // establlish command object
                using (SqlCommand cmd = new SqlCommand("dbo.Child_Insert", sqlConn))
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ChildName", payload.ChildName);
                    cmd.Parameters.AddWithValue("@ChildAgeYears", payload.ChildAgeYears);
                    cmd.Parameters.AddWithValue("@ChildAgeMonths", payload.ChildAgeMonths);

                    SqlParameter param = new SqlParameter();
                    param.ParameterName = "@ID";
                    param.SqlDbType     = System.Data.SqlDbType.Int;
                    param.Direction     = System.Data.ParameterDirection.Output;
                    cmd.Parameters.Add(param);

                    // open SQL connection
                    sqlConn.Open();

                    // for insert or update statements
                    cmd.ExecuteNonQuery();// takes total number of rows that are affected

                    id = (int)cmd.Parameters["@ID"].Value;
                }
            }
            return(id);
        } //CreateChild