1

How to create user oracle database using C# not using SQL manually input username,password? I want to create Oracle database user using C# language into Visual Studio Form.

When I click Create button then it should automatically create database username and password using C# language. Please help me. enter image description here

2
  • The syntax is documented here. What isn't working? Commented Jan 25, 2018 at 10:28
  • Looks like a "Do it for me" request, until you add what have you done so far? Commented Jan 25, 2018 at 10:32

1 Answer 1

1

Well, that's not a straightforward task. I don't speak C# so I'll comment only Oracle side of story.

New users are created by another, privileged user (i.e. the one that is granted CREATE USER privilege) - not every user is capable of doing it. For example, SYS is, but SYS is special and you shouldn't use it at all, but create another user and grant it privileges required to perform such actions (one option is to grant it DBA role).

Now, connected as that privileged user, you'd run a CREATE USER command which looks like this:

create user oliur            --> this is username
  identified by oliur_pw     --> this is password
  default tablespace users
  temporary tablespace temp
  profile default
  quota  unlimited on users;

List of available tablespaces is

select tablespace_name from dba_tablespaces;

Once you do that, you should grant some initial privileges to newly created user. Otherwise, he won't be able to do anything, not even connect to the database. That would be CREATE SESSION privilege. Some other privileges:

grant create session       to oliur;
grant create table         to oliur;
grant create procedure     to oliur;
...

Perhaps you could create a stored procedure, which resides in the privileged user's schema. You'd pass username (as a parameter), and it would create user with that name. As of the password, you can set it to be the same as username, something constant, encrypted, etc. - it's up to you.

Note that such a procedure has to utilize dynamic SQL (EXECUTE IMMEDIATE) because otherwise you can't perform DDL from a procedure.

This is how it might look like:

create or replace procedure p_create_user (par_username in varchar2) is
begin
  execute immediate ('create user '     || par_username ||
                     '  identified by ' || par_username ||
                     '  default tablespace user_data '  ||
                     '  temporary tablespace temp'      ||
                     '  profile default'                ||
                     '  quota unlimited on user_data');

  execute immediate ('grant create session to ' || par_username);
end;
/ 

In Oracle, call it like this:

begin
  p_create_user('oliur');
end;
/

PL/SQL procedure successfully completed.

OK, let's connect as oliur:

SQL> connect oliur/oliur@kc11g
Connected.

Cool, success! Can we do something else?

SQL> create table test (id number);
create table test (id number)
*
ERROR at line 1:
ORA-01031: insufficient privileges

Nope, missing privileges to do that.

Now, as of C# part of the story, you just have to figure out how to call an Oracle procedure, but I hope that this is way simpler task than the above.

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

2 Comments

How to create user oracle database using C# not using SQL manually input username,password? I want to create Oracle database user using C# language into Visual Studio Form. down vote favorite How to create user oracle database using C# not using SQL manually input username,password? I want to create Oracle database user using C# language into Visual Studio Form. down vote favorite How to create user oracle database using C# not using SQL manually input username,password? I want to create Oracle database user using C# language into Visual Studio Form.
Thanks Littlefoot.

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.