Home » Open Source » Programming Interfaces » C# Application to backup Schema to .dmp file (Oracle 12.2.0.1.0, Windows Server 2016)
C# Application to backup Schema to .dmp file [message #676838] |
Sun, 21 July 2019 19:57 |
|
Dandaman
Messages: 15 Registered: July 2019
|
Junior Member |
|
|
Hi I am trying to figure out the best approach that a c# application on a end users desktop could backup an Oracle database schema.
End user does not have Oracle or any Oracle tools installed.
Have been trying to run SQL dbms_datapump to create the dmp and log file in a folder accessible to the Oracle database server.
I generate my SQL command in c# console application and run it via
command.ExecuteNonQuery();
But I keep getting the error
"ORA-00922: missing or invalid option".
When I copy and paste the SQL that I output to the console and run within SQL Developer on the Oracle Database server the script runs without error.
So the question is what don't I understand?
Is it the method in which the SQL is sent to the Oracle server that causes the script to fail?
Regards Dan.
(At the end of the line of researching and seeking guidance.)
|
|
|
|
Re: C# Application to backup Schema to .dmp file [message #676840 is a reply to message #676839] |
Sun, 21 July 2019 22:51 |
|
Dandaman
Messages: 15 Registered: July 2019
|
Junior Member |
|
|
Ah OK sorry I was just trying to get confirmation on the concept being valid before posting the code and PL/SQL script.
So here it is one instance of the generated PL/SQL:
set scan off
set serveroutput on
set escape off
whenever sqlerror exit
DECLARE
h1 number;
s varchar2(1000);
errorvarchar varchar2(100):= 'ERROR';
tryGetStatus number := 0;
success_with_info EXCEPTION;
PRAGMA EXCEPTION_INIT(success_with_info, -31627);
begin
h1 := dbms_datapump.open (operation => 'EXPORT', job_mode => 'SCHEMA', job_name => 'Mig2_BBE_Fresh66', version => 'COMPATIBLE');
tryGetStatus := 1;
dbms_datapump.set_parallel(handle => h1, degree => 1);
dbms_datapump.add_file(handle => h1, filename => 'BBE_FRESH66_EXPDAT.LOG', directory => 'MIGDMP3', filetype => 3);
dbms_datapump.set_parameter(handle => h1, name => 'KEEP_MASTER', value => 1);
dbms_datapump.metadata_filter(handle => h1, name => 'SCHEMA_EXPR', value => 'IN(''C##BBE_FRESH6'')');
dbms_datapump.add_file(handle => h1, filename => 'BBE_FRESH266_EXPDAT%U.DMP', directory => 'MIGDMP3', filesize => '100M', filetype => 1);
dbms_datapump.set_parameter(handle => h1, name => 'INCLUDE_METADATA', value => 1);
dbms_datapump.set_parameter(handle => h1, name => 'DATA_ACCESS_METHOD', value => 'AUTOMATIC');
dbms_datapump.set_parameter(handle => h1, name => 'ESTIMATE', value => 'BLOCKS');
dbms_datapump.start_job(handle => h1, skip_current => 0, abort_step => 0);
dbms_datapump.detach(handle => h1);
errorvarchar := 'NO_ERROR';
EXCEPTION
WHEN OTHERS THEN
BEGIN
IF((errorvarchar = 'ERROR')AND(tryGetStatus = 1)) THEN
DBMS_DATAPUMP.DETACH(h1);
END IF;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
RAISE;
END;
/
That works fine when run via SQL Developer.
But when the same/similar SQL script is sent via an Oracle database connection i get "ORA-00922: missing or invalid option".
The C# code for the console app is like so:
private static bool DataPump(string OracleAdminConnectionString, string OracleConnectionString, string datafileName, string userName)
{
bool stagesComplete = false;
string JobName = "Mig2_" + datafileName;
string upperDatafile = datafileName.ToUpper(); // DMP file must be in UPPER case.
if (JobName.Length>30) // Job Name must be not exceed 30 characters.
JobName = JobName.Substring(0, 30);
string sql = "CREATE OR REPLACE DIRECTORY MIGDMP3 AS 'C:\\MigrationDump'";
Console.WriteLine(sql);
stagesComplete = executeOneOracleSQL(OracleAdminConnectionString, sql);
if (!stagesComplete) return false;
sql = string.Format("GRANT READ, WRITE ON DIRECTORY MIGDMP3 TO {0}", userName);
Console.WriteLine(sql);
stagesComplete = executeOneOracleSQL(OracleAdminConnectionString, sql);
if (!stagesComplete) return false;
sql = string.Format("GRANT export full database TO {0}", userName);
Console.WriteLine(sql);
stagesComplete = executeOneOracleSQL(OracleAdminConnectionString, sql);
if (!stagesComplete) return false;
sql =
"set scan off\n" +
"set serveroutput on\n" +
"set escape off\n" +
"whenever sqlerror exit\n" +
"DECLARE\n" +
"h1 number;\n" +
"s varchar2(1000);\n" +
"errorvarchar varchar2(100):= 'ERROR';\n" +
"tryGetStatus number := 0;\n" +
"success_with_info EXCEPTION;\n" +
"PRAGMA EXCEPTION_INIT(success_with_info, -31627);\n" +
"begin\n" +
"\th1 := dbms_datapump.open (operation => 'EXPORT', job_mode => 'SCHEMA', job_name => '" + JobName + "', version => 'COMPATIBLE');\n" +
"\ttryGetStatus := 1;\n" +
"\tdbms_datapump.set_parallel(handle => h1, degree => 1);\n" +
"\tdbms_datapump.add_file(handle => h1, filename => '" + upperDatafile + "_EXPDAT.LOG', directory => 'MIGDMP3', filetype => 3);\n" +
"\tdbms_datapump.set_parameter(handle => h1, name => 'KEEP_MASTER', value => 1);\n" +
"\tdbms_datapump.metadata_filter(handle => h1, name => 'SCHEMA_EXPR', value => 'IN(''" + userName + "'')');\n" +
"\tdbms_datapump.add_file(handle => h1, filename => '" + upperDatafile + "_EXPDAT%U.DMP', directory => 'MIGDMP3', filesize => '100M', filetype => 1);\n" +
"\tdbms_datapump.set_parameter(handle => h1, name => 'INCLUDE_METADATA', value => 1);\n" +
"\tdbms_datapump.set_parameter(handle => h1, name => 'DATA_ACCESS_METHOD', value => 'AUTOMATIC');\n" +
"\tdbms_datapump.set_parameter(handle => h1, name => 'ESTIMATE', value => 'BLOCKS');\n" +
"\tdbms_datapump.start_job(handle => h1, skip_current => 0, abort_step => 0);\n" +
"\tdbms_datapump.detach(handle => h1);\n" +
"\terrorvarchar := 'NO_ERROR';\n" +
"EXCEPTION\n" +
"WHEN OTHERS THEN\n" +
"BEGIN\n" +
"IF((errorvarchar = 'ERROR')AND(tryGetStatus = 1)) THEN\n" +
"DBMS_DATAPUMP.DETACH(h1);\n" +
"END IF;\n" +
"EXCEPTION\n" +
"WHEN OTHERS THEN\n" +
"NULL;\n" +
"END;\n" +
"RAISE;\n" +
"END;\n" +
"/\n\n";
Console.WriteLine(sql);
stagesComplete = executeOneOracleSQL(OracleConnectionString, sql);
if (!stagesComplete) return false;
return true;
}
private static bool executeOneOracleSQL(string connectionString, string sql)
{
bool executionComplete = false;
// This will use the connection defined to connect to Oracle and execute the SQL statement past to it.
using (OracleConnection conn = new OracleConnection(connectionString))
{
try
{
conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return executionComplete;
}
using (OracleTransaction transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted))
{
using (OracleCommand command = new OracleCommand())
{
command.Connection = conn;
command.CommandType = CommandType.Text;
command.CommandTimeout = 120;
command.Transaction = transaction;
command.CommandText = sql;
//command.ArrayBindCount = dataRows;
command.Prepare();
try
{
command.ExecuteNonQuery();
transaction.Commit();
executionComplete = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
transaction.Rollback();
}
command.Parameters.Clear();
}
}
conn.Close();
conn.Dispose();
}
return executionComplete;
}
|
|
|
|
Re: C# Application to backup Schema to .dmp file [message #676844 is a reply to message #676841] |
Mon, 22 July 2019 19:42 |
|
Dandaman
Messages: 15 Registered: July 2019
|
Junior Member |
|
|
Oh my that is all that was blocking it. Thank you so much!
I will research more those commands and what they do and if the Database server administrators will allow or need them to complete the export.
Working solution to backup schema to .dmp file.
private static bool DataPump(string OracleAdminConnectionString, string OracleConnectionString, string datafileName, string userName)
{
bool stagesComplete = false;
string JobName = "Mig2_" + datafileName;
string upperDatafile = datafileName.ToUpper(); // DMP file must be in UPPER case.
if (JobName.Length>30) // Job Name must be not exceed 30 characters.
JobName = JobName.Substring(0, 30);
string sql = "CREATE OR REPLACE DIRECTORY MIGDMP3 AS 'C:\\MigrationDump'";
Console.WriteLine(sql);
stagesComplete = executeOneOracleSQL(OracleAdminConnectionString, sql);
if (!stagesComplete) return false;
sql = string.Format("GRANT READ, WRITE ON DIRECTORY MIGDMP3 TO {0}", userName);
Console.WriteLine(sql);
stagesComplete = executeOneOracleSQL(OracleAdminConnectionString, sql);
if (!stagesComplete) return false;
sql = string.Format("GRANT export full database TO {0}", userName);
Console.WriteLine(sql);
stagesComplete = executeOneOracleSQL(OracleAdminConnectionString, sql);
if (!stagesComplete) return false;
sql =
"DECLARE\n" +
"h1 number;\n" +
"s varchar2(1000);\n" +
"errorvarchar varchar2(100):= 'ERROR';\n" +
"tryGetStatus number := 0;\n" +
"success_with_info EXCEPTION;\n" +
"PRAGMA EXCEPTION_INIT(success_with_info, -31627);\n" +
"begin\n" +
"\th1 := dbms_datapump.open (operation => 'EXPORT', job_mode => 'SCHEMA', job_name => '" + JobName + "', version => 'COMPATIBLE');\n" +
"\ttryGetStatus := 1;\n" +
"\tdbms_datapump.set_parallel(handle => h1, degree => 1);\n" +
"\tdbms_datapump.add_file(handle => h1, filename => '" + upperDatafile + "_EXPDAT.LOG', directory => 'MIGDMP3', filetype => 3);\n" +
"\tdbms_datapump.set_parameter(handle => h1, name => 'KEEP_MASTER', value => 1);\n" +
"\tdbms_datapump.metadata_filter(handle => h1, name => 'SCHEMA_EXPR', value => 'IN(''" + userName + "'')');\n" +
"\tdbms_datapump.add_file(handle => h1, filename => '" + upperDatafile + "_EXPDAT%U.DMP', directory => 'MIGDMP3', filesize => '100M', filetype => 1);\n" +
"\tdbms_datapump.set_parameter(handle => h1, name => 'INCLUDE_METADATA', value => 1);\n" +
"\tdbms_datapump.set_parameter(handle => h1, name => 'DATA_ACCESS_METHOD', value => 'AUTOMATIC');\n" +
"\tdbms_datapump.set_parameter(handle => h1, name => 'ESTIMATE', value => 'BLOCKS');\n" +
"\tdbms_datapump.start_job(handle => h1, skip_current => 0, abort_step => 0);\n" +
"\tdbms_datapump.detach(handle => h1);\n" +
"\terrorvarchar := 'NO_ERROR';\n" +
"EXCEPTION\n" +
"WHEN OTHERS THEN\n" +
"BEGIN\n" +
"IF((errorvarchar = 'ERROR')AND(tryGetStatus = 1)) THEN\n" +
"DBMS_DATAPUMP.DETACH(h1);\n" +
"END IF;\n" +
"EXCEPTION\n" +
"WHEN OTHERS THEN\n" +
"NULL;\n" +
"END;\n" +
"RAISE;\n" +
"END;\n" +
"\n\n";
Console.WriteLine(sql);
stagesComplete = executeOneOracleSQL(OracleConnectionString, sql);
if (!stagesComplete) return false;
return true;
}
|
|
|
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sat Dec 21 09:51:14 CST 2024
|