How to retrieve data from a SQL Server db - ms-access

I'm writing a code snippet in VBA to pull data from a SQL Server 2008r2 database. However, I'm getting a error at Conn.Open: [Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [53].
Using ConnectionString.com and another SO Question, I've written this:
Function GetAddress() As String
Dim Conn As ADODB.Connection
Set Conn = New ADODB.Connection
Dim Rst As ADODB.Recordset
Set Rst = New ADODB.Recordset
Conn.ConnectionString = "Provider=SQLNCLI10;Server=12.34.5678;" & _
"Database=OurDB;User ID=myuserid;Password=mypw;"
Conn.Open
Set Rst.ActiveConnection = Conn
Rst.Open "Select top 1000 * from MainTable"
End Function
Googling the error suggested that the Named Pipes might be Disabled. Following that lead, I checked the SQL Server Configuration Manager and Named Pipes are quite Enabled (3rd order behind Shared Memory and TCP/IP).
How do I make this error go away? Am I using the right connection string?

Double-check this part of your connection string:
Server=12.34.5678
It looks like referring to the server by an IP address, but it's not an IP address.

Related

Can't connect to web hosted MySQL database

I am using VB.NET to connect to a MySQL database.
I have the following code:
Public Function getListOfDatabase() As List(Of String)
Dim SQL As String = "select * from covenusers"
Dim output As New List(Of String)()
' Set the connection string in the Solutions Explorer/Properties/Settings object (double-click)
Using cn = New MySqlConnection("Server=server33.web-
hosting.com;Port=3306;Database=dbname;Uid=dbusername;
Pwd=password123;")
Using cmd = New MySqlCommand(SQL, cn)
cn.Open() 'this is where it breaks
' do stuff
End Using
End Using
Return output
End Function
I'm using Imports MySql.Data.MySqlClient for the MySQL handling. I get the exception: Unable to connect to any of the specified MySQL hosts.
I know my server address, username, database name, and password are all correct. What exactly am I connecting to here? Does it go through SSH or something else?
Apparently shared hosts do not allow you to connect to the MySQL database directly. I had to set up a SSHClient using Renci.SSHClient (google it) and port forward my database through that. What a pain smh
Hopefully this will help someone having the same issue.

Excel VBA to SQL Database

I've just started looking into interacting with an SQL database via Excel VBA, and I started off with the basic connection code from MSDN:
Sub GetDataFromADO()
'Declare variables'
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=fatcoin;User ID=root;Password=root;"
objMyConn.Open
'Set and Excecute SQL Command'
strSQL = "select * from productlist"
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
'Copy Data to Excel'
ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)
End Sub
The issue I'm having is while I have an SQL instance running on my machine, on port 3306, which I can access using for example, HeidiSQL, every time I run this code I get an error message:
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied
I have also tried adding a port:
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost,3306;Initial Catalog=fatcoin;User ID=root;Password=root;"
And other such things. I can't see any reason it shouldn't work, but I haven't played with this much at all. I've tried searching through other threads to no avail.
Any ideas?
I am working with Excel 2010, on a 64-bit Machine, using MySQL 5.7
I should mention the above coding is being inputted into the "Module1" section of the VBAProject on Excel.
Thanks
Thanks for the answers.
I solved my own question via a helpful link I found that suggested I go into ODBC 32-bit (because Excel is 32-bit) and create a System DSN. Then, I used the connections string below:
objMyConn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=root;Data Source=localhost;Initial Catalog=fatcoin"
It worked a treat.

MySQL - Visual Studio Express 2013 Issue

I'm having a bit of trouble trying to connect to a MySQL database through Visual Studio Express for Web 2013.
I have done a heap of reading about it and understand that its not easy for Express to connect with MySQL but it can be done manually.
I have redone a website in ASP from its Original PHP version that was done a while ago by someone else.
I have the connection string from the PHP document which reads
"DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; PORT=1234; DATABASE=DB; UID=root;PASSWORD=123456789; OPTION=3"
So I went to the MySQL website and downloaded the ODBC connector for version 3.51 and installed it.
In my ASP.net code I have the following to get a list of tables within the database
Dim da As MySqlDataAdapter
Dim MysqlConn As New MySqlConnection
Dim ds As New DataSet
Dim slTables As ArrayList = New ArrayList()
Dim dr As DataRow
Dim sSQL As String = "select * from information_schema.tables"
MysqlConn.ConnectionString = "SERVER=000.000.00; PORT=1234; DATABASE=DB; UID=root;PWD=123456789"
MysqlConn.Open()
da = New MySqlDataAdapter(sSQL, MysqlConn)
da.Fill(ds)
For Each dr In ds.Tables(0).Rows
slTables.Add(dr("TABLE_NAME"))
Next
MysqlConn.Close()
When trying to connect I receive an authentication error regarding the username. Seeing as its both the same in the PHP and ASP connection strings, I'm not too sure why its not working. Is it something to do with not being on the Localhost server now? (I do have the actual IP address of the server).
If that's not the issue, any pointers? I'm thinking that I have either done something wrong, or that the way i'm trying to do it still cannot be done with the express version.
I have also tried using the MySQL Connector through the control panel and receive the same error.

Do I need to install drivers to connect to remote MYSQL database from Excel 2010 via VBA?

I am trying to build an application for my team the connects to a mysql database in Excel through vba. Below is my code to connect to the mysql database but when it runs it returns the following error:
[Microsoft ODBC Driver Manager] Data source name not found and no default driver specified
Do I need to install the mysql drivers in order to attach to the db? I was hoping that I wouldn't have to have everyone install them for this to work.
Is there another way or is something wrong with my connection string?
' Create a connection object.
Dim cnFEYS As ADODB.Connection
Set cnFEYS = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
'strConn = "PROVIDER=MySQLProv;"
'Use the MYSQL Driver.
strConn = "DRIVER={MySQL ODBC 5.1 Driver};"
'Connect to the database on abc123.
strConn = strConn & "SERVER=abc123; DATABASE=db123;"
'Use an integrated login.
strConn = strConn & " USER=user; PASSWORD=hello; OPTION=3"
'Now open the connection.
cnFEYS.Open strConn

Excel VBA connecting to remote MySQL Database

i am using the OBDC connector to use VBA to connect to my MySQL database. It currently runs on a local webserver (localhost) but is accessible from other PCs on the network via my PC's IP address.
In my connection function I had localhost as the location but when I change it to my IP address I get an
[MySQL][ODBC 5.2 Driver] (my computer name) is not allowed to connect to this MySQL server
error.
I presume this is a security problem. Any way to fix this?
Here is my connection function:
Public Function OpenConnection() As ADODB.connection
//This function requires the "Microsoft ActiveX Data Objects" Library (Choose v2.8 from references for compatibility across Office versions)
Dim source As String, location As String, user As String, password As String
source = "MySQL"
location = "192.168.1.60"
user = "root"
password = ""
database = "database name"
mysql_driver = "MySQL ODBC 5.2 ANSI Driver"
//Build the connection string
Dim connectionString As String
connectionString = "Driver={" & mysql_driver & "};Server=" & location & ";Database=" & database & ";UID=" & user & ";PWD=" & password
//Create and open a new connection to the selected source
Set OpenConnection = New ADODB.connection
OpenConnection.CursorLocation = adUseClient
Call OpenConnection.Open(connectionString)
End Function
You need to modify the user account in MySQL. It is possible to modify the locations users are allowed to connect from and their credentials. Look at this post:
Allow all remote connections, MySQL