R RODBC SQL Server Connection - sql-server

I use windows authentication to connect to sql server. And I'm trying to connect to the sql server table in R. But R is not connecting to the database. I also tried typing in my windows login for uid and pwd. still no luck.
library(RODBC)
driver.name <- "SQL Server"
db.name <- "dw-xxx"
host.name <- "xx-xxx"
port <-"xxxx"
server.name <-"dw-xx"
con.text <- paste("DRIVER=",driver.name,
";Database=",db.name,
";Server=",server.name,
";Port=",port,
";PROTOCOL=TCPIP",
";trusted_connection=true",
sep="")
con1 <- odbcDriverConnect(con.text)
Warning messages:
1: In odbcDriverConnect(con.text) :
[RODBC] ERROR: state 42000, code 4060, message [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "dw-xxx" requested by the login. The login failed.
2: In odbcDriverConnect(con.text) :
[RODBC] ERROR: state 01S00, code 0, message [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
3: In odbcDriverConnect(con.text) : ODBC connection failed
> odbcGetInfo(con1)
Error in odbcGetInfo(con1) : argument is not an open RODBC channel
RStudio : Version 1.0.153
Microsoft SQL Server Management Studio 14.0.17177.0

I got this same problem. Apparantly the PORT format was not accepted. It was working when I moved the port to the server part likes this (giving SERVER=server,port):
con.text <- paste("DRIVER={",driver.name,"}",
";Database=",db.name,
";Server=",server.name,
",",port
";PROTOCOL=TCPIP",
";trusted_connection=true",
sep="")

Related

How to get the SQL Server name and the other arguments of odbcConnect function in RODBC package?

I want to connect to SQL Server using R and am trying my hand at RODBC package.
In the odbcConnect function, i confused about the arguments:-
For the Server, i went to regedit and odbc and saw SQL Server Native Client 10.0
For database, i wrote Tableau (which is the name)
For uid and pwd i gave my credentials
Following is the code that i wrote:-
cn <- odbcDriverConnect(connection="Driver={SQL Server Native Client 10.0};server=.;database=Tableau;trusted_connection=yes;uid=SQL_User;pwd=Greentableau!")
But i got error in the code:-
Warning messages:
1: In odbcDriverConnect(connection = "Driver={SQL Server Native Client 10.0};server=.;database=Tableau;trusted_connection=yes;uid=SQL_User;pwd=Greentableau!") : [RODBC] ERROR: state 08001, code 2, message [Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
2: In odbcDriverConnect(connection = "Driver={SQL Server Native Client 10.0};server=.;database=Tableau;trusted_connection=yes;uid=SQL_User;pwd=Greentableau!") :[RODBC] ERROR: state HYT00, code 0, message [Microsoft][SQL Server Native Client 10.0]Login timeout expired
3: In odbcDriverConnect(connection = "Driver={SQL Server Native Client 10.0};server=.;database=Tableau;trusted_connection=yes;uid=SQL_User;pwd=Greentableau!") :[RODBC] ERROR: state 08001, code 2, message [Microsoft][SQL Server Native Client 10.0]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.
4: In odbcDriverConnect(connection = "Driver={SQL Server Native Client 10.0};server=.;database=Tableau;trusted_connection=yes;uid=SQL_User;pwd=""""!") :
ODBC connection failed
Can anyone recommend any solution? I don't understand the errors too well

How to access a database file .MDF from R

I am trying to connect to the AdventureWorks2012 database (MDF file) from R. The file is stored on my local computer. I have been able to setup SQl Server and the SQL Server Management Studio.
In Server Management Studio I have my PC name (2NDHANDBRUTUS) as the server and I can find AdventureWorks2012 :
However, I am not able to connect to it via R through the odbcConnect() statements. I have setup the following datasource:\
I have tried the following R command:
CON <- odbcConnect('AdventureWorks2012', uid = 'Rob Oudehand', pwd = 'Hondenmand5')
And I receive this error:
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=AdventureWorks2012;UID=Rob Oudehand;PWD=Hondenmand5") :
[RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In RODBC::odbcDriverConnect("DSN=AdventureWorks2012;UID=Rob Oudehand;PWD=Hondenmand5") :
ODBC connection failed
Can someone assist me in how I should access such a MDF-file from R? Should it be done via SQl Server via ODBSConnect, or is there another way?

Connecting to SQL Server from R which is Running on Remote UNIX Machine

We have a UNIX machine which includes R engine on it and installed SQL Server ODBC Driver 11.
We need to connect R to SQL Server. SQL Server currently works on another machine, so we tried to connect R to SQL Server with the code below.
library(RODBC)
dbhandle <- odbcDriverConnect('driver=ODBC Driver 11 for SQL Server;server=10.XXX.XX.XX:1663;')
But getting that error:
Warning messages: 1: In odbcDriverConnect("driver=ODBC Driver 11 for SQL Server;server=10.XXX.XX.XX:1663;") :
[RODBC] ERROR: state HYT00, code 0, message [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired 2: In odbcDriverConnect("driver=ODBC Driver 11 for SQL Server;server=10.XXX.XX.XX:1663;") :
[RODBC] ERROR: state 08001, code 87, message [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]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. 3: In odbcDriverConnect("driver=ODBC Driver 11 for SQL Server;server=10.XXX.XX.XX:1663;") :
[RODBC] ERROR: state 08001, code 87, message [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. 4: In odbcDriverConnect("driver=ODBC Driver 11 for SQL Server;server=10.XXX.XX.XX:1663;") : ODBC connection failed
If I run telnet to SQL Server from UNIX machine, it works perfectly. I mean there is a healthy connection between these two machine.
How can we connect R to SQL Server without using DSN?
I changed delimiter which between server and port from : to , . It fixed my problem.
dbhandle <- odbcDriverConnect('driver=ODBC Driver 11 for SQL Server;server=10.XXX.XX.XX,1663;')

unable to connect to azure vm database from R

I am trying to connect to a Microsoft SQL Server database running on a virtual machine in Microsoft Azure, from R.
Here is what my SQL server looks like
This is my connection string
library(RODBC)
channel = odbcConnect(dsn="something.cloudapp.net",uid="myusername",pwd="mypassword");
However, I keep getting this error
Warning messages:
1: In odbcDriverConnect("DSN=servername.cloudapp.net,1433;UID=myusername;PWD=mypassword") :
[RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In odbcDriverConnect("DSN=servername.cloudapp.net,1433;UID=myusername;PWD=mypassword"):
ODBC connection failed
Why do I keep getting this error?
Try using the more flexible odbcDriverConnect function like this:
odbcDriverConnect('driver={SQL Server};Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;')
Here it is with carriage returns for readability:
odbcDriverConnect(
'driver={SQL Server};
Server=myServerAddress;
Database=myDataBase;
User Id=myUsername;
Password=myPassword;')
See also:
SQL Server RODBC Connection
http://www.connectionstrings.com/sql-server/
http://www.inside-r.org/packages/cran/rodbc/docs/odbcConnect

sqlstate=42000 on sqlconnect (HDBC),Login failed

I'm getting the below error while connecting the sql server through unix box, i have created the DSN for the sql server and passing the user id and pass word to connect the sql server,
here is the error
SQLSTATE=42000 ON SQLCONNECT(HDBC) DIAGNOSTIC=[FreeTDS] [SQL SERVER]
Login Failed.

Resources