Tag: console application to connect to databas of dynamics crm

Simplified Connection To Dynamics CRM 2016 Onpremise Using Console Application

Here is the quick code that i use when i have to build a console application that connects to dynamics crm onpremise version with AD/IFD Authentication.

Main Class:

ClientCredentials _clientCreds = new ClientCredentials();
_clientCreds.Windows.ClientCredential.UserName = ConfigurationManager.AppSettings["username"];
_clientCreds.Windows.ClientCredential.Password = ConfigurationManager.AppSettings["password"];
_clientCreds.Windows.ClientCredential.Domain = ConfigurationManager.AppSettings["domain"];
var organizationUri = ConfigurationManager.AppSettings["CRMUrl"];

_service = (IOrganizationService)new OrganizationServiceProxy(new Uri(organizationUri), null, _clientCreds, null);

OrganizationServiceContext _orgContext = new OrganizationServiceContext(_service);

App.Config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="AppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>
<appSettings>
<add key="CRMUrl" value="https://Oranization.com/XRMServices/2011/Organization.svc" />
<add key="Username" value="rawishkumar" />
<add key="Password" value="password" />
<add key="Domain" value ="exampledomain"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>

 

Required Assemblies:

using System.ServiceModel.Description;

using System.Configuration;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Client;

Hope this helps!

SQL Connection to CRM Database

Dynamics CRM has very efficient web services using which you can retrieve data such as Organization service Or WebApi. However if you still need to retrieve data from database here is a quick code helper – which i am using in a console application.

//Initiate the connection
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString))
{
//create a command variable
using (var command = connection.CreateCommand())
{
//add query to your command
command.CommandText = "your SQLquery goes here";

// open the connection first and then execute the query using Execute Reader
connection.Open();
result = command.ExecuteReader();

here is the app.config which i have used , i am using the windows authentication hence you donot need to mention credentials.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="SQLServerConnection" connectionString="Server=.;Database=events.website;Trusted_Connection=True;"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>

By Now you will have your dataset in “result”; here is another piece of code to read the data:

 if (result.HasRows)
{
 while (result.Read())
{
string contactid= result["columnName"].ToString();
string contact = result["columnName"].ToString();
DateTime createdonDate= (DateTime)result["createdon"];
string type = result["columnName"].ToString();
}

}
// close the result
 if (result != null)
result.Close();


 

I hope this helps!

Cheers!