connecting my vb.net app to MySQL database - mysql

guys i'm trying to develop an app (using vb.net & sql database) ,managing my database using PhpMyAdmin ,so the problem is , when executing my app , it shows nothing :/ , i don't know if the problem is a bad password , but even so , it should be showing me a error message (i used catch nd try ...)
> the code !
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim Connexion As New SqlConnection("Data Source=localhost;Initial Catalog=stockage;User Id=pma;Password=pmapass;")
Try
Connexion.Open()
Dim Requete As String = "UPDATE client SET nom_client ='client unknown' WHERE nom_client is null"
Dim Commande As New SqlCommand(Requete, Connexion)
Try
Console.WriteLine("there was " & Commande.ExecuteNonQuery() & " lignes changed")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Commande.Dispose()
Connexion.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
End Sub
End Module
(when executing the console it shows nothing )
thanks !

First of all you're not managing a SQL Database with phpMyAdmin. phpMyAdmin manages mySQL databases instead.
Secondly you need to install Connector/Net through this link
Note : Please go for version 6.6.7 as they have removed Visual
Studio integration from 6.7.7 and above
Once the connector is installed, start a new project in VB.NET (Im using 2008) and you will then need to add a reference to the connector.
Choose "Add Reference" from the Project menu, then select "Browse" and browse to the installation folder where the connector was installed,
choose "MySQL.Data.dll" .
Imports MySql.Data.MySqlClient 'Very Important
Module Module1
Sub Main()
Dim Connexion As New MySqlConnection
Dim db_name As String = "stockage"
Dim db_host As String = "localhost"
Dim db_username As String = "pma"
Dim db_password As String = "pmapass"
If Not Connexion Is Nothing Then Connexion.Close() 'to close already open connections
Connexion.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", db_host, db_username, db_password, db_name)
Try
Connexion.Open()
Console.WriteLine("Connected to server " + db_host)
Dim Commande As MySqlCommand
Dim Requete As String = "UPDATE client SET nom_client ='client unknown' WHERE nom_client is null"
Commande = New MySqlCommand(Requete, Connexion)
Console.WriteLine("there was " & Commande.ExecuteNonQuery() & " lines changed")
Connexion.Close()
Console.WriteLine("Disconnected from server " + db_host)
Catch ex As MySqlException
MessageBox.Show("Cannot connect to database: " & ex.Message)
Finally
Connexion.Dispose()
End Try
End Sub
End Module

Related

What is the ConnectionString to connect MySQL db with VB.NET

I am working on to connect my App form VB.Net with MySQL table database with this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str As String = "data source=localhost;Initial Catalog=tabledb;user id=root;password=password1234"
Dim con As New SqlConnection(str)
con.Open()
Dim com As String = "Select * from springdata where rfid_tag='" & Rfid_tagTextBox.Text & "'"
Dim cm As New SqlCommand(com, con)
Dim rd As SqlDataReader = cm.ExecuteReader()
If rd.Read() = True Then
MessageBox.Show("Valid username and password")
Else
MessageBox.Show("Invalid username and password", caption:="login")
End If
End Sub
But when I run the app gave me this error:
Additional information: 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)
What is the correct ConnectionString to use with MySQL 5.7.
The connection string below is an SQL Server connection string:
Dim str As String = "data source=localhost;Initial Catalog=tabledb;user id=root;password=password1234"
According to MySQL Connector .NET connection string, you should provide server name, port number (if using different port), database name and credentials like this:
Dim str As String = "Server=localhost;Port=3306;Database=tabledb;Uid=root;Pwd=password1234"
Also you should use MySqlConnection, MySqlCommand & MySqlDataReader instances with parameters instead of value concatenation inside query string (ensure that reference to MySql.Data.dll added first):
Using con As New MySqlConnection(str)
con.Open()
Dim com As String = "Select * from springdata where rfid_tag=#tag"
Using cm As New MySqlCommand(com, con)
cm.Parameters.Add("#tag", MySqlDbType.VarChar).Value = Rfid_tagTextBox.Text
Dim rd As MySqlDataReader = cm.ExecuteReader()
' Check if any rows exist
If rd.HasRows = True Then
' do something
Else
' do something else
End If
End Using
End Using

Error connecting to MySQL database (No such host is known)

While using the following code to connect to my SQL database:
MySqlConnection.ConnectionString = "server=vipervenom.net.mysql; userid=vipervenom_net_userinfo; password=PASSGOESHERE; database=vipervenom_net_userinfo;"
it prompts an error saying
Unable to connect to any of the specified MySQL hosts. ---> No such host is known.
I've been looking for hours to find an answer to this, but even though my host and login information is correct, it gives me that error. Take a look at the picture for login information -->
The entire source code:
MySqlConnection = New MySqlConnection
MySqlConnection.ConnectionString = "server=vipervenom.net.mysql; userid=vipervenom_net_userinfo; password=PASS GOES HERE; database=vipervenom_net_userinfo;"
Try
MySqlConnection.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim MyAdapter As New MySqlDataAdapter
Dim sqlquery = "SELECT * From Users WHERE email='" & TextBox1.Text & "' AND password='" & TextBox2.Text & "';"
Dim command As New MySqlCommand
command.Connection = MySqlConnection
command.CommandText = sqlquery
MyAdapter.SelectCommand = command
Dim Mydata As MySqlDataReader
Mydata = command.ExecuteReader
If Mydata.HasRows = 0 Then
MsgBox("Wrong login information!")
Else
MsgBox("Login Successful! :D")
End If
Thank you in advance!
For what it's worth, vipervenom.net.mysql is not, and cannot be, a valid hostname, your hosting provider's statements to the contrary notwithstanding.
There aren't any hostnames ending in .mysql because that is not a valid top level domain name.
It's possible your hosting provider has an internal domain naming service handling .mysql as a TLD. But you won't be able to use that hostname from outside their network.
Ask your hosting provider what hostname to use for your MySQL server from outside their network.

Use VB.net and MySQL exported sql file in other computer

I'm done coding in vb.net with mysql as my database. And also I'm done exporting mysql database into .sql file and done building my code into an application, but there's a problem, when im trying to run my application in other computer, my application didnt read the exported mysql database file. can anyone help me how to fix this? i've attached some few lines of my code if someone ask my code.
Dim query As String
Dim count As Integer
sqlcon = New MySqlConnection
sqlcon.ConnectionString =
"server=localhost;userid=root;password=root;database=svfmemberlistdb"
Try
sqlcon.Open()
query = " SELECT * FROM svfmemberlistdb.account where username='" & txtUser.Text & "' and password='" & txtPass.Text & "' "
sqlcmd = New MySqlCommand(query, sqlcon)
sqlrdr = sqlcmd.ExecuteReader
count = 0
While sqlrdr.Read
count = count + 1
End While
If count = 1 Then
Me.DialogResult = DialogResult.OK
MessageBox.Show("Login Successful")
Me.Hide()
ElseIf count > 1 Then
MessageBox.Show("Username or Password are Duplicated")
Else
MessageBox.Show("Login Failed, Invalid Username or Password")
End If
sqlcon.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
End Try
this is the error message appear when i try to run my build application with it's database in other computer.

Error connecting to SQL with VB

So i tried to connect my sql database to my vb project but it gives me this error:
Error Connecting to Database : Authentication to host "for user" using
method 'mysql_native_password' failed with message : Acces denied for
user "#'N56VB' (using password : NO)
N56VB is my pc
UPDATE the error wich I get now is : A first chance exception of type
'System.ArgumentException' occurred in System.Data.dll
Additional information: Keyword not supported.
If there is a handler for this exception, the program may be safely
continued
I dont know what to do anymore it will be a stupid I guess ,be easy on me i'm a noob :(
I hope someone knows the problem
Imports MySql.Data.MySqlClient
Imports System.Data.Sql
Imports System
Imports System.Data
Public Class NieuweItems
Public dbconn As New MySqlConnection("Data Source=localhost;user id=root;password=;database=gip;")
Dim conn As New MySqlConnection
Private Sub btn_return_Click(sender As Object, e As EventArgs) Handles btn_return.Click
Me.Close()
SalesApplication.Show()
End Sub
Private Sub btn_inlezen_Click(sender As Object, e As EventArgs) Handles btn_inlezen.Click
Dim sqlCommand As New MySqlCommand
Dim SQLConnection As MySqlConnection = New MySqlConnection
Dim strStockSQL As String
SQLConnection.ConnectionString = "Data Source= localhost ;user id = root'#'localhost; password = ;database=gip;table=stock;"
Try
conn.Open()
strStockSQL = "INSERT INTO Barcode " & txtBarcode.Text & "INSERT INTO Naam_Product" & txtNaam.Text & "INSERT INTO Verkoopprijs" & txtVP.Text
sqlCommand.ExecuteNonQuery()
SQLConnection.Close()
conn.Close()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
Finally
conn.Close()
End Try
End Sub
End Class
I presume that the error is occurring inside the "btn_inlezen_Click" event handler? I think what's happening is that you're declaring a new empty MySqlConnection, opening the connection inside your Try block and only then are you setting the ConnectionString. Because MySqlConnection was declared with no parameters it might be trying to use N56VB as the user account and this doesn't have any permissions on your MySQL db.
So set your connection string before opening the connection.

Visual Studio 2010. Saving Multiline textbox in my sql

Can anyone give me an idea on how to save the datas that are typed into a textbox (multiline) in vb.net 2010 to my database (mysql)?
Use a String-representation of your Object and store this String to your database. JSON might be useful for you here, JSON.net is commonly used...
Here is a code to insert a record in a mysql database:
Let's say you have a mysql database called (myDB) and a table called (myTable).
There are two fields in myTable: ID(int) and TextBoxData(Text)
Imports MySql.Data.MySqlClient
Imports System.Data.OleDb
Public Class Form1
Dim conn As New MySqlConnection
Sub connect()
Dim DatabaseName As String = "myDB"
Dim server As String = "ip address here"
Dim userName As String = "username here"
Dim password As String = "password here"
If Not conn Is Nothing Then conn.Close()
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
Try
conn.Open()
MsgBox("Connected")
Catch ex As Exception
MsgBox(ex.Message)
End Try
conn.Close()
End Sub
End Class
' The following code is used to write to MySQL database:
Try
conn.Open()
Catch ex As Exception
End Try
Dim cmd As New MySqlCommand(String.Format("INSERT INTO `myTable` (`ID` , `TextBoxData`) VALUES ('{0}' , '{1}')", "", TextBox1.Text))
cmd.ExecuteNonQuery()
conn.Close()
Source: VB.NET connect to MySql Server