Blog Content

    티스토리 뷰

    11.ASP.NET - 자료실 게시판(4) : Biz

    반응형
    App_Code - Bsl - UploadBiz.cs


    using System;

    using System.Collections.Generic;

    using System.Data;

    using System.Transactions; // 참조추가 : System.Transactions.dll

     

    // 비스니스 객체. 트랜잭션 처리

    public class UploadBiz

    {

        // 입력 메서드 : Write

        public int WriteUpload(

            string name, string email, string title, string postIp, string content

            , string password, string encoding, string homepage, string fileName, int fileSize)

        {

            // using 구문에 있는 여러개 메서드들을 트랜잭션으로 처리

            // 첫번째 메서드 처리 두번째 메서드에서 에러가 발생하면,

            // 첫번째 메서드도 롤백시킨다.

            int result = 0;

     

            using (TransactionScope scope =

                      new TransactionScope(TransactionScopeOption.RequiresNew))

            {

                UploadDac ud = new UploadDac();

     

                result = ud.AddUpload(

                    name, email, title, postIp, content, password, encoding,

                    homepage, fileName, fileSize);

     

                scope.Complete(); //

            }

            return result; // UI영역으로 반환

        }


        // 출력 메서드 : List

        //데이터셋으로 받아 데이터셋으로 넘김

        public DataSet ListUpload()

        {

            return (new UploadDac()).GetUploads();

        }

        // 백업 메서드

        //public void BackupUpload()

        //{

        //    UploadDac ud = new UploadDac();

        //    ud.GetUploads().WriteXml("Backup.xml");

        //}

     

        // 상세 메서드 : View

        public UploadEntity ViewUpload(int num) {

            UploadEntity ue = new UploadEntity();  //객체 생성

            using (IDataReader dr = (new UploadDac()).GetUploadByNum(num)) {//DB연동

                while (dr.Read()) {

                    ue.Num = num;

                    ue.Name = dr["Name"].ToString(); // 문자열 인덱서

                    ue.Email = dr.IsDBNull(2) ? "" : dr[2].ToString(); //dr[2].ToString(); 정수형 인덱서

                    ue.Title = dr.GetString(3); // GetXXX() 메서드

                    ue.PostDate = dr.GetDateTime(4); // GetXXX() 메서드

                    ue.PostIP = dr["PostIP"].ToString();

                    ue.Content = dr[6].ToString();

                    ue.Password = dr.GetString(7);

                    ue.ReadCount = dr.GetInt32(8);

                    ue.Encoding = dr.GetString(9);

     

                    //null 가능한 속성들이기 때문에 null 값이 아닌 경우 데이터를 읽어옴

                    if (dr["Homepage"] != null) {

                        ue.Homepage = dr["Homepage"].ToString();

                    }

                    ue.ModifyDate = dr.IsDBNull(11) ?

                                                   DateTime.Now : Convert.ToDateTime(dr[11].ToString());

                    if (dr[12] != null) {

                        ue.ModifyIP = dr["ModifyIP"].ToString();

                    }

                    if (dr[13] != null) {

                        ue.FileName = dr.GetString(13);

                    }

                    ue.FileSize = Convert.ToInt32(dr["FileSize"]); //[1] Convert 사용 형변환

                    ue.DownCount = int.Parse(dr["DownCount"].ToString()); //[2] Parse 사용 형변환

                }   

            }

            return ue;

        }

     

        // 수정 메서드 : Modify

        public int ModifyUpload(UploadEntity entity) //엔티티는 전체 레이어에서 사용

        {

            //Modify.cs 클래스에서 UpdataUpload() 사용 : Delete 저장 프로시저 실행

            UploadDac ud = new UploadDac();

            return ud.UpdateUpload(entity);

        }

     

        // 삭제 메서드 : Delete

        public int DeleteUpload(int num, string password)

        {

            /*

            UploadDac ud = new UploadDac();

            return ud.DeleteUpload(num, password);

          */

     

            // 표현

            return (new UploadDac()).DeleteUpload(num, password);

        }

     

        // 검색 메서드 : Search

        //데이터셋으로 받아서 리스트제네릭으로 넘기기. 시간은 오래 걸리지만 세련된 방법

        public List<UploadEntity> SearchUpload(string searchField, string searchQuery)

        {

            List<UploadEntity> lst = new List<UploadEntity>(); // 컬렉션

            UploadEntity entity = new UploadEntity(); // 한개 레코드

            UploadDac ud = new UploadDac();

           

            using (IDataReader dr = ud.GetUploadsByWord(searchField, searchQuery))

            {

                while (dr.Read())

                {

                    #region 레코드 가져오기

                                       entity.Num = Convert.ToInt32(dr["Num"]);

                    entity.Name = dr["Name"].ToString(); //[1] 문자열 인덱서

                    entity.Email = dr.IsDBNull(2) ? "" : dr[2].ToString();//dr[2].ToString 정수형인덱서

                    entity.Title = dr.GetString(3); //[3]GetXXX() 메서드

                    entity.PostDate = dr.GetDateTime(4); //GetXXX() 메서드

                    entity.PostIP = dr["PostIP"].ToString();

                    entity.Content = dr[6].ToString();

                    entity.Password = dr.GetString(7);

                    entity.ReadCount = dr.GetInt32(8);

                    entity.Encoding = dr.GetString(9);

                    if (dr["Homepage"] != null) {

                        entity.Homepage = dr["Homepage"].ToString();

                    }

     

                    // 널값 처리해서 날짜값 가져오기

                    if (dr.IsDBNull(11))

                    {

                        entity.ModifyDate = DateTime.Now;

                    }

                    else

                    {

                        entity.ModifyDate = dr.GetDateTime(11);

                    }

     

                    if (dr[12] != null) {

                        entity.ModifyIP = dr["ModifyIP"].ToString();

                    }

                    if (dr[13] != null) {

                        entity.FileName = dr.GetString(13);

                    }

                    entity.FileSize = Convert.ToInt32(dr["FileSize"]);

                    entity.DownCount = int.Parse(dr["DownCount"].ToString());

                           #endregion           

     

                    lst.Add(entity); // 한개레코드 담기

                }   

            }

     

            return lst; // 전체 담겨진 리스트제네릭클래스(레코드셋) 반환

        }

     

        // 다운 메서드

        public void UpdateDownCount(string fileName)

        {

            // Biz단에서 Dac 호출

            using (TransactionScope scope =

                      new TransactionScope(TransactionScopeOption.RequiresNew))

            {

                (new UploadDac()).UpdateDownCount(fileName);        

                scope.Complete();

            }

        }

    }




    반응형

    Comments