How can I specify a schema name in SQL ConnectionString using ASP.net dynamically. I have a single database with multiple schema. I need to fetch data from table belonging to particular schema.
Asked
Active
Viewed 1.7k times
4 Answers
6
You can only specify the database and user details in the connection string.
To retrieve data from a specific schema, you need to reference that schema in your query. For example;
SELECT field1, field2 FROM [Schema].[Table]

ChrisBint
- 12,773
- 6
- 40
- 62
2
I would like to reference here an older thread, which is definitely useful in this question:
Possible to set default schema from connection string?
You can only set the default schema to the user itself. You can try an ALTER USER statement, where you can define a default schema to the user. It means you can create a user to each schema, if that is what you need.

Community
- 1
- 1

András Ottó
- 7,605
- 1
- 28
- 38
0
Each user has their own schema and that is the default schema. Once logged in you can execute:
ALTER SESSION SET CURRENT_SCHEMA = myschema
So you need to execute an extra statement after connecting.

Robert Smith
- 302
- 3
- 13
0
Here is how to do it. I just tried it.
"ConnectionStrings": {
"<Connection Name>": "Host=<Database Server>;Database=<Database Name>;Port=5432;Username=<WindowUserName>;Password=<WindowsPassword>;IntegratedSecurity=true;SearchPath=MySchema"

toyota Supra
- 3,181
- 4
- 15
- 19

John
- 3
- 5