好文档 - 专业文书写作范文服务资料分享网站

C#存储图片到数据库中的Image字段剖析

天下 分享 时间: 加入收藏 我要投稿 点赞

字号:大 中 小

private void button1_Click(object sender, System.EventArgs e {

string filename= textBox1.Text;//图片文件所在的路径

BinaryReader reader=null;

FileStream myfilestream = new FileStream(filename,FileMode.Open;

try {

reader=new BinaryReader(myfilestream;

byte[] image = reader.ReadBytes((intmyfilestream.Length;//存储图片到数组中。

using (SqlConnection conn = new

SqlConnection(\

{

using (SqlCommand command = conn.CreateCommand(

{

command.CommandText =@\ INTO photo(photo VALUES (@photo\

command.Parameters.Add(\

conn.Open(;

command.ExecuteNonQuery(;

conn.Close(;

MessageBox.Show(\文件保存成功!\提示\

}

}

}

catch(IOException ee {

MessageBox.Show(ee.Message.ToString(; }

finally {

if(reader!=null

reader.Close(; } }

private void button2_Click(object sender, System.EventArgs e

{//把数据表中Image字段中保存的图片信息转换图片形式在Piceture控件中显示出来。

using (SqlConnection conn = new

SqlConnection(\ {

using (SqlCommand command = conn.CreateCommand(

{

command.CommandText = @\Pid=@Pid\

command.Parameters.Add(\

conn.Open(;

byte[] imageData = (byte[]command.ExecuteScalar(;

conn.Close(;

MemoryStream mStream = new MemoryStream(;

mStream.Write(imageData, 0, imageData.Length;

mStream.Flush(;

Bitmap img= new Bitmap(mStream;

pictureBox1.Image=img;

} } }

private void button3_Click(object sender, System.EventArgs e {

openFileDialog1.ShowDialog(; }

private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e {

textBox1.Text=openFileDialog1.FileName; } }

C#存储图片到数据库中的Image字段剖析

字号:大中小privatevoidbutton1_Click(objectsender,System.EventArgse{stringfilename=textBox1.Text;//图片文件所在的路径BinaryReaderreader=null;
推荐度:
点击下载文档文档为doc格式
2gkgg3sciu17c19373fh7l7tx29ybm00g17
领取福利

微信扫码领取福利

微信扫码分享