开发者

How to insert string in CLOB type column in oracle db using c#.net

开发者 https://www.devze.com 2023-04-13 03:14 出处:网络
i am trying to save a xml file as a string in clob type column in oracle db from c# am not sure how to insert clob type data from c#.

i am trying to save a xml file as a string in clob type column in oracle db from c# am not sure how to insert clob type data from c#.

code here:

public bool Insert_XMLDocument(string ReportType,object XMLDocument)
    {
      开发者_如何学运维  try
        {
            Database db = DatabaseFactory.CreateDatabase("XMLDOC_ConnectionString");
            DbCommand dbc = db.GetStoredProcCommand("insert_XMLDOC");
            dbc.CommandType = CommandType.StoredProcedure;
            db.AddInParameter(dbc, "pid", DbType.Int32, 1);                
            db.AddInParameter(dbc, "repo_document", DbType.Object,XMLDocument);                
            int i = db.ExecuteNonQuery(dbc);
            if (i > 0)
                return true;
            else
                return false;
        }
        catch (Exception ex) { 
            //HandleException(ex); 
            return false; }

    }

Error due to compilation of this : Cannot bind type System.String as Blob.


Can you show us your stored procedure and/or your db.AddInParameter() method? Without seeing more code this is more or less just a guess:

It seems like passing DbType.Object to your data layer would indicate that the data type of the parameter is supposed to be a BLOB (binary) yet you have indicated you want it to be a CLOB (character).

Let's assume your stored procedure is defined like this: CREATE PROCEDURE insert_XMLDOC (pid IN NUMBER, repo_document IN CLOB)

If the input object XMLDocument parameter is a string, or if your db.AddInParameter() method is converting this object to a string or any sort of textual representation then your data layer could be trying to assign a string as a BLOB parameter.


instead of using database factory i used the .net oracle provider method in which we can get "OracleType.Clob" which solves the problem i just passed the xml document as string and the job was done


Although the question seems to be outdated, I want to share an example that worked for me.

My intention was to save a JSON string (with more than 32k characters) into a clob field.

This is what I did:

string JSON_string = JsonConvert.SerializeObject(SomeObject);
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
myCommand.Parameters.AddWithValue("", SomeAttribute);
myCommand.Parameters.AddWithValue("", SomeAttribute2);
myCommand.Parameters.AddWithValue("", SomeAttribute3);
myCommand.Parameters.AddWithValue("", JSON_string);

And then execute the command. I'm using our companies library to do that, so I don't have to worry about the database connection:

DataSet myDS = myUser.myLoginUser._MySpAppS.RunSQL("INSERT INTO MARS$T_BCSAVINGS (MASSNAHMEN_ID, USER_ID, AKTIV, HEBELDATEI) VALUES (?, ?, ?, ?);", myCommand.Parameters);

I'm saving the result in a DataSet only to check if the query was successful.

So basically what I did, is to handover the string to the OleDbCommand parameters list and executed the query with those parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号