首页 > 数据库 >c# winform 读取oracle中blob字段的图片并且显示到pictureBox里,保存进库

c# winform 读取oracle中blob字段的图片并且显示到pictureBox里,保存进库

时间:2023-04-27 23:34:51浏览次数:44  
标签:进库 c# Image cmd pictureBox ms reader new conn


private void button2_Click(object sender, EventArgs e)
{
	OracleConnection conn = dbc.getConnection();//获得conn连接
	try
	{
		conn.Open();
		OracleCommand cmd = conn.CreateCommand();
		cmd.CommandText = "SELECT zp FROM kk.kkbj WHERE xh = 2345 ";//查询获得图片流

		OracleDataReader reader = cmd.ExecuteReader();//创建一个OracleDateReader对象 
		reader.Read();

		MemoryStream ms = new MemoryStream((byte[])reader["zp"]);

		Image image = Image.FromStream(ms, true);

		reader.Close();
		conn.Close();

		pictureBox1.Image = image;
	}
	catch (Exception ee)
	{
		MessageBox.Show(ee.Message.ToString());
	}

}




[color=red]----------下边是上传和存入数据库有asp.net和winform(转)----------------------------------[/color]



本文总结如何在.Net Winform和.Net webform(asp.net)中将图片存入sqlserver中并读取显示的方法


1,使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来



一,上传并存入SqlServer



数据库结构


create table test 

{ 

 id identity(1,1), 

 FImage image 

} 


 相关的存储过程 

Create proc UpdateImage 

( 

 @UpdateImage Image 

) 

As 

Insert Into test(FImage) values(@UpdateImage) 

GO



在UpPhoto.aspx文件中添加如下:


<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传"></asp:Button>



然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码:


private void btnAdd_Click(object sender, System.EventArgs e)
{
        //获得图象并把图象转换为byte[]
        HttpPostedFile upPhoto=UpPhoto.PostedFile;
        int upPhotoLength=upPhoto.ContentLength;
        byte[] PhotoArray=new Byte[upPhotoLength];
        Stream PhotoStream=upPhoto.InputStream;
        PhotoStream.Read(PhotoArray,0,upPhotoLength);

        //连接数据库
        SqlConnection conn=new SqlConnection();
        conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

        SqlCommand cmd=new SqlCommand("UpdateImage",conn);
        cmd.CommandType=CommandType.StoredProcedure;

        cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
        cmd.Parameters["@UpdateImage"].Value=PhotoArray;

        //如果你希望不使用存储过程来添加图片把上面四句代码改为:
        //string strSql="Insert into test(FImage) values(@FImage)";
        //SqlCommand cmd=new SqlCommand(strSql,conn);
        //cmd.Parameters.Add("@FImage",SqlDbType.Image);
        //cmd.Parameters["@FImage"].Value=PhotoArray;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}



二,从SqlServer中读取并显示出来



在需要显示图片的地方添加如下代码:


<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>



ShowPhoto.aspx主体代码:


private void Page_Load(object sender, System.EventArgs e)
{
     if(!Page.IsPostBack)
     {
                SqlConnection conn=new SqlConnection()
                conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

                string strSql="select * from test where id=2";//这里假设获取id为2的图片
                SqlCommand cmd=new SqlCommand()
                reader.Read();
                Response.ContentType="application/octet-stream";
                Response.BinaryWrite((Byte[])reader["FImage"]);
                Response.End();
                reader.Close();
     }
}




3,在winform中将图片存入sqlserver,并从sqlserver中读取并显示在picturebox中



1,存入sqlserver



数据库结构和使用的存储过过程,同上面的一样



1.1,在窗体中加一个OpenFileDialog控件,命名为ofdSelectPic



1.2,在窗体上添加一个打开文件按钮,添加如下单击事件代码:


Stream ms;
byte[] picbyte;
//ofdSelectPic.ShowDialog();
if (ofdSelectPic.ShowDialog()==DialogResult.OK)
{
   if ((ms=ofdSelectPic.OpenFile())!=null)
   {
    //MessageBox.Show("ok");
    picbyte=new byte[ms.Length];
    ms.Position=0;
    ms.Read(picbyte,0,Convert.ToInt32(ms.Length));
    //MessageBox.Show("读取完毕!");

    //连接数据库
    SqlConnection conn=new SqlConnection();
    conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

    SqlCommand cmd=new SqlCommand("UpdateImage",conn);
    cmd.CommandType=CommandType.StoredProcedure;

    cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
    cmd.Parameters["@UpdateImage"].Value=picbyte;

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

    ms.Close();
    }
   }



2,读取并显示在picturebox中



2.1 添加一个picturebox,名为ptbShow



2.2 添加一个按钮,添加如下响应事件:


SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

string strSql="select FImage from test where id=1";

SqlCommand cmd=new SqlCommand(strSql,conn);

conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
        reader.Read();

MemoryStream ms=new MemoryStream((byte[])reader["FImage"]);

Image image=Image.FromStream(ms,true);

        reader.Close();
        conn.Close();

ptbShow.Image=image;




黑色头发:http://heisetoufa.iteye.com


标签:进库,c#,Image,cmd,pictureBox,ms,reader,new,conn
From: https://blog.51cto.com/u_2543512/6232503

相关文章

  • c# winform 打开网站,状态栏上打开网站,超链接,isLink
    设置状态栏为isLink 然后给这个状态个onClick事件在事件里写上System.Diagnostics.Process.Start("http://www.heisetoufa.iteye.com");privatevoidtoolStripStatusLabel1_Click(objectsender,EventArgse){System.Diagnostics.Process.Start......
  • 【LeetCode动态规划#14】子序列系列题(最长递增子序列、最长连续递增序列、最长重复子
    最长递增子序列力扣题目链接(opensnewwindow)给你一个整数数组nums,找到其中最长严格递增子序列的长度。子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7]是数组[0,3,1,6,2,2,7]的子序列。示例1:输入:nums=[10,9,2,5,3,7......
  • c# winform 辅助测试工具,方便记录错误信息,方便查看调试错误,用INI存储,可脱离数据库...
    测试的时候做的一个辅助测试工具在找到了bug的时候可以方便的记录下出错的页面,出错方式,错误信息和解决办法错误信息都保存到一个ini文件下,可通过软件查看更加直观当然了,因时间仓促做的并不完美,源码提供下载,有兴趣的可自行修改,当然改完了也给我看看更好......
  • struts 1.2 struts连接池河dbcp连接池所要用的3个jar包
    连接池struts-config.xml里配置<struts-config>下<data-sources> <data-sourcekey="mysqlDB"type="org.apache.commons.dbcp.BasicDataSource"> <set-propertyproperty="driverClassName" val......
  • c#线程安全
    引用:https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/statements/locklock语句获取给定对象的互斥lock,执行语句块,然后释放lock。持有lock时,持有lock的线程可以再次获取并释放lock。阻止任何其他线程获取lock并等待释放lock。lock语句可确保......
  • SQLServer2005 AMD8450,3核CPU装不上sql 2005的解决办法
    中午12点开始,安装SQLServer2005,一直到晚上9点半,把网上的各个文章翻了个遍,依然没有安装上我的SQLServer2005,安装不上的症状跟网上其它人遇到的一样,可是为什么别人的就解决了,我的就不行呢```带着郁闷的心情睡觉了```夜里3点几分,起夜,想到数据库还......
  • js javascript js隐藏页面上有id的控件,隐藏页面上无控件包含的文字,控制页面控件属性
    1.隐藏页面上有id的控件varinput=document.getElementsByTagName("input");//获取页面所有inputfor(vari=0;i<input.length;i++){if(input.item(i).id.indexOf("txt")>=0)//判断input的id中是否包含txt字符串{......
  • iOS MachineLearning 系列(9)—— 人物蒙版图生成
    iOSMachineLearning系列(9)——人物蒙版图生成人物蒙版图能力是Vision框架在iOS15中新增的功能,这个功能可以将图片中的人物按照轮廓生成无光蒙版。无光蒙版在实际业务中非常有用,使用此蒙版可以方便的将人物从图片中提取出来,然后和其他的背景图进行合成。1-人物蒙版的提取首......
  • #yyds干货盘点#Class 对象
    类对象支持两种操作:属性引用和实例化。属性引用 使用Python中所有属性引用所使用的标准语法: obj.name。有效的属性名称是类对象被创建时存在于类命名空间中的所有名称。因此,如果类定义是这样的:classMyClass:"""Asimpleexampleclass"""i=12345deff(s......
  • C语言--练习
    1、写一个函数输出a的二进制(补码)中1的个数。intcount_(inta){ intcount=0; for(inti=0;i<32;i++) { if(((a>>i)&1)==1) count++; } returncount;}intmain(){ intcount=0; inta=0; scanf("%d",&a); count=count_(a);......