Ejemplo n.º 1
0
        //This will get some post data by using post ID.
        public NewPost GetPost(int id)
        {
            string        conString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=OnlineWebSite;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            SqlConnection con       = new SqlConnection(conString);

            con.Open();
            string query = $"select * from Posts " +
                           $"where Id = @u";
            SqlParameter p1  = new SqlParameter("u", id);
            SqlCommand   cmd = new SqlCommand(query, con);

            cmd.Parameters.Add(p1);
            SqlDataReader dr = cmd.ExecuteReader();
            NewPost       p  = new NewPost();

            if (dr.HasRows)
            {
                if (dr.Read())
                {
                    p.title   = dr["title"].ToString();
                    p.content = dr["content"].ToString();
                }
            }
            con.Close();
            return(p);
        }
Ejemplo n.º 2
0
        //This will Update the Post into the DBMS.
        public void UpdatePost(int id, NewPost p)
        {
            string        conString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=OnlineWebSite;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            SqlConnection con       = new SqlConnection(conString);

            con.Open();
            string query = $"update Posts set title = @t, content = @c " +
                           $"where Id = @i";
            SqlParameter p1  = new SqlParameter("i", id);
            SqlParameter p2  = new SqlParameter("t", p.title);
            SqlParameter p3  = new SqlParameter("c", p.content);
            SqlCommand   cmd = new SqlCommand(query, con);

            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);
            cmd.Parameters.Add(p3);
            int status = cmd.ExecuteNonQuery();

            con.Close();
        }