ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • PDS 게시만 만들기 #2
    .NET/ASP.NET 2009. 1. 9. 10:27
    반응형







    PDS_view.aspx





    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class DataBank_PDS_View : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    PDS pds = new PDS();
    if (!IsPostBack)
    {
    //PDSId 값 읽어옴
    string pdsId = Request.QueryString["PdsId"];
    //조회수 1 증가
    pds.IncreaseReadCount(pdsId);
    //작성자와 동일인이 아니면 삭제 , 수정버튼 안보이게
    if (pds.IsWriter(pdsId,User.Identity.Name))
    {
    ibEdit.Visible = false;
    ibDelete.Visible = false;
    }
    //내용 읽어옴
    pds.GetContent(pdsId);
    // pds객체 변수안의 값들
    lblTitle.Text = pds.Title;
    lblWriter.Text = pds.Writer;
    lblRegDate.Text = pds.RegDate.ToString();
    lblReadCount.Text = pds.ReadCount.ToString();
    lbFile.Text =pds.FileName;
    lblDownCount.Text = pds.DownCount.ToString();
    lblContent.Text = pds.Content.Replace("\r\n","<br/>");
    }
    }
    protected void ibEdit_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void ibDelete_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void lbFile_Click(object sender, EventArgs e)
    {

    }
    }






    app_date PDS.cs


    #region ##. 네임스페이스
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    using System.Data.SqlClient;
    using System.IO;

    #endregion
    /// <summary>
    /// PDS의 요약 설명입니다.
    /// </summary>

    public class PDS
    {
    public PDS() { }

    #region ##. 전역변수
    private string _writer = string.Empty;
    private string _title = string.Empty;
    private string _content = string.Empty;
    private string _fileName = string.Empty;
    private int _readCount = 0;
    private int _downCount = 0;
    DateTime _regDate = System.DateTime.Now;
    #endregion

    #region ##. 전용 속성(프로퍼티)
    public string Writer
    { get { return _writer; } set { _writer = value; } }
    public string Title
    { get { return _title; } set { _title = value; } }
    public string Content
    { get { return _content; } set { _content = value; } }
    public string FileName
    { get { return _fileName; } set { _fileName = value; } }
    public int ReadCount
    { get { return _readCount; } set { _readCount = value; } }
    public int DownCount
    { get { return _downCount; } set { _downCount = value; } }
    public DateTime RegDate
    { get { return _regDate; } set { _regDate = value; } }
    #endregion

    /// <summary>
    /// 자료실 데이터 입력 메서드
    /// </summary>
    /// <param name="writer">작성자</param>
    /// <param name="title">제목</param>
    /// <param name="content">내용</param>
    /// <param name="fileName">저장된파일</param>
    public void InsertConent(string writer, string title, string content, string fileName)
    {
    SqlConnection conn =
    new SqlConnection(ConfigurationManager.ConnectionStrings["DBconStr"].ConnectionString);

    string strInsQry = "INSERT INTO t_pds " +
      "(Writer, Title, Content, FileName, ReadCount, DownCount, RegDate) " +
      "VALUES " +
      "(@Writer, @Title, @Content, @FileName, 0, 0, getDate()) ";

    SqlCommand cmd = new SqlCommand(strInsQry, conn);
    cmd.Parameters.AddWithValue("@Writer", writer);
    cmd.Parameters.AddWithValue("@Title", title);
    cmd.Parameters.AddWithValue("@Content", content);
    cmd.Parameters.AddWithValue("@FileName", fileName);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();


    /// <summary>
    /// 파일저장 메서드
    /// </summary>
    /// <param name="fUpload">업로드클래스</param>
    /// <param name="upDir">업로드 디렉토리</param>
    /// <returns>실제 저장된 파일명</returns>
    public string SaveFile(FileUpload fUpload , string upDir)
    {
    DirectoryInfo di = new DirectoryInfo(upDir);
    if (!di.Exists)
    di.Create();
    string fileName = fUpload.FileName;
    string fFullName = upDir + fileName;
    string newfileName = string.Empty;
    FileInfo fi = new FileInfo(fFullName);
    if (fi.Exists) //동일한 파일이 존재하면
    {
    int fIndex = 0;
    string fExtention = fi.Extension; // 확자자때냄
    string fRealName = fileName.Replace(fExtention,"");

    do
    {
    fIndex++;
    newfileName = fRealName + "_" + fIndex.ToString() + fExtention;
    fi = new FileInfo(upDir + newfileName);
    } while (fi.Exists);
    fFullName = upDir + newfileName;
    fUpload.PostedFile.SaveAs(fFullName);
    return newfileName;
    }
    else
    {
       
    fUpload.PostedFile.SaveAs(fFullName);
    return fileName;
    }
    }

    /// <summary>
    /// 게시물의 조회수올리는 메서드
    /// </summary>
    /// <param name="pdsId">자료실 번호</param>
    public void IncreaseReadCount(string pdsId)
    {
    SqlConnection conn =
    new SqlConnection(ConfigurationManager.ConnectionStrings["DBconStr"].ConnectionString);

    string strUpQry = "UPDATE t_Pds SET " +
      "ReadCount = ReadCount + 1 " +
      "WHERE PdsId = @PdsId ";
    SqlCommand cmd = new SqlCommand(strUpQry, conn);
    cmd.Parameters.Add("@PdsId", SqlDbType.Int).Value = Convert.ToInt32(pdsId);

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    }

    /// <summary>
    /// 작성자와 동일인인가를 확인하는 메서드
    /// </summary>
    /// <param name="pdsId">자료실번호</param>
    /// <param name="userId">사용자아이디</param>
    /// <returns>true , false</returns>
    public bool IsWriter(string pdsId, string userId)
    {
    SqlConnection conn =
    new SqlConnection(ConfigurationManager.ConnectionStrings["DBconStr"].ConnectionString);

    string strCntQry = "SELECT COUNT(*) AS cnt " +
      "FROM t_Pds " +
      "WHERE PdsId = @PdsId " +
      "AND Writer = @Writer ";
    SqlCommand cmd = new SqlCommand(strCntQry, conn);
    cmd.Parameters.Add("@PdsId", SqlDbType.Int).Value = Convert.ToInt32(pdsId);
    cmd.Parameters.Add("@Writer", SqlDbType.NVarChar).Value = userId;

    conn.Open();
    SqlDataReader read = cmd.ExecuteReader();
    int ICnt = 0;
    if (read.Read())
    ICnt = Convert.ToInt32(read["cnt"]);

    read.Close();
    conn.Close();

    if (ICnt >= 1)
    return true;
    else
    return false;
    }

    /// <summary>
    /// 자료실 상세보기 메서드
    /// </summary>
    /// <param name="pdsId">자료실 번호</param>
    public void GetContent(string pdsId)
    {
    SqlConnection conn =
      new SqlConnection(ConfigurationManager.ConnectionStrings["DBconStr"].ConnectionString);
    string sqlSelQry = "SELECT Writer,Title,Content,FileName,ReadCount,DownCount,RegDate "+
      "FROM t_Pds " +
      "WHERE PdsId = @PdsId ";
      
    SqlCommand cmd = new SqlCommand(sqlSelQry, conn);
    cmd.Parameters.Add("@PdsId", SqlDbType.Int).Value = Convert.ToInt32(pdsId);
    conn.Open();
    SqlDataReader read = cmd.ExecuteReader();
    if (read.Read())
    {
    this.Writer = read["Writer"].ToString();
    this.Title = read["Title"].ToString();
    this.Content = read["Content"].ToString();
    this.FileName = read["FileName"].ToString();
    this.ReadCount = Convert.ToInt32(read["ReadCount"]);
    this.DownCount = Convert.ToInt32(read["DownCount"]);
    this.RegDate = Convert.ToDateTime(read["RegDate"]);
    }
    read.Close();
    conn.Close();

    }
    }











    반응형

    댓글

Designed by Tistory.