1

I am trying to create database,schema and tables through c# code and so far i have successfully created database but getting error while creating schema:

I am trying to create below schema:

1) Person
2) Production
3) Sales
4) dbo

Now some tables have schema as Person and some tables have schema as dbo and so while creating tables i would like that tables to have appropriate schema.

enter image description here

I am not getting error while creating Person,Production and sales schema but i get error while creating schema for dbo.

Error:The specified schema name "dbo" either does not exist or you do not have permission to use it.CREATE SCHEMA failed due to previous errors.

Code: This is my code:

 conn.Open();
 foreach (var item in schemaList)
                    {
                        sqlQuery = "CREATE SCHEMA " + schema;
                        using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
                    {
                         cmd.CommandTimeout = 0;
                         cmd.ExecuteNonQuery();
                    }
                    }
  conn.Close();

Why does this permission problems doesnt comes in case of creating other schema and why it comes only while creating dbo schema.

1
  • The dbo schema (and several others) always exists in any database. As such, it can't be created. Commented Dec 14, 2016 at 7:01

2 Answers 2

2

dbo schema is created by default in your database and you cannot recreate it. Get a list of all schemas using:

SELECT name FROM sys.schemas

Or

SELECT SCHEMA_NAME FROM
INFORMATION_SCHEMA.SCHEMATA

Also if I may, your table names are more like columns to me. You might wanna reconsider how you use schemas, tables and the way you wanna communicate with your SQL server using c# code

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

Comments

1

There are certain default Built-In Schemas are created by sql servere. user can not drop them or create those schema. below schema are Built-in.

  1. dbo
  2. guest
  3. sys

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.