I was trying to connnect to a MSSQL database using the JDatabase-Class but got the "Could not connect to SQL Server database." error. After lots of hours checking SQL Server, Firewall, PHP... I checked the Joomla library and found the issue. The database name contains dots - and renaming database is not possible.
Connect to database and return connection id.
Error message "Could not connect to SQL Server database."
Joomla 3.9.14 on IIS 8.5, PHP 7.2
I found the solution in libraries/joomla/database/sqlsrv.php, line 775. In the select-method, which auto-selects the choosen database, the database name is not quoted, so if there is a dot (or maybe other characters) inside the database name, the USE-Statement fails and throws this error.
Original code:
if (!sqlsrv_query($this->connection, 'USE ' . $database, null, array('scrollable' => SQLSRV_CURSOR_STATIC)))
In my case, I just put the database name inside brackets and all works fine:
if (!sqlsrv_query($this->connection, 'USE [' . $database .'], null, array('scrollable' => SQLSRV_CURSOR_STATIC)))
Mabe the $this->quoteName()-method can be used for adding brackets.
Labels |
Added:
?
|
I tried to add square brackets using $this->quoteName() method now. The result is not as I expected; example: My database name is database2.0.3. Result should be [database2.0.3]; but I got [database2].[0].[3] and the connection fails.
an interesting reading on the subject https://dba.stackexchange.com/questions/1166/is-it-okay-to-put-dots-in-sql-server-database-names
Do not use period in database names, publication names, user names. And I strongly recommend never to use a period in name (column, table, view, database, etc).
Yes, I agree, but it wasn't my decision. This database is a external database of a key management system which I need to manage via joomla. The database still exists and I'm not able to change the database name. So, unfortunally, I do have to use it as it is... for my own databases I would't use dots or characters like this...
Result should be [database2.0.3]; but I got [database2].[0].[3] and the connection fails.
That's by design: The quoteName has a special treatment of dots. It assumes that they are separator between a table alias and a column name, e.g. a.id
with a
being the table alias, which has to be quoted with squared brackets as [a].[id]
in MS-SQL.
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2020-01-09 11:32:49 |
Closed_By | ⇒ | HLeithner |
looking at the Stack Exchange example [MAIN-SQL\EXPRESS].[MyApp.Sales].sch_HR.[Products From.Our-Competitors]
seams to be unfixable for quoteName.
I would suggest to not include the database name in the query if possible or to quote it by hand in your database queries.
I'm closing this. Feel free to reopen it.
looking at the Stack Exchange example
[MAIN-SQL\EXPRESS].[MyApp.Sales].sch_HR.[Products From.Our-Competitors]
seams to be unfixable for quoteName.
Yes, that's right, but as I wrote in my first post, the only thing that needs to be fixed is the select-method in sqlsrv.php on line 775 by adding square brackets for the database name. Its not necessary to use the quoteName-method in this case. The select-method is defined in sqlsrv.php, so square brackets are always the right choice for quoting the database name inside this file.
Code: 'USE [' . $database .'], null, array('scrollable' => SQLSRV_CURSOR_STATIC)
Although dots shouldn't be used for database name, this simple solution would fix it...
If this fix will not be merged, I will have to change it by hand after each joomla update. So could you please check if this fix can be merged?
If someone write a PR for it would merge it, in J4 we don't support sqlsrv any longer but you can still use the joomla framework to access sqlsrv.
your pull request is based on 3.10-dev but have to be based on staging
Its easy to change or @HLeithner can do it for you (I dont have the permissions to do it)
The problem is not the target branch, he used 3.10-dev as source branch. That can't be changed.
Ah - oops
@HLeithner Am confused now because to me it looks vice versa:
Into 3.10-dev from staging ... or am I getting something wrong?
@HLeithner I just see it is even worse: The PR wants to merge the CMS' staging branch into the CMS' 3.10-dev branch!!!
@HLeithner I've posted a link to instructions in the PR.
@dawe78 Please check #27462 (comment) and let us know if you need further advise.
Status | Closed | ⇒ | New |
Closed_Date | 2020-01-09 11:32:49 | ⇒ | |
Closed_By | HLeithner | ⇒ |
@dawe78 If you want to try it again with making a Pull Request (PR): Feel welcome.
You can find instructions here: https://docs.joomla.org/Using_the_Github_UI_to_Make_Pull_Requests.
But if you don't want let me know (i.e. post back here) then I can make one.
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2020-01-11 18:20:15 |
Closed_By | ⇒ | alikon |
i didn't have a sqlsrv instance for test right now ....
but if you can test...
if (!sqlsrv_query($this->connection, 'USE ' . $this->quoteName($database), null, array('scrollable' => SQLSRV_CURSOR_STATIC)))
probably brackets are uneeded.....iirc
please report back if you have the time to test...