 |
|
 |
Hi ,
I need help for oracle datatype-
I am using TEMP_STRING VARCHAR2(32767);
but while stroring in it I am building string more than 32767(so exception) ..which is around 40000+
So I used LONG datatype - but still the same.
Will it be appropriate or is there any other one in Oracle ?
Can you please suggest.
Thanks and regards,
- Ajay K
|
|
|
|
 |
|
 |
The head of the article is different from it content. No useful information.
|
|
|
|
 |
|
 |
I copied your code into a project and I got a exception at the line
clob.Write(newvalue, 0, newvalue.Length);
THE CONNECTION IS CLOSED!!!
the connection of the datareader is open but the connection of the Clob is Null and I can not set it because it is a read-only property.
|
|
|
|
 |
|
 |
If you use Oracle 9, 10 or 11 you can do it much easier. I haven't tested with Oracle 8 because I don't have Oracle 8 on my pc so I can't test with Oracle 8, Oracle 8 is really really old (year 2000). I show two variants. One with the Oracle provider of Microsoft and one with Oracle's own provider OPD.NET.
/* create table testclob (large clob); */
//Microsofts provider for Oracle
using System.Data.OracleClient;
using (OracleConnection con = new OracleConnection("user id=scott;password=tiger;data source=ORA9"))
{
con.Open();
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = " insert into testclob values (:parameter) ";
OracleParameter oracleParameterClob = new OracleParameter();
oracleParameterClob.OracleType = OracleType.Clob;
//1 million long string
oracleParameterClob.Value = "A".PadRight(1000 * 1000, 'B');
oracleParameterClob.ParameterName = "PARAMETER";
cmd.Parameters.Add(oracleParameterClob);
cmd.ExecuteNonQuery();
}
con.Close();
}
If you use Oracle's owner provider (ODP.NET):
using Oracle.DataAccess.Client;
using (OracleConnection con = new OracleConnection("user id=scott;password=tiger;data source=ORA9"))
{
con.Open();
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = " insert into testclob values (:parameter) ";
OracleParameter oracleParameterClob = new OracleParameter();
oracleParameterClob.OracleDbType = OracleDbType.Clob;
//1 million long string
oracleParameterClob.Value = "A".PadRight(1000 * 1000, 'B');
cmd.Parameters.Add(oracleParameterClob);
cmd.ExecuteNonQuery();
}
con.Close();
}
|
|
|
|
 |
|
 |
How to read CLOB???? from datareader
modified on Thursday, October 30, 2008 9:54 PM
|
|
|
|
 |
|
 |
Hi,
I'm getting a null return from GetOracleClobForUpdate at index 0.
My select statement select 1 column which is the clob type column.
Any idea how this could happen?
Thanks in advance.
|
|
|
|
 |
|
 |
I am just inserting clobs by adding my clob parameter as OracleType.Clob. Is this not easier?
|
|
|
|
 |
|
 |
I don't really get u what you meant..
More details please..
'ace'
|
|
|
|
 |
|
 |
Hi!
If you use "Parametrized SQL Statements" you can insert as much data as you want. I didn't even have to specify that data type. The driver recognizes the necessary mapping and does everything. I inserted Strings of sizes up to 100 MB into one CLOB, then things got too slow on our machines.
Just look at the docs for the "OracleCommand.Parameters" Property and the "OracleParameter" Class. In my help file the examples provided are for SELECT statements, but you use it for INSERTs too.
Best regards
Daniel
|
|
|
|
 |
|
 |
This help me a lot!!! Thanks
Nicola
If we use Oracle.DataAccess it's a slight different :
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
public void UpdateClob(OracleConnection connOracle, string SQLStatement, string str)
{
try
{
if (SQLStatement.Length > 0)
{
OracleTransaction trn;
OracleClob clob = new OracleClob(connOracle);
byte[] newvalue = System.Text.Encoding.Unicode.GetBytes(str);
sqlCommandOracle = new OracleCommand(SQLStatement, connOracle);
rstOracle = sqlCommandOracle.ExecuteReader();
bool recExist = rstOracle.Read();
if (recExist)
{
clob = rstOracle.GetOracleClobForUpdate(1);
trn = connOracle.BeginTransaction();
clob.Erase();
clob.Write(newvalue, 0, newvalue.Length);
trn.Commit();
}
}
}
catch (Exception ex)
{
}
finally
{
}
}
|
|
|
|
 |
|
 |
Yup, Nicola
Thx for the support..
'ace'
|
|
|
|
 |
|
 |
I am facing a problem on clob.Write(newvalue, 0, newvalue.Length). The error I get is "Invalid operation. The connection is closed." At this point, clob's Connection property is null. But I can't assign a connection because it is a readonly property. What do I need to do to get the code to recognize that the Connection in sqlCommandOracle should be set as the Connection for clob?
|
|
|
|
 |
|
 |
If the connection is closed, means you have to check your connection first. It's has nothing wrong with the clob...
Good luck
'ace'
|
|
|
|
 |
|
 |
I am getting the same error on at clob.Write(newvalue, 0, newvalue.Length);
connOracle.State is equal to "Open"
My clob variable has the following properties:
{System.Data.OracleClient.OracleLob}
base {System.IO.Stream}: {System.Data.OracleClient.OracleLob}
CanRead: true
CanSeek: true
CanWrite: true
ChunkSize: 0
Connection: null
IsBatched: false
IsNull: true
IsTemporary: false
Length: 0
LobType: Blob
Position: 0
Value: {}
Thanks
|
|
|
|
 |
|
 |
I had the same problem. You have to create a temp lob. I didn't like this article because this code didn't work for me.
See this: EXAMPLE OK[^]
|
|
|
|
 |
|
 |
Your code work correctly with text sql statement.
But I want use stored procedure (like this below), but don't work.
PROCEDURE Test(p_field IN pls_integer)
AS
v_temp clob;
BEGIN
select field2 into v_temp from table
WHERE field = p_field
FOR UPDATE;
END Test;
Thanks.
Sara
|
|
|
|
 |
|
 |
Hi, Sara
What i experienced was the statement need some kind of transaction locking, so when u want to update the record in store procedure, probably u could try this sample:
PROCEDURE Test(p_field IN pls_integer)
AS
v_temp clob;
BEGIN
-- Begin transaction
BEGIN TRANSACTION
-- Check for errors
IF @@error <> 0 GOTO E_General_Error
select field2 into v_temp from table
WHERE field = p_field
FOR UPDATE;
-- No errors; perform COMMIT and exit
COMMIT TRANSACTION
RETURN
-- If an error occurs, rollback and exit
E_General_Error:
PRINT 'Error'
ROLLBACK TRANSACTION
RETURN
END Test;
Hope it will help you... Gut luck
'ace'
|
|
|
|
 |
|
 |
What kind of language is this? Is it T SQL? T SQL can't work in Oracle, in Oracle you will have to use PL/SQL.
|
|
|
|
 |
|
 |
The title of article says "Using C# for Inserting CLOB data in Oracle". How are you inserting it? You are using DataReader to read something.
It will be good to have sample code, just like all other articles on CodeProject.
|
|
|
|
 |
|
 |
This article will help u a lot when u are trying to insert CLOB data more than 4000 characters into Oracle. So, i'm assuming u already have one record in the table with one primary key.
Then, u just open that table with that record for updating new clob data which is more than 4000 characters. And in the void InsertingData, u will need DataReader method to read clob data as bytes. that's how its process.
Thanks for your comments.
'ace'
|
|
|
|
 |
|
|
 |
|
 |
Yes, we're doing an update. Absolutely, u are right.
Thanks so much...
'ace'
|
|
|
|
 |
|
 |
This code needs some improvements:
There are some well known rules when dealing with databases and one goes like this
"Always open a database connection at the latest possible point in code and close it soon as possible !"
It's also a good idea to use the Dispose()-Pattern to be sure the Db-Connection is cleaned up in case an exception is thrown !
So (in my opinion) it's a bad idea to open the DB-Connection in the CStr of your class, use it in a method and clean it up in another method. What if the user of your class doesn't call your CloseDatabase() method ?
What about implementing your class as a static class cause it's just a utility that doesn't need to manage any state ?
Here is a rough sketch of my proposal:
public static class clsOracle
{
public static void InsertingData(string connectionStr, string SQLStatement, string str)
{
using (OracleConnection connOracle = new System.Data.OracleClient.OracleConnection(connstr))
{
connOracle.Open();
}
}
}
Last point:
For reading/writing of large blobs one should use StreamReader/Writer objects to minimize the usage of memory on the db-server ... please search on the web for further information
Have a nice day!
|
|
|
|
 |
|
 |
Thanks hleuze for your improvement code. I really understand what your purpose and will try it later.
Thanks for your comments.
'ace'
|
|
|
|
 |