Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database name after underscore in connection string is being ignored

Tags:

c#

.net

sql

I am trying to do something which I believe is very simple. I have a connection string which I am grabbing from a database. When I create a new instance of SQL connection it looks (when I debug and look at the object) like it is populating the db connection string properly. However, the error messasge I get makes me think that all the information after the underscore in the database name is being eliminated.

Code Ex:

Database db = new SqlDatabase(ConnectionString); // Connection string appears correct
DbCommand dbCommand = db.GetStoredProcCommand("StoredProcName");
DataSet approverDataset = db.ExecuteDataSet(dbCommand); // when this executes, exception is generated

When I run this code I'm greeted with the following exception message: The server principal "testdb_psidentity" is not able to access the database "testdb" under the current security context

Here's my connection String:

DataSource=testserver.com;Database=testdb_ps;Trusted_Connection=false;uid=testdb_psidentity;pwd=testpwd

I've tried changing this to:

DataSource=testserver.com;Database=[testdb_ps];Trusted_Connection=false;uid=testdb_psidentity;pwd=testpwd

And while that seems to get the full name of the database, it also seems to be looking for a database with those brackets in the name as well. As I get this error: Cannot open database "[testdb_ps]" requested by the login. The login failed. Login failed for user 'testdb_psidentity'.

However, I can log connect to the database using that login/password in SQL server management studio.

Any way I can make it accept the database name testdb_ps database name? I'm unable to simply change the database name because I don't have access to this, and I've been told that changing the database name simply will not happen.

like image 539
C Wilson Avatar asked Nov 14 '22 03:11

C Wilson


1 Answers

You can try putting quotes around the connection string, I think it is interpreting the underscore as a delimiter.

Depending on how you use it use quotes or

"
like image 86
Andrew Hagner Avatar answered Dec 10 '22 14:12

Andrew Hagner