How To Insert Data in Hosting Database Using VB.NET - mysql

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..

Related

Connecting to MySQL server from excel with VBA code

I'm running a MySQL server on my old computer and trying to connect it from my current computer. I would like to access to the server from Excel but I keep getting the error:
'[Microsoft] [ODBC Driver Manager] Data source name not found and no default driver specified'
I've downloaded a 'MySQL connector ODBC 8.0.20 win 32' which should match my computer requirements. At this point I'm quite clueless why I keep getting the error. Please find my VBA code below.
Private Sub Data_to_SQL_Click()
Dim con As ADODB.Connection
Set con = New ADODB.Connection
Dim strConn As String
strConn = "Driver={MySQL ODCB 8.0 Unicode Driver};SERVER=(my old computer ip address);DATABASE=Database;USER=root;PASSWORD=(password)"
con.Open strConn
Dim rng As Range
Set rng = Application.Range("A2:E14")
Dim row As Range
For Each row In rng.Rowss
Dim Sql As String
Sql = "insert into kavijamaarat (pvm, ravintola, ruoka, hinta, kpl) values ('" & row.Cells(1).Value & "', '" & row.Cells(2).Value & "', '" & row.Cells(3).Value & "', '" & row.Cells(4).Value & "', '" & row.Cells(5).Value & "')"
con.Execute Sql
Next row
con.Close
MsgBox "Done"
End Sub
Rewrite
strConn = "Driver={MySQL ODCB 8.0 Unicode Driver};SERVER=(my old computer ip address);DATABASE=Database;USER=root;PASSWORD=(password)"
to
strConn = "Driver={MySQL ODBC 8.0 Unicode Driver};SERVER=(my old computer ip address);DATABASE=Database;USER=root;PASSWORD=(password)"

Error handling in Excel VBA connecting to remote MySQL

I'm creating a macro to connect an excel sheet to a SQL server.
The excel file will be used both when I'm home and when I'm outside.
Now everything is working adressing to my DNS www.server1.com.
Now, if for some reason the server 1 is not working, I want to adress to the second backup server www.server2.com
The problem is that I cannot handle the error.
after the "Cn.Open ...." if it cannot communicate with the server, the macro is stopped and I receive the excel error:
"Run-rime error: '-2147467259 (80004005)"
I want that instead of return this error, I can manually handle the error with something like that:
If Cn.Error Then
Server_Name = "www.server2.com"
etc ...
How can I do that?
Server_Name = "www.server1.com" ' Enter your server name here
Port = "3306"
Database_Name = "mydb" ' Enter your database name here
User_ID = "myuserid" ' enter your user ID here
Password = "mypassword" ' Enter your password here
Dim Cn As ADODB.Connection
Set Cn = New ADODB.Connection
Cn.Open "Driver={MySQL ODBC 5.3 ANSI Driver};Server=" & Server_Name & ";PORT=" & Port & ";Database=" & Database_Name & ";Uid=" & User_ID & " ;Pwd=" & Password & ";"
You need to add a On Error ... and test on the next line if you are connected; if you aren't, then connect to your backup server. Finally, deactivate the error handling.
Dim Cn As ADODB.Connection
Set Cn = New ADODB.Connection
On Error Resume Next
Cn.Open "Driver={MySQL ODBC 5.3 ANSI Driver};Server=" & Server_Name & ";PORT=" & Port & ";Database=" & Database_Name & ";Uid=" & User_ID & " ;Pwd=" & Password & ";"
if Cn.State = 0 Then : _
Cn.Open ... 'Backup connection
On Error Goto 0

Excel Vba Problems deleting rows from mysql database

Hi I'm trying to delete all the rows in mysql database, using vba in excel.
I have tried a lot of codes now and the closest I get, is the following code but I still get an syntax error in :
rsmysql.Open rsstr, coninc
Can someone help me correct the code, so I can get it to work please.
Dim rsmysql As ADODB.Recordset 'Holds records
Dim rsstr As String
Dim coninc As String
Set cnmysql = New ADODB.Connection
Set rsmysql = New ADODB.Recordset
coninc = "Driver={MySQL ODBC 5.3 ANSI Driver};Server=" & _
Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rsstr = "delete * from TLHMember_List;"
cnmysql.Open coninc
rsmysql.Open rsstr, coninc
MsgBox "Data has been deleted"
Exit Sub
rsstr = "delete from TLHMember_List"
cnmysql.Open coninc
cnmysql.Execute rsstr
MsgBox "Data has been deleted"

How do I setup an ADODB connection to SQL Server 2008 in Microsoft Access 2010?

I just installed SQL Server 2008 on my laptop. I also have Microsoft Access 2010 installed. Using VBA, I am trying to create an ADODB connection to my own database on SQL Server but I'm having trouble finding the right line of code:
When I use this below, it doesn't work.
The name of my computer is LAPTOPX and the database is HomeSQL.
I am sure it's super easy but since I'm just starting out I can't seem to find the right way to ask the question.
Thanks!
Dim DBCONT As Object
Set DBCONT = CreateObject("ADODB.Connection")
Dim strDbPath As String
strDbPath = "LAPTOPX/HomeSQL"
Dim sConn As String
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source =" & strDbPath & ";" & _
"Jet OLEDB:Engine Type=5;" & _
"Persist Security Info=False;"
DBCONT.Open sConn
First, you need to make sure SQL Native Client is instaled. Reference
SQL Server 2008
Standard security
Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername;
Pwd=myPassword;
Trusted connection
Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;
Trusted_Connection=yes;
Connecting to an SQL Server instance
The syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.
Provider=SQLNCLI10;Server=myServerName\theInstanceName;Database=myDataBase;
Trusted_Connection=yes;
Source
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim sConnString As String
Dim recordsAffected as Long
'Create connection string
sConnString = "Provider=sqloledb; Server=LAPTOPX; Database=HomeSQL; Trusted_Connection=True;"
'Open connection and execute
conn.Open sConnString
'Do your query
With cmd
.ActiveConnection = conn
.CommandType = adCmdText
.CommandText = "Select ...;"
.Execute recordsAffected 'Includes a return parameter to capture the number of records affected
End With
Debug.Print recordsAffected 'Check whether any records were inserted
'Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set cmd = Nothing
Set conn = Nothing
This connetion string works under Excel VBA. In MsAccess also should.
dbName = "test" 'your database name
dbFilePath = "C:\db.mdf" 'your path to db file
connStr = "Driver={SQL Server native Client 11.0};" & _
"Server=(LocalDB)\v11.0;" & _
"AttachDBFileName=" & dbFilePath & ";" & _
"Database=" & dbName & ";" & _
"Trusted_Connection=Yes"
Full solution: http://straightitsolutions.blogspot.com/2014/12/how-to-connect-to-sql-server-local.html

VBA Connect to MYSQL

I am trying to setup a remote database connection to a server running MySQL version 5.0.96. When I execute I get the following error.
Any assistance is appreciated.
Here is my connection code:
Public Function opendb()
Dim oConn As ADODB.Connection
Dim Server_Name As String
Dim User_Name As String
Dim Password As String
Dim Database_Name As String
Server_Name = Sheets("_config").Range("B2").value
User_Name = Sheets("_config").Range("B3").value
Password = Sheets("_config").Range("B4").value
Database_Name = Sheets("_config").Range("B5").value
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.0.96 Driver};" & _
"SERVER=Server_Name;" & _
"DATABASE=Database_Name;" & _
"USER=User_Name;" & _
"PASSWORD=Password;" & _
"Option=3"
End Function
Dim oConn As ADODB.Connection
Dim Server_Name As String
Dim User_Name As String
Dim Password As String
Dim Database_Name As String
Server_Name = Sheets("_config").Range("B2").value
User_Name = Sheets("_config").Range("B3").value
Password = Sheets("_config").Range("B4").value
Database_Name = Sheets("_config").Range("B5").value
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.0.96 Driver};" & _
"SERVER=" & Server_Name & ";" & _
"DATABASE=" & Database_Name & ";" & _
"USER=" & User_Name & ";" & _
"PASSWORD=" & Password & ";" & _
"Option=3"
There might be other problems with your drivers but when I tested the code you posted on my end I got the same error - it's not going to read your variables unless you concat them
Edit - I'm using a 5.2a server - when I try to connect using 5.0.96 I get the same error again , but of course using the 5.2a driver in the connection string works fine. So you will probably need to download the 5.0.96 driver specifically. I did a little searching and couldnt find it, I'll keep looking around for a minute
MySQL server is running 5.0.96. DO NOT INSTALL MySQL Connector/ODBC 5.2.6. This driver is NOT backwards compatible from VBA. If you have it installed and you are having the same problem I did do the follow:
1) FIRST remove any User & System Data Sources you have added
2) Uninstall all ODBC 5.2 drivers
3) Installed Connector/ODBC 3.51.30. (You May Need to Reboot)
4) Make your Connection String > DRIVER={MySQL ODBC 3.51 Driver};
Hope this saves you lots of time.
You may write Set oConn = CreateObject("ADODB.Connection") 'NEW STATEMENT
instead of Set oConn = New ADODB.Connection