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
Related
I have a problem with my code, I want to connect to my mysql glpi database but I have a launch error.
[This is a log of mysql base of glpi]
'Charger Liste'
ConnectionString = "server=10.14.242.190;port=3307;userid=root;database=GLPI;"
Dim conn As New MySqlConnection
conn.ConnectionString = ConnectionString
conn.Open()
Dim Query As String
Query = "SELECT * FROM glpi_computers"
MessageBox.Show(Query)
End If
End Sub
This is error : MySql.Data.MySqlClient.MySqlException : 'Unable to connect to any of the specified MySQL hosts.'
Trying to connect to a remote database on my hostinger.de account using VB.net. Connecting to the database using PHP on my hostinger.de website works just fine. Trying to connect via VB.net from desktop throws an error message:
a network-related error occurred... Win32Exception: The network path was not found."
I double-checked the server path. It is exactly as mentioned in my php admin area. Connecting via MySQL workbench works fine.
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myConn As SqlConnection
Dim myCmd As SqlCommand
Dim myReader As SqlDataReader
Dim results As String
myConn = New SqlConnection("server=sql588.main-hosting.eu;uid=u942496093_User1;database=u942496093_Test1;password=abcDEF123!")
'myConn = New SqlConnection("server=145.14.151.51;uid=u942496093_User1;database=u942496093_Test1;password=abcDEF123!")
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT Vorname, Nachname FROM TestTabelle1"
myConn.Open()
myReader = myCmd.ExecuteReader
Do While myReader.Read()
results = results & myReader.GetString(0) & vbTab &
myReader.GetString(1) & vbLf
Loop
MsgBox(results)
myReader.Close()
myConn.Close()
End Sub
End Class
Dim conn As New MySql.Data.MySqlClient.MySqlConnection
Dim myConnectionString As String
myConnectionString = "server=xxx.yyy.zzz.aaaa:3306;" _
& "uid=aaa;" _
& "pwd=bbb;" _
& "database=ccc"
Try
conn.ConnectionString = myConnectionString
conn.Open()
Catch ex As MySql.Data.MySqlClient.MySqlException
MessageBox.Show(ex.Message)
End Try
conn.Close()
Server was Ubuntu 18.04
Here is error screenshot.
Error screenshot
Anyone have solved this issue before?
Thanks for your support.
I'm a fan of constructing connections strings - whether using the dedicated builder or not - from defined connection parameters:
Dim (or Const) Server As String = "MyServer"
Dim (or Const) Port As UInteger= 3456
Dim (or Const) Database As String = "MyDatabase"
Dim (or Const) UserId As String = "MyUser"
Dim (or Const) Password As String = "MyPassword"
Dim mySql As New MySql.Data.MySqlClient.MySqlConnectionStringBuilder() With {
.Server = Server,
.Port = Port,
.Database = Database,
.UserId = UserId,
.Password = Password
}
Result from MySQL String Builder (mySql.ConnectionString):
server=MyServer;port=3456;database=MyDatabase;user id=MyUser;password=MyPassword
Or roll your own:
Dim myConn As String = $"Server={Server};Port={Port};Database={Database};Uid={UserId};Pwd={Password}"
(Note alternate parameter naming for UserId and Password.)
A useful resource is the Connection Strings website: https://connectionstrings.com.
I was wrong in my code.
Should specify the port as a separate parameter as Andrew Morton answered.
Here is new connection string which connect with the remote sql server.
myConnectionString = "server=xxx.yyy.zzz.ddd;" _
& "port=aaaa;" _
& "uid=bbbb;" _
& "pwd=xxxxxx;" _
& "database=eeeee"
I have an application written in VB.NET.
It was written for a client some time ago on 32 bit architecture.
The database is MySQL 5.5.
The reports were inbuilt Crystal reports and the database connectivity was ODBC 32bit MySQL connector through a DSN.
Now the client has upgraded all the clients to windows 10(64bit).
The desktop application works well with the same old 32bit connector, but when any report is run, it asks for a logon screen. Unfortunately, passing the login credentials at run-time doesn't work.
The code is as attached. I have browsed through all the answers, but the codes don't work. Is there something I am missing? Can anyone help?
I know the issue is an old one, but it is a running application for some years and we don't really need to rewrite.
Code: (Called on a button link)
Public Sub PrintAmcRemainderForIndus(ByVal componentId As Integer, ByVal AddressId As Integer)
'Dim cryRpt As New ReportDocument
Dim rPath As String = ""
Dim objForm As New frmViewReport
rPath = Application.StartupPath & "\Reports\rptAMCReminderForIndus.rpt"'
MessageBox.Show(rPath.Length & vbCrLf & rPath)
Dim crtableLogoninfos As New TableLogOnInfos()
Dim crtableLogoninfo As New TableLogOnInfo()
Dim crConnectionInfo As New ConnectionInfo()
Dim CrTables As Tables
Dim CrTable As Table
Dim TableCounter
Dim cryRpt As New rptAMCReminderForIndus()
'If you are using ODBC, this should be the DSN name NOT the physical server name. If
'you are NOT using ODBC, this should be the
'physical server name
With crConnectionInfo
.ServerName = "Indus"
'If you are connecting to Oracle there is no
'DatabaseName. Use an empty string.
'For example, .DatabaseName = ""
.DatabaseName = "indus"
.UserID = "root"
.Password = "simsoft"
End With
'This code works for both user tables and stored
'procedures. Set the CrTables to the Tables collection
'of the report
CrTables = cryRpt.Database.Tables
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next
Try
'cryRpt.Load(rPath)
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterValues.Clear()
crParameterDiscreteValue.Value = componentId
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("piCustomerId")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
crParameterDiscreteValue.Value = AddressId
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("piAddressId")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
objForm.rptViewer.ReportSource = cryRpt
objForm.Show()
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.InnerException.Message)
End Try
End Sub
I would say that there is something wrong here:
'If you are using ODBC, this should be the DSN name NOT the physical server name. If
'you are NOT using ODBC, this should be the
'physical server name
With crConnectionInfo
.ServerName = "Indus"
Are you sure you are providing the name of the ODBC, not the hostname?
Have a look here:
http://www.tek-tips.com/faqs.cfm?fid=4870
and here:
How to set database login infos (connection info) for crystal report vb.net?
I would like to make a connection between my program in VB.NET and my sql database. The code is like this:
Public Function connecter()
Dim Connexion As String = "Server=197.28.178.33;Database=test;Uid=userid;Pwd=xxxxxxxxxpassxxxx;"
Dim conn As MySqlConnection = New MySqlConnection
conn.ConnectionString = Connexion
conn.Open()
Return conn
End Function
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lecteur As MySqlDataReader
Dim Requete As String
connecter.Close()
connecter()
Requete = "select * from article"
Dim Commande As New MySqlCommand(Requete, connecter)
lecteur = Commande.ExecuteReader
Do While lecteur.Read
ComboBox1.Items.Add(lecteur.GetString("description"))
Loop
connecter.Close()
End Sub
but this error appears:
An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException'
occurred in MySql.Data.dll
Additional information: Unable to connect to any of the specified
MySQL hosts.
what is the cause of this error?
I don't understand what is the problem and how I can verify if I communicate with the server? and How I'm sure that the server is running