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 AX (Archived)

connection to the sql database using a ' job ' x ++ code

(0) ShareShare
ReportReport
Posted on by
I tried to make a connection to the sql database using a ' job ' x ++ code .
In his connection managed server 1 .
but for the server 2 does not succeed .

error :

"Exception has been thrown by the target of an invocation.Login timeout expired
A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online".

then I was looking for know about the error.
for the security firewall on the server 1 and server 2 is made ​​equal ( firewall off ).
To enabled connectionnya already made ​​full .
all steps following link already done , but still wrote error to the process on the serve to 2.

http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/

Can help me ?
 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Jonathan  Halland Profile Picture
    11,310 on at

    Hi Hendra. Can you maybe post the code you are using. I use the following:

        LogInProperty Lp = new LogInProperty();
        OdbcConnection myConnection;
        Statement myStatement;
        str sql;
        SysSQLSystemInfo   systemInfo =  SysSQLSystemInfo::construct();
    
        LP.setServer(systemInfo.getLoginServer());
        LP.setDatabase(systemInfo.getloginDatabase());
        try
        {
            myConnection = new OdbcConnection(LP);
        
           myStatement = myConnection.createStatement();
           sql = "....";
           myStatement.executeUpdate(sql);
        info(strFmt("Executed statement: %1", sql));
        }
        catch
        {
            error("Failed to execute");
            return false;
        }
  • Hariharans87 Profile Picture
    3 on at

    Please test connect with SQL Server Management Studio to make sure that you are able to connect the database with authentication.

    Please refer this link msdn.microsoft.com/.../ee677510.aspx

    If you are interest, please use the Visual Studio Project for connect the external database. It is really useful and you can give better solution.

    dev.goshoom.net/.../connection-to-external-database

  • Community Member Profile Picture
    on at

    Hi Jonathan,

    I use code following :

       System.Exception                    e;

       System.Data.OleDb.OleDbConnection   objConn;

       System.Data.OleDb.OleDbCommand      cmdSelect;

       System.Data.OleDb.OleDbDataReader   reader;

       InteropPermission                   perm;

       str ConnectStr = "Provider=SQLNCLI10;Server=Server-    APP;Database=DynamicsAX2009;UId=sa;Password=123456";

       str                 exceptionStr,StrGetData,StrGetCount;

       boolean             ret = false;

       int                 i;

       try

       {

            perm = new InteropPermission(InteropKind::ClrInterop);

               if (perm == null)

               {

                   throw error("Error with file permissions");

               }

               perm.assert();

               objConn = new System.Data.OleDb.OleDbConnection(connectStr);

               objConn.Open();

               cmdSelect   = objConn.CreateCommand();

               cmdselect.set_CommandText(" select * from ListTransactionTabel ");

               reader = cmdSelect.ExecuteReader();

               while (reader.Read())

               {

                   i++;

                   reader.GetString(0)

               }

       }

        catch(Exception::CLRError)

           {

               CodeAccessPermission::revertAssert();

               perm = new InteropPermission(InteropKind::ClrInterop);

               if (perm == null)

               {

                     throw error("Error with file permissions");

               }

               perm.assert();

               e = ClrInterop::getLastException();

               CodeAccessPermission::revertAssert();

               while( e )

               {

                   exceptionStr += e.get_Message();

                   e = e.get_InnerException();

               }

               info(exceptionStr);

           }

           catch

           {

               error("An Exception has occurred");

           }

           if(objConn)

               objConn.Close();

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Hariharan,

    for its connection to the authentication database can be .

    scenario:

    I possessed two servers and two database sql .

    then I tried using x ++ code to read the database either from the server 1 (live app ) and server 2 ( dev app ) .

    2 server code syntax for its success . but for server 1 is not successful

    I will try to link its web .

    Thank you for the help.

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Hariharan S,

    I've tried the code below,

    his code work if run on the server .

    If run on the client does not succeed .

    an error :

    ODBC operation failed.

    Unable to log on to the database.

    [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

    Object 'OdbcConnection' could not be created

    can provide a solution if the code themselves can be run on the client as well .

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

    LoginProperty loginProperty;

       OdbcConnection odbcConnection;

       Statement statement;

       ResultSet resultSet;

       str sql, criteria;

       SqlStatementExecutePermission perm;

       ;

       // Set the information on the ODBC.

       loginProperty = new LoginProperty();

       loginProperty.setDSN("dsnName");

       loginProperty.setDatabase("databaseName");

       //Create a connection to external database.

       odbcConnection = new OdbcConnection(loginProperty);

       if (odbcConnection)

       {

           sql = "SELECT * FROM MYTABLE WHERE FIELD = "

               + criteria

               + " ORDER BY FIELD1, FIELD2 ASC ;";

           //Assert permission for executing the sql string.

           perm = new SqlStatementExecutePermission(sql);

           perm.assert();

           //Prepare the sql statement.

           statement = odbcConnection.createStatement();

           resultSet = statement.executeQuery(sql);

           //Cause the sql statement to run,

           //then loop through each row in the result.

           while (resultSet.next())

           {

               //It is not possible to get field 3 and then 1.

               //Always get fields in numerical order, such as 1 then 2 the 3 etc.

               print resultSet.getString(1);

               print resultSet.getString(3);

           }

           //Close the connection.

           resultSet.close();

           statement.close();

       }

       else

       {

           error("Failed to log on to the database through ODBC.");

       }

  • Suggested answer
    Jonathan  Halland Profile Picture
    11,310 on at

    Hi Hendra

    As far as I can determine, if you run on Server it will run under the AOS credentials which are basically guaranteed to be able to access the DB. If you run on client, it will run under your credentials which are unlikely to have access.  

  • Verified answer
    Hariharans87 Profile Picture
    3 on at

    Can you please check your connection string?

    I hope some times if we use \ in the string, it won't reflect, so, we need to use double backslash like \\

    Example:

    str ConnectStr = "Provider=SQLNCLI10;Server=Server-APP\TestInstance;Database=DynamicsAX2009;UId=sa;Password=123456";

    We need to use like below double backslash "Server-APP\\TestInstance"

    str ConnectStr = "Provider=SQLNCLI10;Server=Server-APP\\TestInstance;Database=DynamicsAX2009;UId=sa;Password=123456";

    You can use @ like this

    str ConnectStr = @"Provider=SQLNCLI10;Server=Server-APP\TestInstance;Database=DynamicsAX2009;UId=sa;Password=123456";

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Hariharan,

    connection to the database server has succeeded .

    by adding characters "@" on connectstr.

    Thank you very much.

  • Hariharans87 Profile Picture
    3 on at

    Could you please mark reply as verified?

  • Walid Gamal Profile Picture
    on at

    Hi

    Now I use the above code but when I try to insert the values in table it crash

    and when I print the values it gives me the data in the printing

    Can you help me how to change this code to insert into another table

    Regards.

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 AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans