How can I connect to SQL through RODBC? - sql

I am trying to connect R to SQL to upload data and I do not manage to use the connection string. I have tried the following code:
I keep getting the error: "SQL Server does not exist or access denied."
dbhandle <- odbcDriverConnect('driver={SQL Server};server=local;database=db;trusted_connection=true')
I would like to ask you what could have gone wrong?
It is probably related to driver or server. Could you please tell me where exactly I can find these information?
Also, please notice I am using Windows Authentication.

Related

ORA-12557: Connecting ORACLE to modelling software (EMBLEM)

I'm trying to make a connection between Emblem and Oracle. I've already made a similar connection using R which works fine. The image below shows the environment variables that I am using. I also have set up a driver via ODBC Data Source Administrator. Again, this set up has worked fine when connecting to R using ROracle.
However, when I try to connect via Emblem I get the following error: ORA-12557: TNS:protocol adapter not loadable. When in SQL Developer I can see that my connection uses LDAP instead of TNS, so I'm not sure why I have a TNS error (though my knowledge of these protocols are very limited). Can someone please advise on how to resolve this?
So far I've tried deleting ORACLE_HOME variable below but then I ran into OCI errors, so I reverted back to the original path.

how to use odbcDriverConnect when using shinyapps.io to show the tables retrieving from SQL by means of RODBC

I just asked a question about SQL not being linked when using shinyapps. “first argument“ error when using shinyapps.io, rodbc to show sql query result in web. This is about the error information "first argument is not an open rodbc channel" when I try to use shinyapps.io to share my SQL query results that I got from SQL database using RODBC.
Thanks a lot for Lars Br.'s help, I think I also need to try odbcDriverConnect instead of odbcConnect. I saw some suggestions from https://support.rstudio.com/hc/en-us/articles/225408367-Connecting-to-databases-using-RODBC-on-shinyapps-io, which could deal with my problem.
However, my problem is: as I am very new to all this stuff, I am confused about all the parameters in odbcDriverConnect. I tried it like:
ch <- odbcDriverConnect('driver=SQL Server;server=HANARB1P;database=testing;uid=****;pwd=****;TrustServerCertificate=true')
I have got the following information:
Warning messages:
1: In odbcDriverConnect("driver=SQL Server;server=HANARB1P;database=testing;uid=****;pwd=****;TrustServerCertificate=true") :
[RODBC] ERROR: state 08001, code 17, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied.
2: In odbcDriverConnect("driver=SQL Server;server=HANARB1P;database=testing;uid=****;pwd=****;TrustServerCertificate=true") :
[RODBC] ERROR: state 01000, code 53, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()).
3: In odbcDriverConnect("driver=SQL Server;server=HANARB1P;database=testing;uid=****;pwd=****;TrustServerCertificate=true") :
[RODBC] ERROR: state 01S00, code 0, message [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
4: In odbcDriverConnect("driver=SQL Server;server=HANARB1P;database=testing;uid=****;pwd=****;TrustServerCertificate=true") :
ODBC connection failed
So far, I only have DSN: HANARB1P. I do not know what driver, sever and database in odbcDriverConnect are. I cannot find the definitions online. Could someone explain to me?
I also find some information probably useful from http://docs.rstudio.com/shinyapps.io/applications.html. It saids: "If your shiny application needs to access a database that you have running behind your organization’s firewall, your IT department will need to know which servers will be connecting through the firewall. Below are the shinyapps.io IP addresses you must whitelist:". I do not know if this is related to my problem. And if so, how I can set up those things?
I think you need to try changing the driver types as mentioned in the below article: https://support.rstudio.com/hc/en-us/articles/225408367-Connecting-to-databases-using-RODBC-on-shinyapps-io . I am inferring that you are trying to access this database remotely, correct? If so, you need to be using the public IP address and port number for that database server- not the server's name.
con <- odbcDriverConnect('Driver=FreeTDS;TDS_Version=7.0;server="publicIPAddress";port"portNumber";database=testing;uid=****;pwd=****;')
Try with all 3 versions of TDS.
If this is your company's database, then you are most likely going to also have to allow access to it over the internet. Be careful though, doing this could have consequences and allow intrusions into your systems. Do you have an IT team that can handle this for you? It seems like you are unfamiliar with the impacts this could have to your company if done incorrectly, so I would leverage any skilled IT resources you have to do this correctly.
You will need to configure your router to allow port forwarding to your database (typically port 1433). This is the port number you will enter into your odbcDriverConnect call above. You will then also need to check your server's firewall settings- my understanding is that the standard rules are to block all incoming traffic to your SQL Server. This means you need to add an inbound rule that will allow your IP address to make a connection. You included a link to shinyapps.io in your post... are you hosting this application on shinyapps.io or just on your own computer? If you are deploying to shinyapps.io then you will also need to include those IPs listed in that article, in the allowed inbound connections I mentioned above.
My suggestion would be to work with your IT department on creating an whitelisted inbound rule on your firewall that only allows connections for those IPs needed to run your shiny app.

Cannot login to SQL Server 2008 remotely - Error 1326 - Incorrect user or pass

I have a problem trying to connect to SQL Server 2008 SP3. It's really strange, I have looked everywhere for an answer and couldn't find anyone experiencing the same problems. I'll try to explain.
I can connect to my server locally, both using Windows login and SQL Server authentication, but when I try to connect remotely, I get error 1326 and an error message specifying Incorrect username or password.
Then I login locally to SQL, but the error log doesn't show up anything.
Yesterday, it showed the login errors, but now it's not showing up anymore. It's like it's trying to connect to another instance of SQL, though I checked by doing SELECT ##VERSION and running osql -L from the command line, and both of them show just one server instance running.
What's even more strange, I just tried stopping the SQL Server service and logging in again. It still shows the same error: 1326 - Incorrect username or password. It should have told me that the server cannot be found or something like that. How does it show incorrect user or pass when the server is offline?
Any kind of help would be greatly appreciated. Thank you very much in advance.

Connecting to SQL server in ASP

I'm sure this has been answered before, but every example I try I can't seem to connect to my SQL Server.
I'm using SQL Server 2014, and the webpage is being hosted in IIS on Windows 7 x64.
I'm trying to get my webpages to build themselves recursively from information in a database, I've configured IIS to run ASP pages (I believe its classic ASP as opposed to ASP.net, but I may be wrong)
I started by looking at examples that basically returned all entries in in a particular column in a table, however when i got error messages on trying to load the page:
An error occurred on the server when processing the URL. Please contact the system administrator.
If you are the system administrator please click here to find out more about this error.
I tried stripping it down to bare minimum, i.e. just opening the connection to the database.
And even then I get the same error, here are a few of the examples I tried:
Example-1
Example-2
Example-3
Firstly if anyone could tell me how to debug it further, i.e. getting proper error messages that actually tell me something useful, that would be a good start.
If there's any information I missed then please ask.
If you've given your files the extension .asp then it is Classic ASP
Do you know what version of SQL Server you're using? If it's express then you need to say so in your connection string eg Data Source=yourserveraddress\SQLEXPRESS
I recommend using the OLEDB method in Example 3 rather than the ODBC method in 1 and 2 but they should all work. This is a good resource for Connection Strings
http://www.connectionstrings.com/sql-server/
If you follow the steps on this page you will be able to see more useful error messages
http://www.chestysoft.com/asp-error-messages.asp

How can i test my TSQL syntax?

Quick question: How do I get some kind of database to use to test my SQL syntax and create basic data.
I have Sqlite Code which I'll soon put on a server. I have SQL Server 2008 installed with visual studio 2010. I tried connecting to the database and had no luck.
I also tried using an .mdf file instead thinking it's a file and I won't have connectivity issues. Wrong, I still couldn't connect and I used this site to help me (i'm aware its 2005)
In that case I used:
var conn = new SqlConnection(#"Server=.\SQLExpress;AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;Database=dbo;Trusted_Connection=Yes;");
exception
Unable to open the physical file "C:\dev\src\test\SQL_DB_VS_Test\test.mdf".
Operating system error 5: "5(Access is denied.)".
Cannot attach the file 'C:\dev\src\test\SQL_DB_VS_Test\test.mdf' as database 'dbo'.
With trusted = no I get Login failed for user ''. (What user am I suppose to set?). I created the .mdf with Visual Studio somehow.
What if you try this connection string:
var conn = new SqlConnection(#"Server=.\SQLExpress;
AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;
Database=test;Integrated Security=SSPI;");
I don't think it's a good idea to call your database "dbo" (that's a SQL Server keyword - I wouldn't use it for my own purposes!), and also I believe you need to use Integrated Security=SSPI; to define Windows authentication - Trusted_Connection is not used for SQL Server connection strings, AFAIK.
Have you tried using SSMS to access your local instance? It's helpful for getting connected and getting everything setup. Also, I think the default install of Sql Express with VS only support trusted connections.
After creating the mdf file with visual studios right click the mdf and select properties. In it you'll see a row called Connection String. Copy/paste it into your app and it should connect. The key part is User Instance=True