简体   繁体   中英

ORA-01036: illegal variable name/number error while updating Oracle DB in C#

Hi I am developing an application in Winforms C# VS 2010 for saving PDF files in database as BLOB type but while executing code I am getting error that ORA-01036: illegal variable name/number

Below is my code:

private void DoConversion()
    {
     //Step 1
     // Connect to database
     var con = _connector.OpenOracleConnection();

     // Step 2
     const string sourceLoc = "D:/Ashok Karale_.pdf";
     const string destinationLoc = "D:/RecoveredAshok Karale_.pdf";

     // provide read access to the file
     var fs = new FileStream(sourceLoc, FileMode.Open, FileAccess.Read);
     // Create a byte array of file stream length
     var pdfData = new byte[fs.Length];

     //Read block of bytes from stream into the byte array
     fs.Read(pdfData, 0, Convert.ToInt32(fs.Length));

     //Close the File Stream
     fs.Close();

     // Step 3
     // Create Anonymous PL/SQL block string
     const string block = " BEGIN " +
                          " UPDATE USER_ACCOUNT_STATEMENT SET statement_FILE = :1 ;
                            SELECT statement_FILE into :1 from testblob WHERE 
                            STATEMENT_id = 1070; END; ";

     // Set command to create Anonymous PL/SQL Block
     var cmd = new OracleCommand {CommandText = block, Connection = con, CommandType = CommandType.Text};

     // Since executing an anonymous PL/SQL block, setting the command type
     // as Text instead of StoredProcedure

     // Step 4
     // Setting Oracle parameters

     // Bind the parameter as OracleDbType.Blob to command for inserting image
     var param = cmd.Parameters.Add("blobtodb", OracleDbType.Blob);
     param.Direction = ParameterDirection.Input;

     // Assign Byte Array to Oracle Parameter
     param.Value = pdfData;

     // Step 5
     // Execute the Anonymous PL/SQL Block

     // The anonymous PL/SQL block inserts the image to the
     // database and then retrieves the images as an output parameter
     cmd.ExecuteNonQuery();
     MessageBox.Show(@"Inserted");
    }

I am getting error at cmd.ExecuteNonQuery(); . I am not getting exactly where it's going wrong. I have checked it many time. I thing I am missing something at Parameter but not sure. Please advise. Thanks in advance.

I used below method to get my answer.

public static void DatabaseFilePut(MemoryStream fileToPut, OracleConnection con)
    {
        try
        {
            //int varID = 0;
            //var file = fileToPut.ToArray();
            const string preparedCommand =
                @"INSERT INTO user_account_statement (statement_id,session_key,login_id,user_id,account_number,from_date,to_date,ipaddress,create_date_time,STATEMENT_FILE)VALUES(1073,'fe79e0345986b5a439c26f731234868b53f877366f529',2335,'204254','108142',to_date('2014-08-23 16:45:06','yyyy-mm-dd hh24:mi:ss'),to_date('2014-08-23 16:45:06','yyyy-mm-dd hh24:mi:ss'),'106.79.126.249',to_date('2014-08-23 16:45:06','yyyy-mm-dd hh24:mi:ss')," + " :ssfile)";

            using (var sqlWrite = new OracleCommand(preparedCommand, con))
            {
                sqlWrite.BindByName = true;
                var blobparameter = new OracleParameter
                {
                    OracleDbType = OracleDbType.Blob,
                    ParameterName = "ssfile",
                    Value = fileToPut.ToArray()
                };
                sqlWrite.Parameters.Add(blobparameter);
                sqlWrite.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

will provide my correct code shortly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM