use basicsite
CREATE TABLE [dbo].[t_Pds] (
[PdsId] [int] IDENTITY (1, 1) NOT NULL ,
[Writer] [nvarchar] (50) NOT NULL ,
[Title] [nvarchar] (200) NOT NULL ,
[Content] [text] NOT NULL ,
[FileName] [nvarchar] (100) NOT NULL ,
[ReadCount] [int] NOT NULL ,
[DownCount] [int] NOT NULL ,
[RegDate] [datetime] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
ALTER TABLE [dbo].[t_Pds] ADD
CONSTRAINT [PK_t_Pds] PRIMARY KEY CLUSTERED
(
[PdsId]
) ON [PRIMARY]
GO
추가
#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;
}
}
}
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_Write : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ibOk_Click(object sender, ImageClickEventArgs e)
{
PDS pds = new PDS();
if (FileUpload1.HasFile) // 파일업로드 파일이 존재하면
{
string upDir = Server.MapPath(ConfigurationManager.AppSettings["filePath"]);
string fifleName = pds.SaveFile(FileUpload1, upDir);
// 게시물 저장
pds.InsertConent(User.Identity.Name, txtTitle.Text.Trim(), txtContent.Text, fifleName);
Library.Alert(Response, "자료가 저장되었습니다", "pds_list.aspx");
}
else
{
Library.Alert(Response, "파일이 없습니다");
}
}
}