반응형
 //사진등록 콤보(클릭이벤트)
private void btnADD_Click(object sender, System.EventArgs e)
{

openFile.DefaultExt = "gif"; //다이얼로그 박스 확장자 gif만을 불러오기
openFile.Filter = "Graphics interchange Format (*.gif)|*.gif";
openFile.ShowDialog();

if( openFile.FileNames.Length > 0 )
{
pictureBox1.Image = Image.FromFile // 우선 픽쳐박스에 뿌려준다..
(openFile.FileNames[0]);
}
}

//세이브 버튼을 클릭하셔서 클릭 이벤트를 생성하시고 Save()를 호출합니다.

private void Save()
{
try
{
object strResult = pictureBox1.Image;

//sql 연결 컨넥트
SqlConnection con = new SqlConnection("Server=111.111.111.111;database=testdb;Password=test;User ID=sa;Initial Catalog=dbTableName");
SqlDataAdapter da = new SqlDataAdapter("select IMG from image", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("image");

da.MissingSchemaAction = MissingSchemaAction.AddWithKey;


FileStream fs = new FileStream(openFile.FileNames[0] , FileMode.OpenOrCreate, FileAccess.Read);

byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));

fs.Close();

da.Fill(ds,"image");

DataRow myRow;
myRow=ds.Tables["image"].NewRow();

myRow["IMG"] = MyData;
ds.Tables["image"].Rows.Add(myRow);
da.Update(ds, "image");

con.Close();
MessageBox.Show("정상적으로 처리되었습니다..","저장되었습니다.",MessageBoxButtons.OK, MessageBoxIcon.Warning);

}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}

//폼로드

private void InitializeForm()
{
try
{
string strConn = "User ID=sa;Password=park0515;database=image";
StringBuilder sbSql = new StringBuilder();
DataTable dt = new DataTable();

sbSql.Append("SELECT TOP 1 img FROM IMAGE");

SqlConnection SqlConn = new SqlConnection(strConn);
SqlConn.Open();

SqlCommand cmd = new SqlCommand(sbSql.ToString(),SqlConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

byte[] MyData = null;
MyData = (byte[])dt.Rows[0][0];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
FileStream fs = new FileStream("tmp.gif", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize+1);
fs.Close();
picPHOTO.Image = new Bitmap("tmp.gif");


}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}

이런식으로 하시면 되요..

1. testdb로 생성

2. 테이블명을 dbTableName 명시

3. 데이터 형식을 이미지로 img 표시

그럼 테스트 고고고!!!

반응형

+ Recent posts