2006-07-13

通过ADO.NET存取文件

来源: 本站收集整理 作者:佚名 评论 0 条
 
}


}

private void button1_Click(object sender, System.EventArgs e)
{
this.openFileDialog1.ShowDialog();
}

/// <summary>
/// 从数据库中读取bitmap图片并显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("server=192.168.2.200;integrated security = sspi;database = northwind");
SqlCommand cmd = new SqlCommand("select * from imgtable where imgname like '%bmp%'",conn);
conn.Open();
SqlDataReader dr;
try
{
dr = cmd.ExecuteReader();
dr.Read();
System.Data.SqlTypes.SqlBinary sb = dr.GetSqlBinary(2);
//或byte[] imageData = (byte[])dr[2];
MemoryStream ms = new MemoryStream(sb.Value);//在内存中操作图片数据
Bitmap bmp = new Bitmap(Bitmap.FromStream(ms));
this.pictureBox1.Image = bmp;
dr.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}

/// <summary>
/// 读取文件并保存到硬盘,然后打开文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("server=192.168.2.200;integrated security = sspi;database = northwind");
SqlCommand cmd = new SqlCommand("select * from imgtable where imgname like '%doc'",conn);
conn.Open();
SqlDataReader dr;
try
{
dr = cmd.ExecuteReader();
dr.Read();
System.Data.SqlTypes.SqlBinary sb = dr.GetSqlBinary(2);
//或byte[] imageData = (byte[])dr[2];
//FileStream fs = new FileStream(@"C:\temp.bmp",FileMode.Create);
string filename = @"C:\" System.IO.Path.GetFileName(dr.GetString(1));
FileStream fs = new FileStream(filename,FileMode.Create);
fs.Write(sb.Value,0,sb.Value.Length);
fs.Close();
//this.pictureBox1.Image = Image.FromFile(@"C:\temp.bmp");
System.Diagnostics.Process.Start(filename);
dr.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
}

直接把整个文件读取到内存中的数组里对于小文件来说是没问题的,但假如是大文件,非凡是大小都超过了物理内存的文件,可能会导致严重的内存问题,需要分段读取,并分段写到数据库。


共3页: 上一页 [1] [2] 3 下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:网页设计配色精彩十例  
下一篇:网页设计技巧—布局
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号