导航

向oracle 数据库写入 LOBs 数据

Posted on 2010-08-08 20:44  kingwangzhen  阅读(225)  评论(0编辑  收藏  举报

create or replace procedure UpdatePhoto(v_id in integer,v_binaryPhoto in blob) is
begin
  update usersinfo u set u.photo=v_binaryPhoto where u.id=v_id;
end UpdatePhoto;

/

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

namespace winformOracleDemo
{
    public partial class BlobAndBFiles : Form
    {
        public BlobAndBFiles()
        {
            InitializeComponent();
        }

        private void BlobAndBFiles_Load(object sender, EventArgs e)
        {

        }

        private void BtnUpLoad_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;

                byte[] bytePhoto = System.IO.File.ReadAllBytes(openFileDialog1.FileName);

                try
                {
                    using (OracleConnection conn = new OracleConnection(BusinessTools.ConnectionString))
                    {
                        conn.Open();

                   
                        OracleCommand cmd = conn.CreateCommand();
                        cmd.CommandText = "UpdatePhoto";
                        cmd.CommandType = CommandType.StoredProcedure;

                        OracleParameter paraId = new OracleParameter();
                        paraId.ParameterName = "v_id";
                        paraId.OracleDbType = OracleDbType.Int32;
                        paraId.Direction = ParameterDirection.Input;
                        paraId.Value = Convert.ToInt32(txtUserID.Text.Trim());
                        cmd.Parameters.Add(paraId);

                        OracleBlob orclBlob = new OracleBlob(conn);
                        orclBlob.Write(bytePhoto, 0, bytePhoto.Length);

                        OracleParameter paraPhoto = new OracleParameter();
                        paraPhoto.ParameterName = "v_binaryPhoto";
                        paraPhoto.OracleDbType = OracleDbType.Blob;
                        paraPhoto.Value = orclBlob;
                        paraPhoto.Direction = ParameterDirection.Input;                     

                        cmd.Parameters.Add(paraPhoto);

                        cmd.ExecuteNonQuery();
                    
                    }
                }
                catch (Exception ex)
                {
                    string ss = ex.Message;
                }
            }
        }


    }
}