728x90

[DB]

테이블의 데이터형식은 Image 로 설정합니다.

 

ex) 테이블

[Name] [varchar](2) NOT NULL
[imageName] [image] NOT NULL

 

-------------------------------------------------

 

[C#]

pickQ = PictureBox 이름

 

<저장할 이미지 파일을 불러와 ​PictureBox 에 보여줍니다.>

OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
pickQ.Image = new Bitmap(ofd.FileName);
pickQ.Tag = ofd.FileName;
}

 

< 저장하기>

FileStream fs = new FileStream(pickInstall.Tag.ToString(), FileMode.Open, FileAccess.Read);
byte[] bImage = new byte[fs.Length];
fs.Read(bImage, 0, (int)fs.Length);

 

strSQL = "INSERT INTO InstallInfo ";
strSQL += "(";
strSQL += "Name, imageName";
strSQL += ")";
strSQL += "values (";
strSQL += "'1번',";
strSQL += "@ImgName)";

 

cmd = new SqlCommand(strSQL, Con);
cmd.Parameters.AddWithValue("@ImgName", bImage);
aRS = cmd.ExecuteReader();
fs.Close();
aRS.Close();

 

 

<저장된 이미지 파일 select 해서 PictureBox에 보여주기>

 

strSQL = "SELECT imageName FROM InstallInfo ";
strSQL += "WHERE Name = '1' ";

cmd = new SqlCommand(strSQL, Con);
aRS = cmd.ExecuteReader();

byte[] bImage = null;
while (aRS.Read())
{
bImage = (byte[])aRS[0];
}
if (bImage != null)
pickSul.Image = new Bitmap(new MemoryStream(bImage));

aRS.Close();

출처 : http://blog.naver.com/ascjun?Redirect=Log&logNo=50193768321

============================================================================================================================

//사진등록 콤보(클릭이벤트)
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());
}
}

출처 : http://blog.naver.com/ychun2k?Redirect=Log&logNo=110005425235

728x90

+ Recent posts