首 页文章中心下载中心繁體中文
设为首页
加入收藏
联系我们
您当前的位置:开源盛世-源代码下载网 -> 文章中心 -> 其他编程 -> VC# 技术 -> 文章内容 退出登录 用户管理
栏目导航
· VC# 技术 · Delphi技术
· Java技术 · 通用算法
· 编程相关
热门文章
· Tab Control控件使用...
· 学生档案管理系统
· [图文] 排列组合公式
· UTF-8与GB2312之间的...
· DirectShow下载安装...
· Virtual PC 在PAE模...
· Windows2000终端服务...
· MapInfo上的GIS系统...
· kalman filter 卡尔...
· Windows2000终端服务...
相关文章
C#对Oracle BLOB字段的写入读取方法
作者:不详  来源:vscodes.com整理  发布时间:2007-7-31 14:18:51  发布人:Polaris

减小字体 增大字体

1.写入BLOB

   string file_name = "D:\\20080523.jpg";
   string conn_str = "Provider=OraOLEDB.Oracle.1;Password=agoodjob;Persist Security Info=True;User ID=ams;Data Source=ims;Extended Properties=";//Provider必須用OraOLEDB.Oracle.1
   OleDbConnection cn = new OleDbConnection(conn_str);
   cn.Open();

   OleDbCommand cmd = new OleDbCommand("UPDATE BLOB SET PHOTO = ? ",cn);
   cmd.Parameters.Add("PHOTO",OleDbType.Binary);  
  
   FileStream fs = new FileStream(file_name, FileMode.OpenOrCreate, FileAccess.Read);
   byte[] MyData= new byte[fs.Length];
   fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
   fs.Close();

   cmd.Parameters["PHOTO"].Value = MyData;
   try
   {
    int result = cmd.ExecuteNonQuery();
    if(result<1)
    {
     OleDbCommand cmd1 = new OleDbCommand(" INSERT INTO BLOB (PHOTO) VALUES ( ? )",cn);
     cmd1.Parameters.Add("PHOTO",OleDbType.Binary);
     cmd1.Parameters["PHOTO"].Value = MyData;
     result = cmd1.ExecuteNonQuery();
    }
   } 
   catch(Exception e1)
   {
    Page.RegisterStartupScript("","<script language='javascript'>alert('"+e1.Message+"')</script>");
   }
   finally
   {
    cn.Close();
   }

2.读取BLOB

   string conn_str = "Provider=OraOLEDB.Oracle.1;Password=agoodjob;Persist Security Info=True;User ID=ams;Data Source=ims;Extended Properties=";//Provider必須用OraOLEDB.Oracle.1
  
   OleDbDataAdapter da = new OleDbDataAdapter();
   DataSet ds = new DataSet();
   OleDbConnection conn = new OleDbConnection(conn_str);
   OleDbCommand cmd = new OleDbCommand("SELECT PHOTO FROM WEB_DB.USERS WHERE USER_ID = 1", conn);
   da.SelectCommand = cmd;
   cmd.Connection.Open();
   da.Fill(ds);

   byte[] img = new byte[0];
   DataRow dr;
   dr = ds.Tables[0].Rows[0];

   if (!ds.Tables[0].Rows[0]["PHOTO"].ToString().Equals(""))
   {
    img =  (byte[])dr["PHOTO"];
   }

   Response.ContentType= "image/*";
   Response.BinaryWrite (img);

   conn.Close();

End of《C#对Oracle BLOB字段的写入读取方法》

[] [返回上一页] [打 印] [收 藏]
 
∷相关“C#对Oracle BLOB字段的写入读取方法”文章评论∷
(评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 网站目录 鄂ICP备06007162
开源盛世 版权所有Copyright © 2003-2005 VSCodes.Com. All Rights Reserved.