配色: 字号:
blob保存图片
2013-01-06 | 阅:  转:  |  分享 
  
usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

usingSystem.Configuration;

usingSystem.IO;



namespaceWindowsFormsApplication1

{

publicpartialclassForm1:Form

{

publicForm1()

{

InitializeComponent();

}



privatevoidbutton1_Click(objectsender,EventArgse)

{

SqlConnectionconnection=newSqlConnection();

stringconnstr=ConfigurationManager.ConnectionStrings["DataBase"].ToString();

connection.ConnectionString=connstr;

connection.Open();

SqlCommandcmd=newSqlCommand("selectphotofromStudentwhereID=000019",connection);

SqlDataAdapterda=newSqlDataAdapter(cmd);

DataSetds=newDataSet();

da.Fill(ds,"Student");

intc=ds.Tables["Student"].Rows.Count;



if(c>0)

{//BLOBisreadintoBytearray,thenusedtoconstructMemoryStream,

//thenpassedtoPictureBox.

Byte[]byteBLOBData=newByte[0];

byteBLOBData=(Byte[])(ds.Tables["Student"].Rows[c-1]["photo"]);

MemoryStreamstmBLOBData=newMemoryStream(byteBLOBData);

pictureBox1.Image=Image.FromStream(stmBLOBData);

}

connection.Close();

}



privatevoidbutton2_Click(objectsender,EventArgse)

{

constintbufferSize=100;

byte[]buffer=newbyte[bufferSize];

longcurrIndex=0;

byte[]photoPtr;

using(SqlConnectionconn=newSqlConnection(ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString))

{

conn.Open();

using(SqlCommandcomm=conn.CreateCommand())

{

comm.CommandText="SelectTEXTPTR(photo)FromStudentWhereID=000019";

photoPtr=(byte[])comm.ExecuteScalar();

}

using(SqlCommandcomm=conn.CreateCommand())

{



OpenFileDialogdlg=newOpenFileDialog();

dlg.Filter="JPGfiles|.jpg|Allfiles|.";

dlg.ShowDialog();

comm.CommandText="UPDATETEXTStudent.Photo@Pointer@Offsetnull@Data";

SqlParameterptrParm=comm.Parameters.Add("Pointer",SqlDbType.Binary,16);

ptrParm.Value=photoPtr;

SqlParameterphotoParm=comm.Parameters.Add("Data",SqlDbType.Image);

SqlParameteroffsetParm=comm.Parameters.Add("Offset",SqlDbType.Int);

offsetParm.Value=0;

using(FileStreamfs=newFileStream(dlg.FileName,FileMode.Open,FileAccess.Read))

{

intcount=fs.Read(buffer,0,bufferSize);

while(count!=0)

{

photoParm.Value=buffer;

photoParm.Size=count;

comm.ExecuteNonQuery();

currIndex+=count;

offsetParm.Value=currIndex;

count=fs.Read(buffer,0,bufferSize);

}

MessageBox.Show("PhotographReplaced!!!");

}

}

}

}

}

}









usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

usingSystem.IO;

usingSystem.Configuration;



namespaceUpdateBLOBData

{

publicpartialclassForm1:Form

{

publicForm1()

{

InitializeComponent();

}



privatevoidbtnUpdate_Click(objectsender,EventArgse)

{

constintbufferSize=100;

byte[]buffer=newbyte[bufferSize];

longcurrIndex=0;

byte[]photoPtr;

using(SqlConnectionconn=newSqlConnection(ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString))

{

conn.Open();

using(SqlCommandcomm=conn.CreateCommand())

{

comm.CommandText="SelectTEXTPTR(photo)FromStudentWhereID=1";

photoPtr=(byte[])comm.ExecuteScalar();

}

using(SqlCommandcomm=conn.CreateCommand())

{

comm.CommandText="UPDATETEXTStudent.Photo@Pointer@Offsetnull@Data";

SqlParameterptrParm=comm.Parameters.Add("Pointer",SqlDbType.Binary,16);

ptrParm.Value=photoPtr;

SqlParameterphotoParm=comm.Parameters.Add("Data",SqlDbType.Image);

SqlParameteroffsaetParm=comm.Parameters.Add("Offset",SqlDbType.Int);

offsaetParm.Value=0;

using(FileStreamfs=newFileStream(@"G:\4.jpg",FileMode.Open,FileAccess.Read))

{

intcount=fs.Read(buffer,0,bufferSize);

while(count!=0)

{

photoParm.Value=buffer;

photoParm.Size=count;

comm.ExecuteNonQuery();

currIndex+=count;

offsaetParm.Value=currIndex;

count=fs.Read(buffer,0,bufferSize);

}

MessageBox.Show("PhotographReplaced!!!");

}

}

}

}

}

}
献花(0)
+1
(本文系巭奥特曼首藏)