[Help] Remote MySQL

Posts 115 of 28 · Page 1 of 2
[Help] Remote MySQL
I am looking to establish a connection from my upcoming program so it will connect to mySQL on my website under a database.

It's going to be under a Serial System so I want it to read off a certain table, and if that input serial they enter is found in the table then it will grant access.

People who help will be in the credits./yea
Well you need an SQL datatable for starters, obviously. Then you'll need the MySQL .dll for .NET (6.2.4 is the version I have).

Reference that .dll to your project and use Imports MySql.Data.MySqlClient

Next you need to define your connection to your SQL database:

[php]
Private SQLConnection As New MySqlConnection("server=xxx.xx.xx.x;" & "user id=yourID;" & "password=********;" & "database=Databasename")
[/php]
Obvious you're need to change all the items appearing after "=" signs to suit your needs.

Next you need to try and connect to the database, but make sure you catch any exceptions if the database fails to connect:

[php]
Private Sub TryConnect()
Try
SQLConnection.Open()
Catch ex As Exception
If MessageBox.Show("An error occurred when trying to connect to the database, would you like to try again?", "Database error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then
TryConnect()
Else
Application.Exit()
End If
End Try
End Sub
[/php]

Basically if it fails, a messagebox will appear queying if they want to try again (in the case that it was just bad luck that the server failed to connect rather than an actual problem with their internet/the database)

After that you just need to write SQL queries to your database.
Quote Originally Posted by Jason View Post
Well you need an SQL datatable for starters, obviously. Then you'll need the MySQL .dll for .NET (6.2.4 is the version I have).

Reference that .dll to your project and use Imports MySql.Data.MySqlClient

Next you need to define your connection to your SQL database:

[php]
Private SQLConnection As New MySqlConnection("server=xxx.xx.xx.x;" & "user id=yourID;" & "password=********;" & "database=Databasename")
[/php]
Obvious you're need to change all the items appearing after "=" signs to suit your needs.

Next you need to try and connect to the database, but make sure you catch any exceptions if the database fails to connect:

[php]
Private Sub TryConnect()
Try
SQLConnection.Open()
Catch ex As Exception
If MessageBox.Show("An error occurred when trying to connect to the database, would you like to try again?", "Database error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then
TryConnect()
Else
Application.Exit()
End If
End Try
End Sub
[/php]

Basically if it fails, a messagebox will appear queying if they want to try again (in the case that it was just bad luck that the server failed to connect rather than an actual problem with their internet/the database)

After that you just need to write SQL queries to your database.
Dude, why dont u fix the MSgbox thing? It is so long:

Code:
if msgboxresult.Yes then
[Your Code In Here]
end if
It is quite Simple.
Quote Originally Posted by o0OpurezO0o View Post


Dude, why dont u fix the MSgbox thing? It is so long:

Code:
if msgboxresult.Yes then
[Your Code In Here]
end if
It is quite Simple.


@Jason, gj. Who thaught you this?

And for reading of a table you can do the following:

[php]Private reader as MySQLDataReader

'Let's say TxtSerial is the Textbox the user entrys the serial


'We have a table with the following structure:
' Serial | Used
'Used will hold either 0 or 1

Private Sub Validation
Dim strSQL as string = "SELECT * FROM Serials WHERE Serial='& cserial &' AND Used='0'" 'Serials = tablename
dim cserial as string = txtSerial.text

Try
dim cmd as new mysqlcommand(strSQL, SQLConnection)
reader = cmd.ExecuteReader

if not reader.hasrows
msgbox("The serial is either in use or does not exist.", msgboxstyle.information)
reader.close
else
reader.close
Dim strUsed as string = "UPDATE Serials SET Used='1' WHERE Serial='& curserial &'"
Dim Update as new mysqlcommand(strUsed, SQLConnection)
Update.ExecuteNonQuery
Msgbox("You have successfully registered the program.")
end if

Catch ex as MysqlException
reader.close
msgbox(ex.message.tostring)
end try

End Sub[/php]



You will have to add all your serials to the database before, mhmkay?

I suggest you make yourself a little application adding 100+ random serials into your program.

[php]
'the connection must be open ofc.
Dim i as integer

Do until i = 100
Dim strSQL as string = "INSERT INTO Serials(Serial, Used) VALUES('" & randomserial & "','0')"
Dim cmd as new mysqlcommand(strSQL,Sqlconnection)
cmd.executenonquery
i += 1
Loop

[/php]

Just make a sub to randomly create strings.

I just woke up and wrote it out of my mind, so there might be some errors =D.
Quote Originally Posted by o0OpurezO0o View Post


Dude, why dont u fix the MSgbox thing? It is so long:

Code:
if msgboxresult.Yes then
[Your Code In Here]
end if
It is quite Simple.
The fuck you even talking about? You haven't even set the parameters for your messagebox

And yes, Kevin is the crazy sonofabitch who taught me
Is MySql.Data.Entity.dll correct? :|
I already had that installed /yea
But where is the main DLL for it?
What folder do I browse in? Im currently at C:\Program Files\MySql\
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.2.4\Assemblies\MySql.Data.dll

That's where it's located on my computer.
Quote Originally Posted by Jason View Post
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.2.4\Assemblies\MySql.Data.dll

That's where it's located on my computer.
Hiya my website has only gone up. I tried the connection again but once again, it failed.
Quote Originally Posted by CainFool View Post

Hiya my website has only gone up. I tried the connection again but once again, it failed.
Did you use the correct IP address, correct datatable and login shit?
Found it, mines was slightly different.
C:\Program Files\MySQL\MySQL Connector Net 6.2.4\Assemblies\v2.0\MySql.Data.dll


Private SQLConnection As New MySqlConnection("server=http://cainfoool.******;" & "user id=cainfool_mysql;" & "password=********;" & "database=cainfool_mysql")

What's wrong there? Says that error saying it cant connect.
Something's wrong with the server or the user and pass, most likely.

Make sure you spelled everything correct.
Is the Server URL correct though? http://cainfoool. co . cc (MPGH blocks co . cc together :/)

User, pass & database name are all correct. I just made them there now.


Edit: My website crashed when I made the MYSQL so I guess ill work on it more tomorrow. PEace
The server isn't the URL, it's the IP. Which is why i left it in the form xxx.xxx.xx.x in my example
Posts 115 of 28 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?