trying to connect mysql with vb.net - mysql

I've found this at connection strings.com
http://connectionstrings.com/mysql
Do I need to download connector-net from this site: http://dev.mysql.com/downloads/connector/net/
I recycled the code that I used in connecting vb.net with ms sql:
Imports system.data.sqlclient
idnum = TextBox1.Text
lname = TextBox2.Text
fname = TextBox3.Text
skul = TextBox4.Text
Using sqlcon As New SqlConnection("Server=localhost;Port=3306;Database=testing;Uid=root;Pwd=mypassword;")
sqlcon.Open()
Dim sqlcom As New SqlCommand()
sqlcom.Connection = sqlcon
sqlcom.CommandText = "INSERT INTO [student](ID, LASTNAME, FIRSTNAME, SCHOOL) VALUES (#ParameterID, #ParameterLastName, #ParameterFirstName, #ParameterSchool)"
sqlcom.Parameters.AddWithValue("#ParameterID", TextBox1.Text)
sqlcom.Parameters.AddWithValue("#ParameterLastName", TextBox2.Text)
sqlcom.Parameters.AddWithValue("#ParameterFirstName", TextBox3.Text)
sqlcom.Parameters.AddWithValue("#ParameterSchool", TextBox4.Text)
sqlcom.ExecuteNonQuery()
End Using
But I get this error:
A network-related or instance-specific
error occurred while establishing a
connection to SQL Server. The server
was not found or was not accessible.
Verify that the instance name is
correct and that SQL Server is
configured to allow remote
connections. (provider: Named Pipes
Provider, error: 40 - Could not open a
connection to SQL Server)
Please help, what solutions would you recommend to this problem?

This article should get you started:
The VB.NET-MySQL Tutorial – Part 3
The article uses MySQL Connector for .NET...
MySQL Connector/NET is available for
download at
http://dev.mysql.com/downloads/connector/net/.
Download the version that includes an
installer to your local hard-drive and
extract the Zip file.
Double-click the installer file to
begin the installation process.
Perform a complete install to the
default directory.
In the code you have:
conn = New MySqlConnection()
conn.ConnectionString = "server=" & txtServer.Text & ";" _
& "user id=" & txtUsername.Text & ";" _
& "password=" & txtPassword.Text & ";" _
& "database=in_out"
Check this out too:
Accessing MySQL Database from my VB.NET 2008 Project

Related

Excel VBA connection to external MySQL server (ODBC 8.0)

I wanted to connect an excel file with a MySQL server for a project, so I searched the internet for a solution. I found some, but all of it was old and I don't prefer 8–10-year-old methods and drivers.
All I got is [Microsoft][ODBC Driver Manager] Data source name not found, and no default driver specified
So, I setted up the following setup:
A MySQL 8.0 server (on a Ubuntu server in the oracle cloud) 64-bit, ODBC connector 8.0 Unicode 64-bit
Office 365 64 bit
I tried to make a connection with some of the methods, but they gave all the same error:
[Microsoft][ODBC Driver Manager] Data source name not found, and no default driver specified
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
location = My server IP
user = "ExcelTest"
password = "Pass"
database = "Test"
mysql_driver = "MySQL ODBC 8.0 Driver" ''Tried "MySQL ODBC 8.0 Unicode Driver" too
''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
Set OpenConnection = New ADODB.Connection
OpenConnection.CursorLocation = adUseClient
Call OpenConnection.Open(connectionString)
End Function
Usually I found a pretty good tutorial or walkthrou but this time nothing up to date.
I yes. I am pretty sure All the things are 64 bit.
I do not know where to search Please help
Test the driver has installed correctly by using Windows ODBC Data Source Administrator first.
Option Explicit
Sub test()
Dim conn
Set conn = OpenConnection()
With conn
.CursorLocation = adUseClient
MsgBox "Connected to " & .DefaultDatabase, vbInformation
End With
End Sub
Public Function OpenConnection() As ADODB.Connection
Const location = ""
Const user = ""
Const password = ""
Const database = "test"
Const mysql_driver = "MySQL ODBC 8.0 Unicode Driver"
' Build the connection string
Dim s As String
s = "Driver={" & mysql_driver & "};Server=" & _
location & ";Database=" & _
database & ";UID=" & _
user & ";PWD=" & password
Debug.Print s
' Open connection
Set OpenConnection = New ADODB.Connection
OpenConnection.Open s
End Function

How To Insert Data in Hosting Database Using VB.NET

I've a Hosting that contains mysql database, and i simply want to insert data into that database but using VB.NET.
Ex :
There's a Textbox and a button, when click on button is performed the value from that textbox have to be inserted in database of hosting.
What service or functions should i use??
Dim Conn As New ADODB.Connection
Conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & server & ";DATABASE=" & database & ";UID=" & user & ";PWD=" & pass & ";PORT=" & port & ";"
Conn.Open()
Conn.Execute("insert into tblname (fld1,fld2) values('"& textVal &"',"& numericVal &")")
Conn.Close()
Conn = Nothing
Paste in .vb file and edit connectionstring & insert string.
Good luck..

MySQL Connection code needs fixing. I hit a dead end

I'm currently working on a custom admin panel for a DayZ server, which will run on a local computer & connect to a remote database. I've Google'd more than ever, but I've hit a dead end. I can't seem to find a fix for this code which I've found & been told that it should work.
Public Function sqlConnect(ByVal server, port, instance, uname, pword) As Boolean
Dim connection As SqlConnection
connection = New SqlConnection()
connection.ConnectionString = "Server=" & server & port & "; Uid=" & uname & "; Pwd=" & pword & "; Database=***;"
Try
connection.Open()
MessageBox.Show("Connection Opened Successfully")
Return True
connection.Close()
Catch mysql_error As SqlException
MessageBox.Show("Error Connecting to Database: " & mysql_error.Message)
Return False
Finally
connection.Dispose()
End Try
End Function
If anyone here could help me out in finding the solution as to how I can get onto that MySQL server, you'ld be forever in my debt.
Notes: I'm using VS 2012 express, programming this exclusively in VB.NET, because it's quite a big project (for me) and it's the language I know best. Any add-ons required can be downloaded, as long as other users won't have to download it aswell.
I've never used a MySQL database before, so I might just be doing something completely wrong.
EDIT: The information that is given beforehand contains:
server ip, port, database ID, username & password.
You can't connect to MySQL using SqlConnection class. It is designed to connect to MS SQL Server. Try use this
Found it, this code works perfectly:
Public Function sqlConnect(ByVal server, port, instance, uname, pword) As Boolean
Dim connection As MySqlConnection
connection = New MySqlConnection()
connection.ConnectionString = "Host=" & server & ";port=" & port & _
";user=" & uname & ";password=" & pword & ";"
Try
connection.Open()
MessageBox.Show("Connection Opened Successfully")
Return True
connection.Close()
Catch mysql_error As MySqlException
MessageBox.Show("Error Connecting to Database: " & mysql_error.Message)
Return False
Finally
connection.Dispose()
End Try
End Function

How to find out what database is linked to a certain ID in VB.NET

Basically, I have a VB.NET project with which I connect to a remote MySQL server.
The credentials that I can provide would be "server ip", "port", "database ID", "username" and "password".
Pretty basic stuff, except for the fact that it's the DB ID that's given and not the actual DB name. How can I retrieve the name of the database which is linked to that ID?
Currently I use this connection string to connect to it
connection.ConnectionString = "Host=" & server & ";port=" & port & ";user=" & uname & ";password=" & pword & ";"
Note: The reason I need to be able to have these in variables is because I plan to distribute this tool to some friends of mine, which have a different MySQL server to connect to, with different database names etc. And because not everyone needs to know what the database is called, due to safety reasons.
Edit: Another thing I could do is have my program check every database on the server and return which database has a certain table in it, then use that output for the rest of my program.
I've found a workaround, although this might not be applicable to other MySQL databases.
Here's how I did it:
Public Function sqlConnectTest(ByVal server, port, instance, uname, pword) As Boolean
Dim connection As MySqlConnection
connection = New MySqlConnection()
connection.ConnectionString = "Host=" & server & ";port=" & port & _
";user=" & uname & ";password=" & pword & ";"
Try
connection.Open()
MessageBox.Show("Connection Opened Successfully")
**Dim stm As String = "SELECT TABLE_SCHEMA from information_schema.TABLES where TABLE_NAME like 'survivor'"**
Dim cmd As MySqlCommand = New MySqlCommand(stm, connection)
Dim reader As MySqlDataReader = cmd.ExecuteReader()
While reader.Read()
DBname = reader.GetString(0)
End While
reader.Close()
connection.Close()
Return True
Catch mysql_error As MySqlException
MessageBox.Show("Error Connecting to Database: " & mysql_error.Message)
Return False
Finally
connection.Dispose()
End Try
End Function
Obviously this solution would only work when you have an information_schema database, containing a table which shows all the tables in all the databases on the server.

Why am I getting Data provider or other service returned an E_FAIL status? SQL Native Client

I'm switching our ASP code to use SQL Native Client so that we can connect to a mirrored database with a failover partner, since you can only supply the failover partner parameters in SQL Native Client. When I run a proc that returns an nvarchar(max) column with Driver={SQL Server} everything works fine. When I run procs that return small colums with Driver={SQL Server Native Client 10.0} that works fine. It's only when I try to run a proc that returns an nvarchar(max) column while using Driver={SQL Server Native Client 10.0}; that I get the error. The error happens as soon as we hit
rs.Open cmdTemplate
So I'm not even referring to the column. Setting the conn string like this:
if bUseSQLNative then
connString = "Driver={SQL Server Native Client 10.0}; Network=DBMSSOCN; server=" & rs("SERVER_NAME") & "," & rs("PORT_NUM") & ";database=" & rs("DATABASE_NAME")
connString = connString & ";uid=" & rs("USER_NAME") & ";pwd=" & UnProtectValueEx(ConnSaltForDBPwd(), rs("CONNECTION_NAME"), rs("PASSWORD"))
else
connString = "Driver={SQL Server}; Network=DBMSSOCN; server=" & rs("SERVER_NAME") & "," & rs("PORT_NUM") & ";database=" & rs("DATABASE_NAME")
connString = connString & ";uid=" & rs("USER_NAME") & ";password=" & UnProtectValueEx(ConnSaltForDBPwd(), rs("CONNECTION_NAME"), rs("PASSWORD"))
end if
connString = connString & ";"
And opening like this:
set rs = server.CreateObject("ADODB.RecordSet")
rs.CursorLocation = 3
rs.CursorType = 3
rs.CacheSize = 50
on error resume next
rs.Open cmdTemplate
The error is:
Microsoft Cursor Engine (0x800A0001)
Data provider or other service returned an E_FAIL status.
In my case, the data to be saved (string) was larger than the specified nvarchr().
Increasing the field size solved the problem
I found it. I had to use
connString = "Provider=SQLNCLI10; DataTypeCompatibility=80;...
The DataTypeCompatibility makes the nvarchar max etc map back to formats ado can handle. And for some reason that parameter doesn't take effect with Driver={SQL Server Native Client 10.0};, only with Provider=SQLNCLI10
Use the reference MSADO 6.1 Library and then construct a data environment to connect to the database and establish the recordset to be used like this:
mydata = is a data environment with the connection to the database
getItemRec = is the query or command inside the mydata
myRecSet = is a Recordset Variable.
Do the code like this:
myData.Commands("getItemRec").CommandText = "Select * from myTable"
myData.getItemRec
Set myRecSet = myData.rsgetItemRec
With myRecSet
If .RecordCount <> 0 Then .MoveNext
Do While .EOF = False
....
....
.movenext
Loop
end With
Hope this will Help.