How to reference a different schema without hard coding it
We have multiple environments (development, test, Production, etc). Using Oracle 10g. All values are read out of web.config. We are using ASP.NET 2.0 (c#)
We are using more than one database schema, one for our application and one for a 3rd party application, for reference I will call them MYAPP and THEIRAPP respectively.
There are SQL statements in the c# code that make reference to THEIRAPP by name. For example: string sql = @"select * from THEIRAPP.address where zip = {0}";
This would work fine if in all environments ha开发者_JAVA技巧d the same naming convention, however, on the test environment the database admin used different name (for example THEIRAPP2), so the call to THEIRAPP.address don't work because it doesn't exist in that environment.
How can I change these queries in the C# code with the least amount of changes. I know that the SQL calls should be in stored procedures, but I didn't write the code, and don't have the lattitude to make those kind of changes (at least not at this time).
Any help or suggestions will be appreciated. Thanks in advance for your help.
The simplest answer, and the most common one, would be to ensure that the schema names always matched in the different environments.
If that is not possible, are there collisions between the table names? That is, is there a My_App.Address
table and a Their_App.Address
table? If there are no conflicts, you could simply create synonyms that point to the various tables and eliminate the schema names
CREATE SYNONYM address FOR my_app.address
CREATE SYNONYM person FOR their_app.person
If there are conflicts, you could still use synonyms, you'd just have to rename objects, i.e.
CREATE SYNONYM my_address FOR my_app.address
CREATE SYNONYM their_address FOR my_app.address
Barring that, you could potentially get away with changing the current schema before executing each query. If you do
ALTER SESSION SET current_schema = MY_APP
SELECT * FROM address
you'll automatically select from the my_app.address
table. If you do
ALTER SESSION SET current_schema = THEIR_APP
SELECT * FROM address
you'll automatically select from the their_app.address
table
Could you use a configuration file to store schema names? That seems the simplest solution.
Okay, some of this is environment specific (that is: how well separated are your Test and Production environments?) but- presuming you absolutely have to do this through code: use your config files.
Add a config section for your Sql statements (We'll call it "SqlStatements") and then have entries for each of your (currently) hardcoded statements.
Now, in your code, instead of: SqlCommand cmd = new SqlCommand("yourTextHere")
you call the correct sql statement out of the config file and then run your sql.
Then, you set up different config files for your test environment vs. your production environment. This also allows you to change your sql statements in the future without actually modifying your code.
But, as you said, the better answer (if you can get your bosses to agree) is to move the code to Stored Procedures and just call those.
精彩评论