毛毛的小窝 — 关注技术交流、让我们一起成长

导航

(原) ODP.NET 演示以参数的形式绑定 OracleClob 对象

using System;
using System.Data;
using System.Text;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

namespace Sample55
{
    
// 演示以参数的形式绑定 OracleClob 对象
    class Program
    
{
        
static void Main(string[] args)
        
{
            
// Connect
            string constr = "User Id=scott;Password=tiger;Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.24)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = bjoracle.oracle10g.mynet)))";
            OracleConnection conn 
= Connect(constr);

            
if (conn == null)
                
return;

            
// Setup
            Setup(conn);

            
// Set the command
            OracleCommand cmd = new OracleCommand("update multimedia_tab set story=:1 where thekey=1");
            cmd.Connection 
= conn;
            cmd.CommandType 
= CommandType.Text;

            
// Create an OracleClob object, specifying no caching and not a NCLOB
            OracleClob clob = new OracleClob(conn,false,false);
            
            
// Write data to the OracleClob object, clob, which is a temporary LOB
            string str = "this is a new story";
            clob.Write(str.ToCharArray(), 
0, str.Length);

            
// Bind a parameter with OracleDbType.Clob
            cmd.Parameters.Add("clobdata", OracleDbType.Clob, clob, ParameterDirection.Input);

            
try
            
{
                
// Execute command
                cmd.ExecuteNonQuery();

                
// A new command text
                cmd.CommandText = "select thekey, story from multimedia_tab where thekey=1";

                
// Create DataReader
                OracleDataReader reader = null;

                
try
                
{
                    reader 
= cmd.ExecuteReader();
                }

                
catch (Exception ex)
                
{
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }


                
// Read the first row
                reader.Read();

                
// Get an oracleclob object from the datareader.
                clob = reader.GetOracleClob(1);

                
// Display the new data using Value property
                Console.WriteLine(clob.Value);

            }

            
catch (Exception ex)
            
{
                Console.WriteLine(
"Exception: " + ex.Message);
            }

            
finally
            
{
                
// Dispose OracleClob object
                clob.Dispose();

                
// Dispose OracleCommand object
                cmd.Dispose();

                
// Close and Dispose OracleConnection object
                conn.Close();
                conn.Dispose();
            }


            
// waiting
            Console.ReadLine();
        }


        
// Opening a new Connection
        public static OracleConnection Connect(string connectStr)
        
{
            OracleConnection conn 
= new OracleConnection(connectStr);
            
try
            
{
                conn.Open();
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(
"Error: {0}", ex.Message);
                conn 
= null;
            }

            
return conn;
        }


        
// Setup the necessary Tables & Test Data
        public static void Setup(OracleConnection conn)
        
{
            StringBuilder blr;
            OracleCommand cmd 
= new OracleCommand("", conn);

            blr 
= new StringBuilder();
            blr.Append(
"DROP TABLE multimedia_tab");
            cmd.CommandText 
= blr.ToString();
            
try
            
{
                cmd.ExecuteNonQuery();
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(
"Warning: {0}", ex.Message);
            }


            blr 
= new StringBuilder();
            blr.Append(
"CREATE TABLE multimedia_tab(thekey NUMBER(4) PRIMARY KEY,");
            blr.Append(
"story CLOB, sound BLOB)");
            cmd.CommandText 
= blr.ToString();
            
try
            
{
                cmd.ExecuteNonQuery();
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(
"Error: {0}", ex.Message);
            }


            blr 
= new StringBuilder();
            blr.Append(
"INSERT INTO multimedia_tab values(");
            blr.Append(
"1,");
            blr.Append(
"'This is a long story.Once upon a time',");
            blr.Append(
"'898787787874454654564578978971114544897')");
            cmd.CommandText 
= blr.ToString();

            
try
            
{
                cmd.ExecuteNonQuery();
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(
"Error: {0}", ex.Message);
            }

        }

    }

}
注意:
1、创建OracleClob对象,并给对象赋值 clob.Write(str.ToCharArray(), 0, str.Length);
2、将对象加入到command中


引用:odp.net sample

posted on 2007-12-28 09:55  mjgforever  阅读(427)  评论(0编辑  收藏  举报