ODBC connection in AX7 (D365) - odbc

I am creating an ODBC connection to Import data from SQL (SSMS) to D365FO.
I have created a Test database and created an ODBC Data Source for it (DSN: TestDS, Server: local, Default database: TestDB).
Running SQL Server ODBC Data Source Test works fine:
Running connectivity tests...
Attempting connection
Connection established
Verifying option settings
Disconnecting from server
TESTS COMPLETED SUCCESSFULLY!
The Server Browser is running, class runOn server is set.
Here is a piece of my code:
public server static void main(Args _args)
{
OdbcConnection con;
LoginProperty loginProp;
loginProp = new LoginProperty();
loginProp.setDSN("TestDS");
con = new OdbcConnection(loginProp);
}
In the debugging window it throws me the following error:
"Object 'OdbcConnection' could not be created"
On the user side I am getting the following errors:
"ODBC operation failed. Unable to log on to the database."
"Unable to retrieve message for retval -1, ODBC call reason code -2, SQLSTATE = []"
As I know, ODBC for data import hasn't been yet supported in 2016. Is it supported now? I have found the exactly similar standard AX7 code, but I am not able to test if it works in standard.
Anybody had something similar to this and got it solved?

Related

Problem while trying to connect from R to SQL server using a service account not the default Windows authentication

I am in a corporate environment. Using packages DBI and odbc I can easily establish a connection between R and a SQL database like that:
con <- DBI::dbConnect(
odbc::odbc(),
driver = SQL.driver,
server = SQL.server,
database = SQL.database,
encoding = SQL.encoding
)
I understood that, without specific UID/PWD, the Windows authentication is used by DBI.
Since this R code will have to run on an external server (in fact, it is a shiny application behind), I need to specify a UID/PWD, and for that I requested and get "service account" from my IT department.
However, as soon as I specify the service account UID/PWD to DBI::dbConnect() I get a login failure:
con <- DBI::dbConnect(
odbc::odbc()
,driver = SQL.driver
,server = SQL.server
,database = SQL.database
,encoding = SQL.encoding
,UID = SQL.UID
,PWD = SQL.PWD
)
# Error: nanodbc/nanodbc.cpp:1021: 28000: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'xxxxxxxx'.
If I manually open MS SQL server and try to connect with this service account using the "SQL server authentication method" instead of "Windows authentication" I also have the same error.
According to the IT department, this is "normal" and I think I am supposed to launch MS SQL server using this service account as user, then to use the Windows authentication to connect. Funny thing is that we are not allowed to "run as" another user.
Therefore, assuming this mechanic would work, how one can mimic this behavior in R please?
I have a similar set up at work with personal creds and service acct creds to a MS SQLServer db. I use the following to connect with the service account...
Note that you will have to download the mssql jdbc jar file.
username <= "myusername"
password <- "mypassword"
drv <- RJDBC::JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
'mssql-jdbc-7.0.0.jre8.jar')
connection_string <- paste0("jdbc:sqlserver://10.10.10.01\\srv1;databaseName=dbname;user=",
username, ";password=", password, ";")
conn <- RJDBC::dbConnect(drv, connection_string)

Login Failed for ' '. when using DSN with Rstudio connecting to MS SQL 2016

Running into a odd error when trying to move a SQL connection details from R code to the DSN and using the DSN as the connection string.
Using the ODBC package, I can build a connection through RStudio using:
con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "XXX",
Database = "YYY",
uid = "username",
pwd = "password",
port = 1443)
This code above works correctly and allows the SQL connection. When moving this to the DSN (have attempted both user and system DSN) an error is generated. Both user and system DSN's have been tried and when connections are tested from the ODBC application in Windows (Win 10), the test is successful.
When using the code below in R, the connection fails.
con <- DBI::dbConnect(odbc::odbc(), "ZZZ")
Error: nanodbc/nanodbc.cpp:950: 28000: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user ''
This would seem to indicate that the username is unknown but my information is that all of the connection parameters are stored in the DSN entry. My initial setup was generated from:
https://support.rstudio.com/hc/en-us/articles/214510788-Setting-up-R-to-connect-to-SQL-Server-
Following the further discussion at the bottom of the page, only appears to discuss the code version of the connection.
https://db.rstudio.com/databases/microsoft-sql-server/
This link discusses the DSN component as well as ensuring the driver is available.
https://db.rstudio.com/getting-started/connect-to-database
This confirms the code structure when trying to use the DSN.
I also verified this code snippet via:
https://www.r-bloggers.com/setting-up-an-odbc-connection-with-ms-sql-server-on-windows/
I attempted to use the SQL Server Driver versus the Native Client version and received the same error except the ODBC SQL Server Driver identifier instead of the Native Client 11.0. This seems to indicate that the dbconnect call is at least identifying the DSN entry.
Using the code below I confirmed that the Driver for both SQL Server and native Client 11.0 were available.
sort(unique(odbcListDrivers()[[1]]))
Add trusted connection attribute
con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "XXX",
Database = "YYY",
Trusted_Connection = "True",
uid = "username",
pwd = "password",
port = 1443)
Not sure if you're encountering the same issue, but the credentials used during DSN creation (Windows account) were different from those used in the dbConnect function call (somehow, my Microsoft account).
The user in the error message did not exist in the Security roles within SQL Server, thus not granting access to connect to the database.
Granting the user in the error message access to the required SQL server/database fixed the issue*.
CREATE LOGIN [MicrosoftAccount\xyz#outlook.com] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]
ALTER SERVER ROLE [sysadmin] ADD MEMBER [MicrosoftAccount\xyz#outlook.com]
* though additional security measures are advised in role creation, off-topic here.

dynamics AX batch job through ODBC

I have problems with an ODBC connection used in a batch job: when executed, I have the following error message :
"The ODBC operation failed.Unable to connect to database
Invalid connection String attribute Trusted_Connection
Unabe to create object 'OdbcConnection' "
I try to connect to an Access database, I use the following code:
LoginProperty loginProperty;
OdbcConnection odbcConnection;
;
loginProperty = new LoginProperty();
loginProperty.setDSN("MyDSN");
loginProperty.setDatabase("MyDatabase");
odbcConnection = new OdbcConnection(loginProperty);
when debugging, it crashes on odbcConnection = new OdbcConnection(loginProperty). I tested the same code in a job in local client execution, there was no problem and it worked fine(without defining login password in code or DSN). My DSN seems to be OK on AOS, I'm using AX 2009 without SP1, I saw some examples where there was login/password defined in loginProperty object , the setUserName() and setPassword() methods are not available in my AX version loginProperty object. My class is executed on server-side (as any batch job).
Is there some particular setup to do to connect to ODBC datasource through batch job?
any ideas or suggestions?
thanks for your help
Edit:AOS is running on Windows server 2003 32 bits, ODBC tested on it, works fine but doesn't work in AX through batch job
Most likely you will need a 32-bit DSN connection for your AX. I would create both versions on the AOS server.
Look here for details.
It may be caused due to permisions. "trusted connection" refers to the fact that you connect to the database through the user logged to the current session. When you run a Job from the AX client, this is executed with the user that runs the current AX client (the one logged to the active session); When code is executed from server in a batch job, code is executed through the user that runs the AOS windows service.
Has this user permissions to read/write/access the access file and directory?
If you are on a 64 bit machine use C:\Windows\SysWOW64\odbca32.exe to create the datasource.
Check under which ID the AOS runs, log on with that ID and make sure you can 'see' the data source.
Also make sure that the AOS account has access to the directory/files (if using file-based odbc)

DSN driver architecture mismatch with application

In AX2012 I want to build an ODBC database connection to SQL Server 2008R2. Therefor I build the 32 Bit System-DSN ‘SQL Server Native Client 10.0’ (C:\Windows\SysWOW64\odbcad32.exe).
If I start the following code as job or directly in a button of a form, the connection to the database is correct.
{
LoginProperty loginProperty;
OdbcConnection myConnection1;
Statement myStatement;
SqlStatementExecutePermission perm;
str _query;
ResultSet result;
str retVal;
;
#macrolib.ocMegaBoard
loginProperty = new LoginProperty();
loginProperty.setDSN( #OCDSN );
myConnection1 = new OdbcConnection( loginProperty );
info('OK');
}
If I start the same code as part of different procedure calls, the following error message is coming:
‘[Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application‘
Please, can you help me?
Similar problems have been reported elsewhere.
Most likely your code works when executed by the client (32-bit), but fails when executed on the server AOS (64-bit). Just be sure to define your DSN on your AOS server as well, here as 64-bit.

DSN tests ok, but login failure trying to use the DSN to SQL Server 2005

I'm writing a small ASP.NET application in a hosted environment (meaning I don't own the server).
Using the hosting provider's webtools, I created a DSN that specifies the Driver, the Server, the UID, the PWD, and the Database. When I test the connection, it tests out fine.
However, when I load my web page with the code:
OdbcConnection DB = new OdbcConnection("DSN=MyDSNName");
DB.Open();
I get the error: ERROR [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user ''.
I know I'm using the correct DSN name because when I change to "DSN=NonExistentDSN" I get a different error.
I don't understand why the logon works when I test it but not when I use it in code. Since I don't own the server, some of the usual troubleshooting tools aren't available to me, but I'd appreciate any feedback the community has.
Perhaps the DSN does not retain the password. Have you tried to supply login credentials?
OdbcConnection DB = new OdbcConnection("DSN=MyDSNName;UID=login;PWD=password;");
DB.Open();

Resources