.NET Equivalent to CreateQueryObj with Database Name parameter?

In the COM API you can specify the target database you want to run a query against using CreateQueryObj. For example CRM.CreateQueryObj ("select * from mytable","MYDB"). This uses the Advanced Customization->Tables and Databases->Database connection.

Is there an equivalent in the .Net API? The QuerySelect object doesn't seem to support this.

  • 0

    Hi Stacy,

    You have to use quryrecordresult method in .NET API. Here is an example which queries Address records.

    //Get address part

    queryrecordresult AddressRecordResult = CRM.queryrecord("*", "addr_addressid='" + PrimaryAddressId + "'", "Address", "addr_addressid");

    crmrecord[] AddressRecordList = AddressRecordResult.records;

    if (AddressRecordList.Length > 0)

    {

    //Loop through address records

    for (int intCount = 0; intCount

    {

    recordfield[] AddressFieldList = AddressRecordList[intCount].records;

    for (int intCount2 = 0; intCount2

    {

    recordfield AddressField = (recordfield)AddressFieldList[intCount2];

    //Add elements to Address root

    string AddressFieldName = AddressField.name.ToString();

    string AddressFieldValue = AddressField.value.ToString();

    }

    }

    }

    Hope this helps!

    Regards,

    Dinesh

    --------------------------------------
    Greytrix
    It's time to think outside the box.

    ** Meet the Sage X3 Certified Specialists at Sage Summit Booth # 288 - [email protected]**
    GUMU - Migration and Integration Solutions for Sage CRM, Sage CRM Cloud,
    Sage Pro, Sage 100/300/500 & ACT/SalesLogix by Swiftpage
    Greytrix :
    YouTube | Twitter | LinkedIn
    blogs: SageCRM | Sage 300 ERP | Sage ERP X3 | Sage 100 & ACT
    E-mail: [email protected] | web:
    www.greytrix.com | Support : Live Help

    ----------------------------------------------------------------------------------------------

  • 0

    And how do you connect to an external database? I don't think the above answers my question.

  • 0

    Stacy

    I think Dinesh gave the an example for the Web Services API.

    Below is the standard QueryObject equivalent

    string strSQL = "select oppo_stage, count(*) as t from vopportunity with (nolock) where oppo_status='In Progress' and oppo_assigneduserid=" + intUserID + " group by oppo_stage";

    QuerySelect queryObj = new QuerySelect();

    queryObj.SQLCommand = strSQL;

    queryObj.ExecuteReader();

    string strStage = "";

    while (!queryObj.Eof())

    {

    if (queryObj.FieldValue("t") != "0")

    {

    strStage = Metadata.GetTranslation("oppo_stage", queryObj.FieldValue("oppo_stage"));

    AddContent(strStage);

    }

    queryObj.Next();

    }

    You are correct that this does not have the 'database' parameter option that the ASP page equivalent has but you should be able to pass a fully qualified SQL statement that is directed to another database e.g.

    select * from panoplytech.dbo.contacts where type = 'supplier'

  • 0

    Stacy

    Please do make the enhancement request.

  • 0

    We are using QuerySelect, but we were hoping it (or another method) had a database parameter to avoid having to use a linked server (in our case the database is on another server), plus having to "hardwire" the servername and db name in the query itself. I'll request this as a feature request for the .NET API.