4

How can I create a oracle database programmatically in ADO.NET and a schema for it with userId + password so I can just go to my non-favorite tool the sql oracle developer tool where I just create a connection entering:

  • connectionstring name
  • UserId(schema)
  • password

1 Answer 1

8

I've done it with SQL before but never tried with ADO.NET ...

string connectionString = "...";
string oracleDataPath = "C:\\PATH_TO_ORADATA\\";

string username = "NEW_USER";
string password = "NEW_PWD";
string schema = "NEW_SCHEMA";

using (OracleConnection conn = new OracleConnection(connectionString))
{
    conn.Open();
    OracleCommand cmd = conn.CreateCommand();
    cmd.CommandText = "CREATE TABLESPACE \"" + schema + "\" DATAFILE '" + oracleDataPath + schema + ".DBF' SIZE 10M AUTOEXTEND ON NEXT 1M";
    cmd.ExecuteNonQuery();
    cmd.CommandText = "CREATE USER \"" + username + "\" IDENTIFIED BY \"" + password + "\" DEFAULT TABLESPACE \"" + schema + "\" TEMPORARY TABLESPACE TEMP";
    cmd.ExecuteNonQuery();
    cmd.CommandText = "GRANT CONNECT TO \"" + username + "\"";
    cmd.ExecuteNonQuery();
    cmd.CommandText = "ALTER USER \"" + username + "\" QUOTA UNLIMITED ON \"" + schema + "\"";
    cmd.ExecuteNonQuery();
}

Use the ADMIN/DBA account on the connection string.
Set oracleDataPath with the path where your Oracle keeps its data files.

Let me know if it works :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.