This code works perfectly in R-Studio but there is no way to make it work in MS Management studio. It keeps on saying that:
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'myserver\LOCAL01'.
That is not my user give it is a trusted connection. Can someone help me understand it?
ALTER PROCEDURE [dbo].[TESTIM] AS
BEGIN
SET LANGUAGE ENGLISH
CHECKPOINTÂ
DBCCÂ DROPCLEANBUFFERS
EXEC sp_execute_external_script
#language = N'R'
, #script = N'
con <- "Server=myserver\\LOCAL;Database=mydb;Trusted_Connection=true";
sql <- RxInSqlServer(connectionString = con, shareDir = "c:\\TMP");
local <- RxLocalSeq(sql);
rxSetComputeContext(local)
ff <- RxSqlServerData(sqlQuery = "select top 1 * from mytable", connectionString = con);
t = rxImport(ff);
OutputDataSet <- data.frame(SUCCESS = TRUE);
'
WITH RESULT SETS (([SUCCESS] BIT))
END
So, when you execute sp_execute_external_script it executes under one of 20 Windows user accounts (worker accounts) that has been created during installation of SQL Server R Services. These accounts are created for the purpose of running tasks under a security token belonging to the SQL Server Trusted Launchpad service.
This works very well, but if you need to create a SQL connection inside your R script (as in your case) and you use trusted connection (Windows Authentication), you are executing under the user account mentioned above ('myserver\LOCAL01' in your case), and that account need to be given permission to log in to the SQL Server instance on your behalf.
To do this:
In SQL Server Management Studio, in Object Explorer, expand Security, right-click Logins, and select New Login.
In the Login - New dialog box, click Search.
Click Object Types and select Groups. Deselect everything else.
In Enter the object name to select, type SQLRUserGroup and click Check Names.
The name of the local group associated with the instance's Launchpad service should resolve to something like instancename\SQLRUserGroup. Click OK.
By default, the login is assigned to the public role and has permission to connect to the database engine.
Click OK.
That should do it (the above steps are copied from here.
If you want to read more about the user accounts you can have a look at my blog-post "Microsoft SQL Server R Services - Internals III".
Hope this helps!
Niels
Related
I am in a corporate environment.
I can easily establish a connection between R and a SQL database thanks to DBI and odbc packages this way:
con <- DBI::dbConnect(
odbc::odbc(),
driver = SQL.driver,
server = SQL.server,
database = SQL.database,
encoding = SQL.encoding
)
This implicitely uses the "Windows Authentication" method. However, the server that will have to run the above R code will not be using a Windows Account that has actually access to the MS SQL DB.
Therefore, I would like to know how can I explicitely specify another Windows Account prior to or within the code above?
Any help would be greatly appreciated.
In other words, I would like to reproduce following behavior in R the following manual process (on a Windows session that uses credentials not allowed to connect to the SQL DB):
SHIFT+right click on MSSSM,
"Run as a different user",
Enter Windows Account #2 (the one that actually has access to the DB),
Connect to SQL DB using Windows Authentication (the #2 one, not the Windows session one which does not)
Hope the context is clear.
For obscure corporate reasons, creating a local SQL account is not possible. Otherwise, thanks to UID/PWD of DBI/odbc it would be theoritically straighforward.
I have been working on SQL Server 2017 via R (on Rstudio as well as R on SSMS) and i am unable to connect to a specific database. I mention the database name in the connection prompt but, it gets connected only to the master database. Is there something that I am missing while connecting?
The syntax I use for connection is:
conn = "Driver={ODBC Driver 13 for SQL Server};server=;Uid=uid; pwd=pwd;Database = mydb"
I am trying to use both RevoscaleR as well as ODBC() package in Rstudio to connect to a specific database but, it still gets connected to master database. Using RStudio connections pane, if i try to explore the other databases, it shows only dbo schemas and no other schemas even if they exist. Can someone help me in figuring out what might have gone wrong?
Most likely the login you use (the uid) is not authorized for that particular database (it is not created as a user in that database).
Some example code you can run in SSMS as - for example - sa:
--switch over to the database in question
USE mydb
GO
CREATE USER uid FOR LOGIN uid;
The above code creates a user in the database in question with the same name as the login.
Hope this helps!
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)
Firstly, I know that there are a lot of similar topics, but none seem to solve my problem.
I have inherited a project where the front-end is built in ASP.NET and the CMS is built in classic ASP. I'm in the process of setting up a local development environment where I've copied the database from the live server and am configuring the website to connect to my new local DB.
The ASP.NET part is connecting with the connection string Data Source=.\\SQLEXPRESS; Database=DBNAME; Trusted_Connection=True; but I had to mess about with Application Pools and Permissions.
I'm having a problem with getting the classic ASP part of the website to connect to the SQL database. The error I am recieving is:
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "DBNAME" requested by the login. The login failed.
/cms/connections/Conn.asp, line 18
The code in that file is:
12. Dim Conn_STRING, URL, mailHost
13. Conn_STRING = "Driver={SQL Server}; Server=.\SQLEXPRESS; Database=DBNAME; Trusted_Connection=True;"
15. Dim conn
16. set conn = Server.CreateObject("ADODB.Connection")
17. conn.ConnectionString = Conn_STRING
18. conn.Open
I know it's a permission problem or some sort, but I can't get to the bottom of it. I'll appreciate any help at all :)
hmmmm, well, the asp.net part is using SQLExpress, connected via an attached file. DBNAME in the first example is going to refer to a db file name in the actual asp.net project. It's been quite a long time since I last used classic ASP, but I'd be STUNNED if it supports this. You need to attach the db file to an actual sql server and use it that way.
server=mycomputername\sqlexpress; database=DBNAME; user id=username; password=password
You'll need to open your database in sql express mngmt studio
http://www.microsoft.com/download/en/details.aspx?id=7593
Go into your security settings for that user and set the default database to "dbname" (whatver your real dbname is of course)
Ensure your app pool is set to run under the user that has permissions to that database. It may just be easier to enable mixed mode authentication and create a sql user to connect to the database rather than messing around with windows authentication on an app pool.
You wil lof course need to change your connection string then to support the sql authentication if you go that route.
http://msdn.microsoft.com/en-us/library/ms143705%28v=sql.90%29.aspx
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();