web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM4.0 to CRM 2013 Database Connection

(0) ShareShare
ReportReport
Posted on by 3,455

Hi I need your advice, I customize some CRM 4.0 plugin to CRM 2013. I am newbie and I did not any exprience with CRM 4.0  My Question is ; I am trying customize a plugin to CRM 2013 . In this plugin there many coding for connection with database and I want to know am I neeed so much coding in CRM 2013  I attached some coding snip from CRM 4.0 and what I use for CRM 2013. Please advice me am I need all coding also for CRM 2013.

CRM 4.0 Database connection coding:

 
public static string connectToDB(string orgaName) {
            string confDataConString;
            using (RegistryKey mscrmKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\MSCRM")) {
                if (mscrmKey == null) {
                    throw new InvalidOperationException(@"RegKey gibts nich.");

                }
                confDataConString = (string)mscrmKey.GetValue("configdb", String.Empty);
                if (String.IsNullOrEmpty(confDataConString)) {
                    throw new InvalidOperationException(@"confingDB gibts nich.");
                }
            }
            using (SqlConnection con = new SqlConnection(confDataConString)) {
                using (SqlCommand cmd = new SqlCommand("SELECT ConnectionString FROM Organization WHERE UniqueName=@orgName", con)) {
                    cmd.Parameters.Add(new SqlParameter("orgName", orgaName));
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) {
                        DataTable organization = new DataTable();
                        adapter.Fill(organization);
                        if (organization.Rows.Count == 0) {
                            throw new InvalidOperationException(String.Format("Keine org die {0} heisst", orgaName));
                        }
                        if (organization.Rows.Count > 1) {
                            throw new InvalidOperationException(String.Format("Es gibt zu viele {0}´s", orgaName));
                        }
                        return (string)organization.Rows[0]["ConnectionString"];
                    }
                }
            }
        }

public static SqlConnection getDBConnection(string orgaName) {
            string DBConnection = connectToDB(orgaName);
            string[] DBConn = DBConnection.Split(';');
            string s = "";
            for (int i = 1; i < DBConn.Length; i++) {
                s = s + DBConn[i] + ";";
            }
            DBConnection = s;
            SqlConnection con = new SqlConnection(DBConnection);
            return con;
        }

 public static SqlConnection getCRMDBConnection(string orgaName) {
            string DBConnection = connectToDB(orgaName);
            string[] DBConn = DBConnection.Split(';');
            string s = "";
            for (int i = 1; i < DBConn.Length; i++) {
                s = s + DBConn[i] + ";";
            }
            DBConnection = s;
            SqlConnection con = new SqlConnection(DBConnection);
            return con;
        }


public static CrmService GetCrmService(string orgName, string server) {
            CrmAuthenticationToken took = new CrmAuthenticationToken();
            took.AuthenticationType = 0; //->Active Directory
            took.OrganizationName = orgName;
            CrmService serv = new CrmService();
            serv.CrmAuthenticationTokenValue = took;
            serv.Credentials = System.Net.CredentialCache.DefaultCredentials;
            serv.CrmAuthenticationTokenValue = took;
            serv.Url = string.Format("http://{0}/mscrmservices/2007/crmservice.asmx", server);
            return serv;
        }














and what I use in CRM 2013 instead of above coding:

  public void Execute(IServiceProvider serviceProvider)
        {
            string frage = "";
           
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
.
.
.
.
 public static SqlConnection getDBConnection()
        {

            string connectionstring = "Initial Catalog=TQUGROUP_MSCRM;Data Source=TQU-SQL;Integrated Security=SSPI;";

            SqlConnection con = new SqlConnection(connectionstring);

            return con;
        }

        public static int GetIntFromDB(string frage, SqlConnection con)
        {
            SqlCommand Kommando = new SqlCommand();

            string Kommandotext = frage;

            Kommando.Connection = con;

            Kommando.CommandText = Kommandotext;

            con.Open();

            int ergebnis = 0;

            try
            {
                ergebnis = System.Convert.ToInt32(Kommando.ExecuteScalar());

            }
            catch (Exception e)
            {

               // Log("SQL Abfrage " + frage + " hat keinen Wert zurückgegeben.");

               // Log("Fehler: " + e.Message);

            }
            con.Close();

           // Log("SQL Abfrage " + frage + " hat " + ergebnis + " zurückgegeben.");

            return ergebnis;

        }

Any insight would be greatly appreciated

*This post is locked for comments

I have the same question (0)
  • AaronRic Profile Picture
    10,035 on at

    Can you provide some additional detail as to what you are trying to accomplish?

  • necsa Profile Picture
    3,455 on at

    Hi Richards thanks a lot for connection.

    When some participant sign in over web site for any seminar then will create new seminar time with different information. forexample seminar name, time, where take place, price, tax etc. and then quote, salesorder, invoice from this seminar will create also generated for all new entitiy serial number. So who wrote this plugin for CRM 4.0 didn't add any commentar on ode for what it was  writing. So I must find by myself and I have not any exprience with CRM 4.0 plus!!! I am newbie (since 5 months) in CRM programming.

    In other plugin it was also so much Database connection for to get string, integer information from sqldatabase. I never understand. for what are there because some plugin I customize briefly like over how i wrote it and they are working but i am not sure for that will working also future. Sorry about long explanation. I hope i expressed myself with my English:)

    Other questions all this plugin registert pro stage but which one i customize for crm 2013 it gave null result with pro  stage registration., then i change insteadof postentityimage inputparameter and register as pre stage so it is working is it wright way or not when not what i must do for to get real result.

    Thanks a lot for your time sorry for the long text.

    Necdet Saritas

  • AaronRic Profile Picture
    10,035 on at

    Thanks Necdet. Unfortunately, this is outside the scope of what I can assist with here on the forums. Maybe one of the developer partners can chime in and assist, but you may need to work directly with a partner to create this custom code.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans