コード例 #1
0
        public AmigosMd GetById(int RowId)
        {
            AmigosMd Amigo = new AmigosMd();

            try
            {
                string Sql = "Select * from Amigos where RowId = " + $"{ RowId}";

                using SqlConnection cn  = new SqlConnection(CnnString);
                using SqlCommand cmd    = new SqlCommand(Sql, cn);
                using SqlDataAdapter da = new SqlDataAdapter(cmd);
                using DataTable dt      = new DataTable();
                {
                    cn.Open(); da.Fill(dt); cn.Close();
                    if (dt.Rows.Count > 0)
                    {
                        Amigo.Nome    = (string)dt.Rows[0]["Nome"];
                        Amigo.Address = (string)dt.Rows[0]["Address"];
                        Amigo.Phone   = (string)dt.Rows[0]["Phone"];
                        Amigo.Email   = (string)dt.Rows[0]["Email"];
                        Amigo.RowId   = (int)dt.Rows[0]["RowId"];
                    }
                }
            } catch (Exception ex) { throw ex; }
            return(Amigo);
        }
コード例 #2
0
        public List <AmigosMd> Listar(string sort)
        {
            DataTable       dt     = new DataTable();
            List <AmigosMd> Amigos = new List <AmigosMd>();

            try
            {
                string Sql = "Select * from Amigos" + $" order by {sort}";

                using SqlConnection cn  = new SqlConnection(CnnString);
                using SqlCommand cmd    = new SqlCommand(Sql, cn);
                using SqlDataAdapter da = new SqlDataAdapter(cmd);
                cn.Open(); da.Fill(dt); cn.Close();

                //  Move data from table to list
                foreach (DataRow dr in dt.Rows)
                {
                    AmigosMd A = new AmigosMd()
                    {
                        Nome    = (string)dr["Nome"],
                        Address = (string)dr["Address"],
                        Phone   = (string)dr["Phone"],
                        Email   = (string)dr["Email"],
                        RowId   = (int)dr["RowId"]
                    };
                    Amigos.Add(A);
                }
            }
            catch (Exception ex) { throw ex; }

            return(Amigos);
        }
コード例 #3
0
        public CodeMsg Put(int id, [FromBody] AmigosMd amg)
        {
            CodeMsg cm;

            try
            {
                AmigosDal am = new AmigosDal();
                cm = am.Incluir(amg);
            } catch (Exception) { throw; }
            return(cm);
        }
コード例 #4
0
        public CodeMsg Incluir(AmigosMd Amg)
        {
            CodeMsg cm = new CodeMsg();

            try
            {
                string sql = "Insert into Amigos";
                sql += "  (        Nome,          Address,          Email,          Phone  ) values ";
                sql += $" ( '{@Amg.Nome}', '{@Amg.Address}', '{@Amg.Email}', '{@Amg.Phone}')";

                using SqlConnection cn = new SqlConnection(CnnString);
                using SqlCommand cmd   = new SqlCommand(sql, cn);
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();

                cm.ErrCode = 0;
                cm.ErrMsg  = $"Registro { Amg.Nome} gravado";
            } catch (Exception) { throw; }
            return(cm);
        }