|
-
Jul 30th, 2009, 11:42 AM
#15
Fanatic Member
.NET and MySQL Quick Guide
OK, so here is what I found about using MySQL to get this done.
Things you will need to get or find out:
MySQL dll's - http://dev.mysql.com/downloads/connector/net/ - Windows Binaries (ZIP)
These are the references you need to be able to access a MySQL database.
Once you have this installed, and start your project, you will need to make sure you add a Reference to MySql.Data
(All assemblies are here: "C:\Program Files (x86)\MySQL\MySQL Connector Net <version>\Assemblies" if installed to the default folder.)
*** HOW-TO ADD A REFERENCE ***
****************************
In the Solution Explorer, there is a button along the top of the pane labeled "Show All Files".
Click it and more items will show up in the Solution Explorer tree; one is labeled "References".
Right-click it and choose "Add Reference...". It will take a little bit for the screen to pop up. When it does, click the "Browse" tab then go to the folder listed above and select just the MySQL.Data.dll file to add.
****************************
****************************
Variables: (stuff you will need to plug into the appropriate places in the code below)
<server address> - Address where the MySQL server is located
<db_name> - The name of the database that will need to be accessed
<username> - Username for access to the database
<password> - Password for access to the database
<table> - Table where data resides
CODE:
The following code will simply allow you to make sure you have a connection.
Make a new Form, put two labels and a button on it. Sort of like you see in the first image I've attached.
Open the Code window, delete everything, and past the following code:
Code:
Imports MySql.Data.MySqlClient
Public Class Form1
Try
Dim conn As New MySql.Data.MySqlClient.MySqlConnection
conn.ConnectionString = "Server=<server address>;Database=<db name>;Uid=<username>;Pwd=<password>;"
conn.Open()
Dim command As New MySqlCommand("SELECT COUNT(*) AS <field> FROM <table>", conn)
Dim adapter As MySqlDataAdapter = New MySqlDataAdapter
Dim table As DataTable = New DataTable
adapter.SelectCommand = command
adapter.Fill(table)
Dim row As DataRow = table.Rows(0)
Console.WriteLine(row("UserCount").ToString)
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Class
There is a Try...Catch block to catch any errors you have that may occur so you can get a general idea what is wrong if something gives an error.
NOTE: You may need to allow access to your MySQL databases remotely. On my Webhost, access was restricted to 192.168.1.%, which meant that only my webserver to connect directly to it. I had to add a reference to %.%.%.% to allow any incoming IP address direct access to the database.
Of course you will have to modify the SQL statements for your own needs, but that is just an example bit of code.
Last edited by Seraph; Oct 9th, 2012 at 09:25 AM.
Reason: Modified some verbiage and checked to see if information was still valid.
Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7
SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
[Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]
[.NET and MySQL Quick Guide]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|