简体   繁体   中英

Changing default database in RODBC for sql server database

I have tried to connect a Sql server database to R using:

conn<-odbcConnect("dsnDb",uid="",pwd="")

It retrieves the default database 'master' instead of the database I need. How can I manually get a particular database?

So, there's no way to do this within the RODBC package, but here's a function which achieves the same thing:

Assuming your DSN is called "MyODBClink":

MakeChannel = function(dbName){
  system(paste("REG ADD HKEY_CURRENT_USER\\Software\\ODBC\\ODBC.INI\\MyODBClink /f /v \"Database\" /t REG_SZ /d ", "\"", dbName ,"\"", sep=""),intern=T)
  return(odbcConnect("MyODBClink"))
}

The way this function works is to edit the details of the registry (I'm assuming you're on Windows) for that particular DSN to point to the correct database, and then return an open connection to that DSN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM