Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am trying to connect my visual basic project to mysql database using xampp. I downloaded an sql vbconnector from this website https://dev.mysql.com/downloads/connector/net/
but when i try to debug my project every mysql command reads an error and if i go and re-add the mysql reference all the errors disapper but then if i debug again the same debugging problem occurs.

What I have tried:

Imports MySql.Data.MySqlClient
Imports System.IO
Module databaseconn
    Public conn As MySqlConnection
    Public dr As MySqlDataReader
    Public flag As Boolean
    Public dt As DataTable
    Public da As MySqlDataAdapter
    Public i As Integer
    Public ds As DataSet
    Public cmd As MySqlCommand
    Public result As Boolean

    Public Function dbconn() As Boolean
        Try
            If conn.State = ConnectionState.Closed Then
                conn.ConnectionString = "server=localhost;username=root;password=;database=database"
                result = True
            End If
        Catch ex As Exception
            result = False
            MsgBox(ex.Message)

        End Try
        Return result
    End Function
End Module
Posted

1 solution

That code doesn't do anything with the DB, so it's difficult to work out what your problem might be. All that code does is set a connections string (which I'll come back to later) and nothing else: the connection is not usable - and is being handled badly anyway.

When you ask a question, you need to include relevant information: "when i try to debug my project every mysql command reads an error" tells nobody anything because we can't see code with does anything with a Command object, or tell what the error messages might be.
Error messages contain a wealth of information if you read them carefully - from which line of which file showed the problem, to a description of the problem that was found. So start here: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it talks about syntax errors, but run time errors tend to have the same information presented a little differently.

There is also a good article here: Asking questions is a skill[^] which explains how to ask a question so you get a helpful response.

Back to the code you do show, and there are three problems I can see there:
1) Your password for a multiuser DB should never be blank, nor should the default admin name be used - doing so lets anyone who wants to do anything to your DB: steal it, scramble it for a ransom, or destroy it for example. Set a good, strong password on a renamed admin account and you'll be safer.
2) Don't hardcode connection strings into your code: it means that when you release it, you have to change it for the production server (or worse test against the production server which is suicidal) and effectively release untested code! Screwups like that can badly impact clients and negatively impact your reputation and bank balance ... Use a config file to hold the connection string so code doesn't have to be changed when you release it.
3) Don't reuse a single Connection to the DB: Create it when you need one inside a Using block and the system will Close and Dispose of it when you are done with it. Not only are such Connections scarce resources, it can cause you real problems if your open a DataReader on a connection and then try to reuse the same connection to INSERT or UPDATE a table: the server will throw an error becuas ethe reader is still using the connection.
 
Share this answer
 
v2
Comments
Andre Oosthuizen 8-Feb-24 12:22pm    
"Amen"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900