SQL Server Connection in R - r

I am trying to connect to SQL Server using R but getting error
library(DBI)
library(odbc)
conn <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "CNV***\\R***" ,
Trusted_Connection = "True")
error snippet
Error
Error: nanodbc/nanodbc.cpp:983: 01000: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SSL Security error [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (SECDoClientHandshake()).
But when I change to different server the connection get established
conn <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "XYZ***" ,
Trusted_Connection = "True")
I am not sure why I am getting this error , I would appreciate if someone could suggest what could be the reason or someone faced similar error and how to resolve.

Related

Trying to work on R using Azure ML Studio Notebook and facing challenges with ODBC package

I am trying to work on R notebook on ML Studio. Using regular python is easy and works as expected but with R i am facing challenges.
While trying to connect to MS SQL database using odbc() :
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "server",
Database = "db",
UID = "user",
PWD = "password",
Port = 1433)
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
As suggested in some posts, i have also tried replacing Driver = "SQL Server", with Driver = "ODBC Driver 11 for SQL Server". But i see similar error
Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 11 for SQL Server' : file not found
Traceback:
Please suggest a work around.
We can assign the Driver as the version of the SQL server and get the password using the API call.
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 13 for SQL Server",
Server = "your server IP address",
Database = "Database name",
UID = "User ID",
PWD = rstudioapi::askForPassword("password"),
Port = port_number_under_user)
In some cases, Driver = "SQL Server” will also work fine. If not functioning. Use the above code block.

How can I connect to an Azure SQL Server Database from R?

I am trying to connect to an Azure SQL Server Database from R, so I can create a table and upload data to the database.
This is how I connect to the server in SQL Server Management Studio
I have tried the following:
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "server_name, 1433",
Database = "database_name",
uid = "me#domain.com",
pwd = "password")
This results in the error:
Error: nanodbc/nanodbc.cpp:1021: HY000: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open server "domain.com" requested by the login. The login failed.
library(RODBC)
con.text <- paste("DRIVER={"SQL Server"}",
";Database=database_name",
";Server=server_name",
",1433",
";PROTOCOL=TCPIP",
";trusted_connection=true",
sep="")
con <- odbcDriverConnect(con.text)
This results in the warning messages:
1: In odbcDriverConnect(con.text) :
[RODBC] ERROR: state HY000, code 40607, message [Microsoft][ODBC SQL Server Driver][SQL Server]Windows logins are not supported in this version of SQL Server.
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
How can I successfully connect to this server from R?
Error: nanodbc/nanodbc.cpp:1021: HY000: [Microsoft][ODBC SQL Server
Driver][SQL Server]Cannot open server "domain.com" requested by the
login. The login failed.
The above error is mainly caused because of driver problem. Use this command to check whether drivers are properly installed or not.
library(odbc)
sort(unique(odbcListDrivers()[[1]]))
Connect Azure SQL database from R ,follow this Syntax:
library(odbc)
library(DBI)
library(RODBC)
con <- DBI::dbConnect(odbc::odbc(),
Driver = "[your driver's name]",
Server = "[your server's path]",
Database = "[your database's name]",
UID = "Database user",
PWD = "Database password",
Port = 1433)
Please follow the below reference it has detailed information about connecting Azure SQL to R in a detailed way:
Connecting to Azure SQL via R
https://db.rstudio.com/getting-started/connect-to-database
https://www.youtube.com/watch?v=ZkcBwYjHYOc

error nanodbc/nanodbc.cpp 1021 client driver version is not supported

Hi I am trying to connect r-studio to sql server but get the above error.
Could anyone tell me what might cause this issue?
I'm trying to connect as follow
library(odbc)
library(DBI)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "xxx",
Database = "xxx",
Trusted_Connection = "True")
Thanks

Connect to remote SQL Server with R

I'm trying to connect R to a remote SQL Server based on this. I use
library(DBI)
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "td",
host = "hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com",
username = "trades",
password = "u#6MS06")
but the connection is not working.Failed to connect to database: Error: Can't connect to MySQL server on 'hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com' (0) What do I miss? Note that I do not give the real credentials here. Then I want to connect with a table named "rep_user_listings".
After installing `ODBC Driver 11 for SQL Server I have also used :
library(DBI)
conn <- dbConnect(
drv = odbc::odbc(),
driver = "ODBC Driver 11 for SQL Server",
database = "td",
server = "hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com",
uid = "trade",
pwd = "u#6MS06X")
and I get:
Error: nanodbc/nanodbc.cpp:950: 08001: [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].
and
library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=trade; Pwd=u#6MS06Xv; trusted_connection=yes")
initdata <- sqlQuery(dbconnection,paste("select * from rep_user_listings;"))
odbcClose(dbconnection)
and I get:
Warning messages:
1: In odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=tradeshow; Pwd=u#6MS06Xvdoy; trusted_connection=yes") :
[RODBC] ERROR: state 08001, code -1, message [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
2: In odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=tradeshow; Pwd=u#6MS06Xvdoy; trusted_connection=yes") :
[RODBC] ERROR: state HYT00, code 0, message [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired
3: In odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=tradeshow; Pwd=u#6MS06Xvdoy; trusted_connection=yes") :
[RODBC] ERROR: state 08001, code -1, message [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.
4: In odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com\\SQLEXPRESS; Database=tdsh;Uid=tradeshow; Pwd=u#6MS06Xvdoy; trusted_connection=yes") :
ODBC connection failed
There isn't a native SQL Server client in development for R, the most recent I'm aware of (https://github.com/imanuelcostigan/RSQLServer) is archived/deprecated in lieu of the odbc package.
Try this:
library(DBI)
conn <- dbConnect(
drv = odbc::odbc(),
driver = "ODBC Driver 17 for SQL Server",
database = "td",
server = "hmsales.cav7mnk7ifa9.us-west-2.rds.amazonaws.com",
uid = "trades",
pwd = "u#6MS06")
(And if that works, immediately change your password. :-)
I chose "ODBC Driver 17 for SQL Server" because I previously installed the mssql driver for my windows and linux machines (current version available here), and found the driver "name" here:
unique(odbc::odbcListDrivers()$name)
# [1] "SQL Server" "PostgreSQL ANSI(x64)"
# [3] "PostgreSQL Unicode(x64)" "SQLite3 ODBC Driver"
# [5] "ODBC Driver 17 for SQL Server"
If you have an older version installed (such as 11 or 13) or something else, use that driver instead (or upgrade).
Firstly, don't give out your credentials online. Based on what i am looking at, open your terminal and ping your server to make sure you have network access to your server.
If you are using Linux one of the known issues is dont use domain name use ipaddess if you have also, make trusted connection =no.

Conntecting to SQL Server by R on macOS

I have a problem to connect to my external sql server database on R. I am working on macOS. I tried to do everything as in tutorial: LINK
My code:
library(DBI)
library(odbc)
con <- DBI::dbConnect(odbc::odbc(), Driver = "SQL Server", Server = "server", Database = "DataBaseName", UID = "Login", PWD = "password", Port = 1433)
but I still get this same error:
ERROR: nanodbc/nanodbc.cpp:950: 01000: [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
Does anyone know how to solve this problem? I will be very grateful!

Resources