Connect to Dynamics 365 database
EDIT : You can also query Dynamics 365 / CDS SQL Server by using C# with this article.
Presentation
A new feature is coming this year and is only available as preview for the moment. You are now able to connect your Dynamics 365 database. This SQL connection provides a read-only access to the CDS environment. You can see each column type of each entity and you can execute SQL request directly in the databases.
Prerequisites
- Having a dynamics 365 Online account with administrator credentials
- Set up an environment which has at least version 9.1.0.17437
- Download Microsoft Sql Server Management studio or Power BI Desktop depending which software you want to use.
Check your environment version
As I said in the prerequisites, your environment you want to access must have at least version 9.1.0.17437.
To verify that, go to your environment (https://********.crm4.dynamics.com), Settings -> About
Here you can see the server version : 9.1.0000.18647
If the version is higher than 9.1.0.17437, we can go to the next step.
Enables Tabular Data Stream (TDS) endpoint
This option is only available for environments enabled for this public preview and with a recent version.
I advise to use Internet Explorer to do it.
To enable it, go to your environment list in the Power Platform admin center -> https://admin.powerplatform.microsoft.com/environments
From there, select your environment and open its settings.
In the Settings page, open up the Product section and click on Features.
If the link doesn't work, you can make it yourself.
First, take the id of your environment in the url:
Then generate the Features link: https://admin.powerplatform.microsoft.com/environments/yourenvironmentid/settings/Features
You can easily activate the TDS from this page:
Now you can access your datas as read-only.
We will see how to connect using Microsoft Sql Server Management studio or Power BI Desktop, depending what you prefer.
Using Microsoft Sql Server Management studio
Open your Microsoft Sql Server Management studio and connect to your environment.
- Server Type: Database Engine
- Server name: your crm url and the port 5558 separated with a comma -> ********.crm4.dynamics.com,5558
- Authentication: Azure Active Directory - Password
- User name: your admin username
- Password: your admin password
Click on Connect.
Now you are connected, you can access the database in read-only mode.
Following the user you are connected with, you will be limited to request the entities you have the read right set in the CRM.
You can use SQL to request the database.
Be aware that not all operation are supported within this feature. You can only use the following:
- SELECT
- UNION
- JOIN
- FILTER
- Batch operations
- Aggregate operations like COUNT() and MIN() or MAX()
Performance
On the following link, a performance comparison has been done between the FetchXML Builder add-on for the XrmToolBox and the T-SQL.
The performance will really depend on the query you are running, the number of column you are requesting.
Read the Joe D365 article which really well explains the tests and gives great conclusions about them.
Using Power BI Desktop
To open your database through Power BI Desktop, we will generate a pbids file in Power Apps.
Connect to Power Apps, then go to Data -> Entities
Then download the pbids file clicking on Analyse in Power BI
Now, double click on the downloaded file. it should be opened with Power BI Desktop.
You will be asked to sign in with your credentials:
Once signed-in, you have now access in read-only to all your tables.
As you can notice in the application, the storage mode used is Direct Query. It means that data are loaded in real-time!
Following the user you are connected with, you will be limited to request the entities you have the read right set in the CRM.
You can start working with them.
Limitations
- This is a preview feature and you're not supposed to use it for a production environment.
- The databases are only accessible in read-only mode.
- Not every operation are available in SQL within this feature.
Comments
Use Azure Active Directory authentication
Very good explanation. However, I don't understand the difference between an oData access via Powerbi and a Tabular Data Stream access.
Moreover, I don't have Use Azure Active Directory authentication under Sql
Have a nice day!
Server name did not work with port name
Hi, I loved your post this is well explained and useful
I was unable to connect Microsoft Sql Server Management studio by appending port number to the org url
where as when i tried only by org url it worked, Also can you please help me that from where did you get port number?
Hi Meghna, this port are…
Hi Meghna, this port are coming from the official MS documentation wherein they say :
Ports 1433 and/or 5558 need to be enabled to use the TDS endpoint from a client application such as SSMS. If you only enable port 5558, the user must append that port number to the server name in the Connect to Server dialog of SSMS - for example: myorgname.crm.dynamics.com;5558.
https://docs.microsoft.com/en-us/powerapps/developer/data-platform/dataverse-sql-query
Hello, This may not always…
Hello,
This may not always work, probably.
TITLE: Connect to Server
------------------------------
Cannot connect to https://www.xxxx-e.xxx.dynamics.com,5558.
------------------------------
ADDITIONAL INFORMATION:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - Hôte inconnu.) (Microsoft SQL Server, Error: 11001)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&EvtSrc…
Hello Francky, It should…
Hello Francky,
It should work. Please follow closely step by step the tuto maybe you miss a step ?
Best
TDS connection in C#
I am despairing :-o. The TDS connection in SSMS works fine. Not in my code (.Net Core)
var sqlConnectionString = @"
Data Source=xxxxx.crm4.dynamics.com;
Initial Catalog=xxxxx;
User ID=;
Password=;
Authentication=ActiveDirectoryServicePrincipal;";
using var cn = new SqlConnection(sqlConnectionString);
cn.Open();
cn.Open() says: "Microsoft.Data.SqlClient.SqlException: 'Login failed: The HTTP request was forbidden with client authentication scheme 'Anonymous'." Can everyone help me? Thanks! ;-)
Hello Karsten, Did you…
Hello Karsten,
Did you check this article ? : https://dynamics-chronicles.com/article/dynamics-365-dataverse-access-sql-server-database-c
Thx
Dataverse as linked server
Hi, thanks for the help. Have you checked connect it as Linked Server? If so, can you share the settings?
Im receiving the "unknown" error
SE [master]
EXEC master.dbo.sp_dropserver @server=N'AZURE', @droplogins='droplogins'
GO
EXEC master.dbo.sp_addlinkedserver @server = N'AZURE', @srvproduct=N'', @provider=N'MSOLEDBSQL', @datasrc=N'XXXXXXXXXX.api.crm4.dynamics.com', @provstr=N'Authentication=ActiveDirectoryPassword'
--[SQLNCLI11] MSOLEDBSQL
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'collation compatible', @optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'data access', @optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'dist', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'pub', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'rpc', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'rpc out', @optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'sub', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'connect timeout', @optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'collation name', @optvalue=null
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'lazy schema validation', @optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'query timeout', @optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'use remote collation', @optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption @server=N'AZURE', @optname=N'remote proc transaction promotion', @optvalue=N'true'
GO
USE [master]
GO
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'AZURE', @locallogin = NULL , @useself = N'False', @rmtuser = N'XXXXXXX@XXXXXX.com', @rmtpassword = N'XXXXXX
GO
Hi, I never tried to use it…
Hi, I never tried to use it with a Linked Server... I dont know any limitations about it.
If you find a way to make it work, don't hesitate to share it here, I will update the article with your precedure.
Best ;)
Linked server
Dear, is it also possible to connect to the TDS endpoint using an SSIS package or linked server (remote query) ?
Thanks,
Tim
Usage in SSRS Reports for this feature
Hi, would you know if you can access this feature via SSRS? If so, how do you connect to it (what data source type and connection string)?
Hi Kelvin, I never tried,…
Hi Kelvin,
I never tried, but it should be possible since SSRS should support this kind of connection, exactly as SSMS can do.
Good luck and don't hesitate to come back to us with your result !
Best
Writable access
Hi,
Great feature. Besides readonly, does it support writable as well, like a normal SQL Server database?
No, not possible at all to…
No, not possible at all to write on it. That's the legacy of the on premise version, in fact, it's a security for the integrity of the application to not allow writing on DB and avoid mistakes.
How to connect D365 F&O
Hi,This document contains how CRM is connected.
I want to know F&O whether or not connect to the database in this way,thanks.
Is this possible for D365 Finance and Operations?
Does Microsoft provide the same option to connect to the underlying database for D365 Finance and Operations? This is previously branded as MS Dynamics AX2012
Possible to export / backup the data
Is it possible to use the backup or export functionality to get all the data out of the D365 server and dump it to another location such as Azure DB?
Yes, you should consider…
Yes, you should consider using an ETL such as Azure Data Factory. but you cannot use SQL Server backup option here.