开发者

SQL Server RODBC Connection

开发者 https://www.devze.com 2023-02-26 19:41 出处:网络
Does anyone have开发者_运维问答 a connection string example for using RODBC and connecting to MS SQL Server 2005 or 2008.

Does anyone have开发者_运维问答 a connection string example for using RODBC and connecting to MS SQL Server 2005 or 2008.

Thank you.


library(RODBC)
dbhandle <- odbcDriverConnect('driver={SQL Server};server=mysqlhost;database=mydbname;trusted_connection=true')
res <- sqlQuery(dbhandle, 'select * from information_schema.tables')


Taken from a posting to r-help:

 library(RODBC)
 channel <- odbcDriverConnect("driver=SQL Server;server=01wh155073")
 initdata<- sqlQuery(channel,paste("select * from  test_DB .. 
 test_vikrant"))
 dim(initdata)
 odbcClose(channel)


If you have to include the USERNAME and PASSWORD:

library(RODBC) # don't forget to install it beforehand

my_server="ABC05"
my_db="myDatabaseName"
my_username="JohnDoe"
my_pwd="mVwpR55zobUldrdtXqeHez"


db <- odbcDriverConnect(paste0("DRIVER={SQL Server};
                                 server=",my_server,";
                                 database=",my_db,";
                                 uid=",my_username,";
                                 pwd=",my_pwd))


sql="SELECT * FROM dbo.MyTableName" #dbo is the schema here
df <- sqlQuery(db,sql)


Try to use RSQLS package: https://github.com/martinkabe/RSQLS

Very fast pushes data from data.frame to SQL Server or pulls from SQL Server to data.frame.

Example:

library(devtools)
install_github("martinkabe/RSQLS")
library(RSQLS)

cs <- set_connString("LAPTOP-USER\\SQLEXPRESS", "Database_Name")
push_data(cs, dataFrame, "dbo.TableName", append = TRUE, showprogress = TRUE)
df <- pull_data(cs, "SELECT * FROM dbo.TableName", showprogress = TRUE)

This solution is much faster and more robust than RODBC::sqlSave or DBI::dbWriteTable.


First You have to Create/configure DSN (ODBC connection with specific DB)

Then install RODBC library.

library(RODBC)
myconn <-odbcConnect("MyDSN", uid="***", pwd="*******")

fetchData<- sqlQuery(myconn, "select * from tableName")
View(fetchData)
close(myconn)
0

精彩评论

暂无评论...
验证码 换一张
取 消