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
Related
Need help using a trusted connection to connect to SQL Server using SQLALCHEMY
So far I have this code to establish the connection
import sqlalchemy as sa
engine = sa.create_engine("mssql+pyodbc://REPORTING/REPORTING_System?driver={SQL Server}?TrustedConnection=yes")
view = f"SELECT Date From Table"
df = pd.read_sql_query(view, engine)
However I got this error message - DBAPIError: (pyodbc.Error) ('01S00', '[01S00] [Microsoft][ODBC Driver Manager] Invalid connection string attribute (0) (SQLDriverConnect)')
(Background on this error at: https://sqlalche.me/e/14/dbapi)
I tried to change the driver to {ODBC Driver 17 for SQL Server}
It did not help.
I've tried a wide range of connection strings trying to find one that will connect LogParser to my SQL Server 2017 (or SQL Azure) instance. I've tried both the ODBC and Native drivers. I've created and verified a DSN. Visual Studio will make the connection - LogParser's error message is misleading:
Error Message: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login
failed for user ''
The user id property was supplied -- i've tried 'User ID=xxx'; UserID=xxx' and 'User_ID=xxx'. And when hitting SQL Azure (instead of on-prem) I've tried with and without the #[servername] suffix.
The DSN version:
-o:SQL -createTable:ON -oConnString:"Driver={ODBC Driver 13 for SQL Server};DSN=odbc_bw33;Connection Timeout=30;" -e:1
Task aborted.
Error connecting to ODBC Server
SQL State: 08001
Native Error: 0
Error Message: [Microsoft][ODBC Driver 13 for SQL Server]Neither DSN nor
SERVER keyword supplied
Again, note that the connection string has a DSN name supplied - the error message is wrong.
I have been able to connect to SQLExpress with:
Driver={SQL Server Native Client 11.0};Server=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\seeder.mdf;Integrated Security=True;Connect Timeout=30;
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="")
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?
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;')