转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> ORACLE >> 正文
c# ,在Oracle 中,对 blob 类型对象的操作         ★★★★

c# ,在Oracle 中,对 blob 类型对象的操作

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1581 更新时间:2009/4/22 22:04:13

尝试的情况是,提供一个TEXT 框,让用户输入,允许输入非常多的值,保存到Oracle 数据库中。 为了能够大量数据保存,因此,对字段类型设置为 blob 型 。
网络上的类似帮助太复杂了,发现如果只是文件内的操作,还是比较简单的。
思路:
1。先将text 文本转变成2进制  
 byte[] ddd;
   ddd = System.Text.Encoding.Unicode.GetBytes(this.textBox1.Text);

2。再将该2进制存入数据库中,发现这种对数据库的访问方法可行。
 cmd.Parameters.Add 。。。
cmd.ExecuteNonQuery();

表结构如下:
create table XLUTEST
(
  HHHH BLOB,
  GGGG VARCHAR2(10)
)
tablespace SYSTEM
  pctfree 10
  pctused 40
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  )


c#  全部WINFORM 代码如下 :
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace blob
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // textBox1
   //
   this.textBox1.Anchor = System.Windows.Forms.AnchorStyles.Top;
   this.textBox1.Location = new System.Drawing.Point(40, 48);
   this.textBox1.Multiline = true;
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(576, 272);
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "textBox1";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(240, 336);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "SAVE";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(408, 344);
   this.button2.Name = "button2";
   this.button2.TabIndex = 2;
   this.button2.Text = "READ";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(88, 336);
   this.button3.Name = "button3";
   this.button3.TabIndex = 3;
   this.button3.Text = "UPDATE";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(648, 397);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.textBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

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

   string  cnnstr = "provider=OraOLEDB.Oracle;data source=yourdb;User Id=xxxx;Password=xxxx;";
   OleDbConnection con = new OleDbConnection(cnnstr);
   try
   {
    con.Open();
   }
   catch
   {} 
   OleDbCommand cmd = new OleDbCommand(cnnstr,con);

   cmd.CommandType=CommandType.Text;
   cmd.CommandText=cnnstr;

   string txvalue = this.textBox1.Text.Trim();

   byte[] expbyte  =  System.Text.Encoding.Unicode.GetBytes(txvalue);

   cmd.CommandText = " insert into xlutest ( hhhh ) values (:hhhh) ";

   cmd.Parameters.Add("hhhh",System.Data.OleDb.OleDbType.Binary,expbyte.Length);
   cmd.Parameters [0].Value = expbyte;

   try
   {
    cmd.ExecuteNonQuery();
    MessageBox.Show("OK");
   }
   catch ( System.Exception e1 )
   {
    MessageBox.Show(e1.Message );
   }


  }

  public void ExecwithReturnBinary(string cmdText,string BinaryContent,byte[] byteBlob)
  {           
           
   string  cnnstr = "provider=OraOLEDB.Oracle;data source=yourdb;User Id=xxxx;Password=xxxx;";
   OleDbConnection con = new OleDbConnection(cnnstr);
   try
   {
    con.Open();
   }
   catch
   {} 
   OleDbCommand cmd = new OleDbCommand(cmdText,con);

   cmd.CommandType=CommandType.Text;
   cmd.CommandText=cmdText;
   cmd.Parameters.Add("str",System.Data.OleDb.OleDbType.VarChar,10).Value = "sdfsddf";
   cmd.Parameters.Add(BinaryContent,System.Data.OleDb.OleDbType.Binary ,byteBlob.Length).Value= byteBlob;
   cmd.ExecuteNonQuery();
  }


  private void save()
  {
   //   byte[] abyte;
   //   string str = this.textBox1.Text;
   //   System.IO.FileStream fs = new FileStream("c:\\xxx.txt",System.IO.FileMode.Open);
   //   System.IO.BinaryReader br = new BinaryReader(fs);
   //   abyte = br.ReadBytes(fs.Length);
   //   br.Close();


  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   string strSQL = "select gggg,hhhh from xlutest ";
   string  cnnstr = "provider=OraOLEDB.Oracle;data source=yourdb;User Id=xxxx;Password=xxxx;";
   OleDbConnection con = new OleDbConnection(cnnstr);
   try
   {
    con.Open();
   }
   catch
   {} 
   OleDbCommand cmd = new OleDbCommand(strSQL,con);
   System.Data.OleDb.OleDbDataReader dr = cmd.ExecuteReader();
   while ( dr.Read())
     
   {
    string dd =dr ["gggg"].ToString();
    byte[] ooo = (byte[])dr["hhhh"];
    string str ;
    str = System.Text.Encoding.Unicode.GetString(ooo);
    this.textBox1.Text =str;

   }
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
   //Decode
   //   string str ;
   //   str = System

[1] [2]  下一页


[C语言系列]C# 过滤html,js,css代码 正则表达式  [C语言系列]C# DataGridView显示行号的两种方法
[C语言系列]C# WinForm 中Label自动换行 解决方法  [C语言系列]C# 线程调用主线程中的控件
[电脑应用]c# winform 打包部署 自定义界面 或设置开机启动  [C语言系列]C# 和 Linux 时间戳转换
[C语言系列]C#实现 WebBrowser中新窗口打开链接用默认或者指定…  [C语言系列]C#全角和半角转换
[C语言系列]c#WebBrowser查找并选择文本  [C语言系列]C#中实现WebBrowser控件的HTML源代码读写
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台