difference between server and DSN in RDOBC package - r

I'm trying to connect to a MS SQL database via RODBC package. The following is not working :
ch = odbcDriverConnect(paste0("Driver=MSODBC;DSN=D.ad009.win.org;database=LCA;UID=LCA-User;PWD=xxxx"))
Warning messages:
1: In odbcDriverConnect(paste0("Driver=MSODBC;DSN=D.ad009.win.org;database=LCA;UID=LCA-User;PWD=xxxx")) :
[RODBC] ERROR: state 08001, code 0, message [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Neither DSN nor SERVER keyword supplied
2: In odbcDriverConnect(paste0("Driver=MSODBC;DSN=D.ad009.win.org;database=LCA;UID=LCA-User;PWD=xxxx")) :
ODBC connection failed
But changing the DSN to server will solve the problem :
ch = odbcDriverConnect(paste0("Driver=MSODBC;server=D.ad009.win.org;database=LCA;UID=LCA-User;PWD=xxxx"))
ch
RODBC Connection 8
Details:
case=nochange
DRIVER=MSODBC
SERVER=D.ad009.win.org
UID=LCA-User
PWD=******
WSID=d04x
DATABASE=LCA
I was wondering what is the difference between DSN and server ???

Related

Trouble accessing oracle database using R (odbc package)

A colleague at work is having trouble using the odbc package function. I am trying to find help.
He is using an oracle database using R instead of running our traditional SAS programs, but he has not been successful. We are trying to find out what is causing the error messages below. Can someone help?
Attempt 1:
#Get the Oracle JDBC driver
jdbcDriver =JDBC("oracle.jdbc.OracleDriver",
classPath="C:/instantclient_19_10/ojdbc8.jar")
Create connection string to the Database we want
connect.string <-
glue("jdbc:oracle:thin:#//{host}:{port}/{sid}",
host = "stdbprd01.states.bls.gov",
port = 1521,
sid = "lausonep")
print(connect.string)
#Establish connection to your database
con <- dbConnect(jdbcDriver,
connect.string,
user = "username",
password = rstudioapi::askForPassword("Database password"))
Error in .jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
java.sql.SQLRecoverableException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
Attempt 2:
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
driver="Oracle in OraClient12Home1",
database="lausprd",
uid="aakre_n",
pwd="!QAZ1qaz#WSX2wsx",
host="stdbprd01.states.bls.gov",
port=1521
)
Error: nanodbc/nanodbc.cpp:1021: IM006: [Oracle][ODBC][ORA]-12560: TNS:protocol adapter error
[Microsoft][ODBC Driver Manager] Driver’s SQLSetConnectAttr failed

How to select a database with non english character in SQL Server using R with RODBC library

My problem is the following: I am using R language with RODBC library and I am trying to get a connection with a database, which its name contain non English character. In this case I wanted to establish a connection with a database with Czech republic alphabet, but in the future could be Chinese, Russian, etc.
Here is my code:
library(RODBC)
con <- odbcDriverConnect("driver={ODBC Driver 17 for SQL Server};server=<server_name>;trusted_connection=yes;database=<database_name_with_č_character>", DBMSencoding="UTF-8")
The error I get is:
Warning messages:
1: In odbcDriverConnect("driver={ODBC Driver 17 for SQL Server};server=<server_name>;trusted_connection=yes;database=<database_name_with_č_character>", :
[RODBC] ERROR: state 28000, code 18456, message [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user '<my_user>'.
2: In odbcDriverConnect("driver={ODBC Driver 17 for SQL Server};server=<server_name>;trusted_connection=yes;database=<database_name_with_č_character>", :
[RODBC] ERROR: state 42000, code 4060, message [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open database "<database_name_with_č_character>" requested by the login. The login failed.
3: In odbcDriverConnect("driver={ODBC Driver 17 for SQL Server};server=<server_name>;trusted_connection=yes;database=<database_name_with_č_character>", :
ODBC connection failed
I also tried to use a DSN connection that it works, but when I want to use the database with the non English character... I cannot.
con <- odbcConnect("dev_server", DBMSencoding="UTF-8")
dbName <- "<database_name_with_č_character>"
query <- paste("select top 1 * from [", dbName, "].<schema>.<table> ", sep="")
exitStatus <- sqlQuery(con, query, errors=T, as.is=T)
The error:
[1] "42S02 208 [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name '<database_name_with_č_character>..'"
[2] "[RODBC] ERROR: Could not SQLExecDirect 'select top 1 * from <database_name_with_č_character>..'"
Note: if I tried the following code with database's name that does not contain non English character the code works.
Software versions:
R: Microsoft- R Open - R-4.0.2
Microsoft SQL Server 2017 - 14.0.3381.3 (X64)
SQL Server running on Windows Server 2016 Standard (10.0)

(434) WSA E TimedOut: Lost socket connection to the Teradata server in R

I am trying to connect to Teradata in R using an ODBC connection. I use the same configuration on my local machine and it works. However, when I connect through a server it throws the below error.
I have set up an ODBC DSA connection using the teradata driver with all the connection credentials.
How do I fix this?
> con <- odbcConnect("Terr_Con")
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=Terr_Con") :
[RODBC] ERROR: state HY000, code 434, message [Teradata][socket error] (434) WSA E TimedOut: Lost socket connection to the Teradata server
2: In RODBC::odbcDriverConnect("DSN=Terr_Con") :
ODBC connection failed

RODBC connection from R to SQL failing

I'm using R Studio to connect to SQL Server Management using library(RODBC).
Note that the below code works on my local system, but does not work when trying to connect remotely.
This is my connection code:
connection <- RODBC::odbcDriverConnect( 'driver={SQL Server};server=SERVERNAME ;database=DBNAME;trusted_connection=TRUE' )
I get the following warning message:
Warning messages:
1: In RODBC::odbcDriverConnect("driver={SQL Server};server=SERVERNAME;database=DBNAME;trusted_connection=TRUE") :
[RODBC] ERROR: state 08001, code 18, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]SSL Security error
2: In RODBC::odbcDriverConnect("driver={SQL Server};server=SERVERNAME;database=DBNAME;trusted_connection=TRUE") :
[RODBC] ERROR: state 01000, code 772, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (SECDoClientHandshake()).
3: In RODBC::odbcDriverConnect("driver={SQL Server};server=SERVERNAME;database=DBNAME;trusted_connection=TRUE") :
ODBC connection failed
It's really odd because I definitely have access to the server and database in question.
It's worth noting that I have access to the server from a local system (above code works) and access to a remote system (code does not work).
Any idea why this might be happening?
RODBC is not a great package, it would be much better to use the RStudio built odbc instead:
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "SERVER",
Database = "DATABASE",
Trusted_Connection = "True" ,
timeout = 5000
)
In the following command line
connection <- RODBC::odbcDriverConnect( 'driver={SQL Server};server=SERVERNAME ;database=DBNAME;trusted_connection=TRUE' )
make trusted_connection = FALSE or no. Its a simple case of SSL error. If you are using linux, for server, use ip address rather than hostname.

How to connect Ms SQL server with R on Ubuntu 16.04?

I am unable to connect R with MS sql using RODBC as well as RJDBC
With RODBC
library(RODBC)
dbhandle <- odbcDriverConnect(paste("driver={SQL Server};server=", sname, ";database=",
dbname, ";uid=", user,";pwd=",password, sep = ""))
res <- sqlFetch(dbhandle, client.table_name)
The error I am getting is:
Warning messages:
1: In odbcDriverConnect(paste("driver=SQL Server;server=", sname, ";database=", :
[RODBC] ERROR: state 01000, code 0, message [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
2: In odbcDriverConnect(paste("driver=SQL Server;server=", sname, ";database=", :
ODBC connection failed
Also I have tried with RJDBC
library(rJava)
library(RJDBC)
drv <- JDBC(driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver",
classPath="/home/****/sqljdbc_4.2/enu/jre8/sqljdbc42.jar")
conn <- dbConnect(drv, paste0("jdbc:sqlserver://",client.hostname),
user=client.username , password=client.password, dbname = client.dbName)
The error is
Error in .jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. ClientConnectionId:253fd8d2-8e60-40fb-96b0-220745818166".
I got the same error when i put below code for connection to MSSQLSERVER
library(RODBC)
dbconnection <- odbcDriverConnect("Driver=SQL Server;Server=192.168.76.60; Database=kaggle;Uid=sa; Pwd=1234")
It throws to me
[unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
why this Error thrown?
Answer: when we fail to put proper ODBC version name on Driver value.
From where we can get Driver ODBC version name
inside "/etc" folder you will find "odbcinst.ini" file open it and check the version name
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.1.so.0.1
UsageCount=1
so i got ODBC Driver name from here , it will be "ODBC Driver 17 for SQL Server"
Then i modify my connection string
library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 17 for SQL Server;Server=192.168.76.60; Database=kaggle;Uid=sa; Pwd=1234")
And it works fine

Resources