Você está na página 1de 32

6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.

NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 1/32
Search... Go
Login | Register
Visual C++ / C++
Sample Chapter
Security
C++
Algorithms & Formulas
General
Checksum Algorithms
Combinations
Compression/Decompression
Developer's Guide to the Euro
Factorials
Hash Tables
Linked Lists
Mathematics
Searching
Sorting
String Algorithms
Portability Issues
C++ & MFC
General
Array Handling
Binary Trees
Bits and Bytes
Buffer & Memory Manipulation
Callbacks
Classes and Class Use
Collections
Compression
Drag and Drop
Events
Exceptions
External Links
File I/O
Function Calling
Linked Lists
Memory Tracking
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 2/32
Object Oriented Programming (OOP)
Open FAQ
Parsing
Patterns
Pointers
Portability
RTTI
Serialization
Singletons
Standard Template Library (STL)
Templates
Tutorials
Date & Time
General
Date Controls
Time Routines
C++/CLI
.NET Framework Classes
General
ASP/ASP.NET
Boxing and UnBoxing
Components
Garbage Collection and Finalizers
Interop
Moving from Unmanaged
Processes & Threads
Templates
Visual Studio .NET 2003
String Programming
General
CString Alternatives
CString Extensions
CString Manipulation
Open FAQ
Regular Expressions
String Arrays
String Conversions
.NET
COM-based Technologies
ATL & WTL Programming
General
ATL
Active Scripting
ActiveX Controls
Database
Debugging
External links
Graphics Support
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 3/32
Misc.
Performance
Printing
Tutorials
Utilities
Windows Template Library (WTL)
ActiveX Programming
General
Active Scripting
ActiveX Controls
ActiveX Documents
Apartments & Threading
Error Handling
External links
General COM/DCOM
Misc.
Registry
Security
Structured Storage
Tutorials
Wrappers
COM+
General
Admin
COM Interop
Managed Code / .NET
SOAP and Web Services
Shell Programming
General
Open FAQ
Shortcuts
Tray Icons
Previous Section Manager
Controls
Property Sheet
Open FAQ
Property Sheet Buttons
Sizing
Wizards
Button Control
Advanced Buttons
Bitmap Buttons
Flat Buttons
Menus
Non-Rectangular buttons
Windows XP
ComboBox
Colour Pickers
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 4/32
DropDown
Font selection combos
Multicolumn combos
Special Effects
Tooltips
Edit Control
Background & Color
Editors
Keyboard
Masked Edit Controls
Passwords and Security
Spin Controls
Transparent
ImageList Control
Open FAQ
ListBox Control
Checkboxes
Color Listboxes
Drag & Drop
LEDs
ListView Control
Advanced
Background color and image
Checkboxes
Columns
Custom Drawing
Data
Deleting
Drag & Drop
Editing items and subitem
FilterBar
Grid lines
Header Control
Introduction
Miscellaneous
Navigation
New ListView control (IE 4.0)
Printing
Property Lists
Reports
Scrollbars
Selection
Sorting
Tooltip & Titletip
Using images
Views
Menu
Alternative menu
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 5/32
Bitmapped menus
Dockable menus
Message and Command Routing
Miscellaneous
XML
XP-Style Menus
Other Controls
Bitmap Buttons
Charting and analogue controls
Check Box Controls
Clocks & Timers
Cool Controls
Date Selection Controls etc.
Digital Controls
Extending/Subclassing Techniques
File and Directory choosers
Grid Control
Group Box
HyperLink controls
Internet & Web Related
Lists, Trees and Combos
Minimize Button
Progress Controls
Resizing
Scroll Bars
Slider Controls
Spin Control
System Tray
Tab Controls
Tooltip controls
Charting and analogue controls
Extending/Subclassing Techniques
Rich Edit Control
Conversions
Editors and Editing
Syntax Hilighting
Windowless
Static Control
Bevel lines
Data display controls
Scrolling Text
Status Bar
Advanced
System Tray
Toolbar
Customizing Toolbars
Docking
Flat Toolbar
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 6/32
Miscellaneous
Placing Controls in Toolbars
Placing Controls in Toolbars
Treeview Control
Classes
Directory Browsers
Drag & Drop
Editing labels
Expand - Collapse
Misc - Advanced
Multiview
New Listview control (IE 4.0)
Searching
Tree traversal
Using images
Data
Database
ADO
ADO.NET
ATL
DAO
Dynamic Data Access
Microsoft Access
Microsoft Excel
Misc.
Object Oriented
ODBC
OLE DB
Oracle
SQL Server
Stored Procedures
XML
Miscellaneous
File Information
INI Files
Values
XML
Frameworks
UI & Printing Frameworks
Component Libraries
Outlook Controls
Reporting and Report Writing
Skins
Reporting and Report Writing
Windowing Techniques and Classes
Graphics & Multimedia
Bitmaps & Palettes
Capturing
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 7/32
Compressing
Displaying and Sizing
External Links
Icons
Image Manipulation
Merging
Other formats...
Palettes and Color Tables
Sample
Special Effects
Using regions
Viewers
Views & MDI clients
DirectX
DirectDraw
DirectInput
DirectShow
DirectX 8
GDI
Capturing Images
Fills
Font Handling and Detection
GDI+
Icons and Cursors
Lines
Multimedia
Audio
Desktop Effects
Graphics
Imaging
Timers
Twain
Video
OpenGL
Game Programming
Printing
Texture Mapping
Internet & Networking
Internet Protocols
ActiveX
Browser Control
Chat Programs
DHTML
Dial-up
DNS
Email
File Transfer
FTP
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 8/32
General Internet
HTML
HTTP
Instant Messaging
Internet Protocol (IP)
Network
Secure Socket Layer (SSL)
Security
Streaming Media
Web Services
XML
IE Programming
Displaying Information
Security
Voice
ISAPI
Cookies
Data/Databases
Extensions
Filters
Related CODEGURU sections
Network Protocols
Active Directory
Basic Network Operations
Dial-up Networking
Games
IPX
Messaging
Named Pipes
Network Information
Remote Administration
Remote Invocation
Serial Communications
TCP/IP
Winsock Solutions
Miscellaneous
Miscellaneous
Application Control
Assembly Language
CD-ROM
Compiler and Pre-Compiler
Console Apps
CryptoAPI
Drag and Drop
File and Directory Naming
File I/O
Flicker Free Drawing
Fonts
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 9/32
Graphics
ImageList
Intellisense
Interfacing to other languages
Internet Explorer
IPC Techniques
Keyboard
Log and Trace Files
MAPI
Math
Memory
Message Box Handling
MFC Help
Microsoft Office / Outlook
Microsoft Shell (MSH)
Mouse and Cursor Handling
MSN
Multi-Lingual Support
Plug-ins / Add-ins
Power Management API
Screen Savers
Splash Screens
Stack
System
System Tray
Task Scheduler
Templatized Classes
Threads/Processes
Timers
Titlebar
Tools
UML
Version Info
Video
Visual InterDev
Windows Messaging
Samples
Basic Programming
Code Highlighting
Games
System Information
Testing & Debugging
Visual Studio
Debugging
Debuggers
Handling Crashes
Logging
Memory Issues
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 10/32
Reverse Engineering
Tracing
Add-ins & Macros
Add-Ins
Bookmarks
Browser Windows
Code Generation
Code Navigation
Code Reformatting
Code Template Add-in
Comment Creators
Custom AppWizards
Debugging
File Creation
File Opening
Text Operations
Version Control
Visual Studio .NET
Window Management
Editor Tips
Customizing the IDE
Debugging
Syntax Highlighting
Windows Programming
Vista
CE
Bitmaps and the GDI
COM
Controls
Database
Embedded Visual C++
Files
Game Programming
Memory Management
Networking
Pocket PC
Registry
Samples
Shell and Related Programming
SmartPhone
Clipboard
External Links
Previous Section Manager
DLL
Hooking
Import/Export issues
Interfacing to Visual Basic applications
Mixed DLLs
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 11/32
Tips
Interfacing to Visual Basic applications
File & Folder
Browser Functions & Dialogs
Controls & Dialogs
File Information
File I/O
Folder/Directory Maintenance
INI files
Installers
Shell API Functions
Help Systems
HTML
MSDN
Printing
Open FAQ
Print Preview
Win32
Cursors
MessageBox
Security
Tutorials
Versioning
System
CD-ROM
Control Panel
Device Driver Development
Hardware Information
Keyboard
Logging
Message Handling
Misc
MMC Snapins
NT Services
Performance Statistics
Processes / Modules
Registry
Resource Detection/Management
Security
Services
Shared Memory
System Information
Taskbar
Task Manager
Threading
Timers
VxD
Windows & Dialogs
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 12/32
Console
Redirection
Dialog
Animation
Background
Bitmaps/Images
Browse Namespace
Colors
CommandUI
Common Dialogs
Credits Dialog
DDX/DDV
Dialog-based applications
Dialog Bars
Dialog for selecting folders
Dialog Layout Manager
Dialog Template Resources
Displaying in a window
Display Performance
Font Handling
Help
Image Preview
Menu Handling
Miscellaneous
Modeless Dialogs
Non-Rectangular Dialogs
Options Dialogs
Progress Indicators
Resizable Dialogs
Scrolling
Splash Screens
Splitter Windows withing Dialogs
Threads
Tip of the Day
Title Bar
Toolbars and Statusbars
Tooltips for Dialog Controls
Tutorials
Win32
Wizards
Splitter Windows withing Dialogs
Docking Window
Open FAQ
Doc/View
Background
CHtmlView
Command Routing
Control Views
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 13/32
DDX/DDV
File Open/Save
Help
Message Maps
Misc
MRU (Most Recent Used file list)
Printing
Scrolling
SDI
Splitter Views
Tabs
Title Bar
View Management
Window Positions/Sizes
Splitter
Open FAQ
Sizing
Toolbars
Tutorials
Standard Template Library
.NET / C#
Sample Chapter
.NET
Silverlight
WCF
WPF
Windows Workflow
Data & Databases
DataGrid
Sorting and Iterating
Web Grid
Debugging
Debugging
Logging
State Management
Techniques
Tracing
Framework
Licensing
Microsoft Namespace
System Namespace
General
Arithmetic
Assemblies
Code-Behind
COM/COM+
Common Type System (CTS)
Debugging and Error Handling
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 14/32
Events and Delegates
Graphics
IL
Internet
Keyboard
Macros
.NET Framework Classes
.NET My Services
Patterns
Performance
Rotor
System Information
Threads
Tips & Tricks
Tools and 3rd Party
Visual Studio .NET Add-Ins
Debugging and Error Handling
JScript .NET
Essam Ahmed on JScript .NET
Managed C++
Windows Services
Net Security
Authentication
Encryption
PGP
VS Add-Ins
UML and Design
Visual Studio Add-ins
Visual Studio .NET 2003 Add-ins
General ASP.NET
Controls
Email
Files
Miscellaneous
Scripting
Themes
Tutorials
Web Forms
Azure
C#
.NET 3.0 / .NET 3.5
LINQ
Basic Syntax
Anand C# Tutorials
Attributes
Component Development
Controls
Enumerations
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 15/32
Error Handling
Indexers
Inheritance
Interfaces
Operators
Reflection
Remoting
Serialization
Threading
Unmanaged Code
Unsafe
Collections
Controls
Custom
Data Grid
Property Grid
Rich Text
Tree View
Tutorials
Wizards
Data & I/O
Caching and Peformance
Searching
Streaming
Tutorials
XML
Date & Time
Formatting
Time Routines
Delegates
Event Handling
Graphics & Multimedia
Charts and Graphing
Customizing User Interfaces
Drawing
Mouse
Screen Captures
Sound
Threading
Wav Files
Customizing User Interfaces
Internet
Desktop Applications
Mail
Miscellaneous
COM
Design/Techniques
DLLs and Executables
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 16/32
E-Mail
Graphics and Images
Icons
Mathematics
Reflection
Sample Programs
Security
User Interface
Network & Systems
Configuration Files / INIs
Database
Directory Services
HTTP
Internet/Web
IP
Remoting
Sockets
Windows Services
WMI
Web Services
Security
Tutorials
Windows 8/RT
XAML
Visual Basic
Sample Chapter
.NET 3.0 (VB)
LINQ (VB)
Internet
ASP .NET
Database
HTML
SMTP / eMail
Web Services
Indexing
VB Controls
VB Other Controls
OCX Controls
VB ListView
VB ListBox
VB ComboBox
VB ActiveX
ActiveX
.NET Controls
DataGrid Control
Listview
VB Files
Directory
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 17/32
Drive
VB Shell
General
VB Multimedia
MP3s
VB Graphics
Animation
File Formats
GDI
Transparency
Database
ADO
ADO.NET
Database Access
DataSets
Microsoft Access
SQL Server
Stored Procedures
Strongly Typed Data
DataGrid
Forms & Controls
Backgrounds
Icons
ListBox
ListView Controls
Resizing
IDE & Language
Attributes
Error Handling
IDE (including Visual Studio .NET)
IDE & Language
Indexers
Miscellaneous
IDE (including Visual Studio .NET)
Misc
Algorithms
Console Applications
Date
Debugging and Tracing
Encryption
Games and Fun
Icons
Multi-Language Support
Numbers
OOP
PDF
Plugins
Printing
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 18/32
Samples
Text
Tips
User Defined Types (UDTs)
System
Directories and Files
Keyboard
Network
Services
Win32
Mobile/Wireless
Mobile Internet Toolkit
Pocket PC
Windows 8/RT
Others
Windows Mobile Dev Center
Windows App Gallery
CodeGuru Blogs
Videos
Whitepapers
Forums
Videos
Submit an Article
Newsletter
Azure Activities NEW!
codeguru
.NET / C#
.NET
Data & Databases
Read More in Data & Databases
Not having data governance can hurt your business. Download this eBook to learn how to take control now.
Post a comment
Email Article
Print Article
Share Articles
Digg
del.icio.us
Newsvine
Facebook
Google
LinkedIn
MySpace
Reddit
Slashdot
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 19/32
The New Business of Technology: Extend, Transact and Optimize
Download Now
StumbleUpon
Technorati
Twitter
Windows Live
YahooBuzz
FriendFeed
A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
Posted by Quin Street on January 10th, 2008
0.25
0.50
0.75
1.00
1.25
1.50
1.75
2.00
2.25
2.50
2.75
3.00
3.25
3.50
3.75
4.00
4.25
4.50
4.75
5.00
Vote!
Tweet
By Ziran Sun
The first part of this article illustrated how to install and configure the MySQL Database Server, install and use the
MySQL Administrator, create a new database, and create and populate a new sample table with some sample data.
Now that the database server is up and running, this part of the article will complete our coverage of using MySQL with
0 Like
Share
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 20/32
ASP.NET by covering how to:
Add a new MySQL user.
Assign the new user the appropriate permisions to the database.
Connect to the MySQL server from .NET.
Build a simple ASP.NET page to query the database.
Step 7 - Creating a New MySQL User Account
I'm assuming that you're been following along with part one, so at this point you should have the MySQL Server and
MySQL Adminstrator installed, and have a sample database named "mydatabase" which contains a sample table named
"mytable" with two rows of data in it.
In order to add a new user account, we'll once again need to run MySQL Administrator and login to your server using
the password you set during installation. You'll then want to select the "User Administration" item from the list of items at
the left of the MySQL Administrator window. This should bring up a list of the current user account on the server
(There should already be once called "root"). If you right-click in the small window where the users are listed you should
get the option to "Add new User".
You'll then be prompted to enter the new user's details. I've named the user "15secs" and assigned a password of
"password".
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 21/32
Once you've finished entering the user's details press "Apply Changes" to save your modifications.
Step 8 - Granting the User Account Access to the Database
By default new users have permission to do almost nothing. In order to allow our new user to connect to the MySQL
database server, we need to grant him what MySQL calls "Schema Privileges". This is naturally done from the "Schema
Privileges" tab in MySQL Administrator.
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 22/32
Notice in the above screen capture that the user has no "Assigned Privileges" to the "mydatabase" database. Since the
user will need to be able to query the database to run the sample code which follows, I'm now going to assign the user
the "SELECT" privilege by highlighting it in the "Available Privileges" column and clicking the arrow to move it to the
"Assigned Privileges" column. Once again I'll click the "Apply Changes" button to save changes.
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 23/32
Depending on your application the user may very well need more permission then I've assigned to "15secs". Most non-
trivial applications will probably need at least "INSERT", "UPDATE", and "DELETE" permissions in addition to
"SELECT", but it's always best to err on the side of caution and add more permissions as they are needed then to
simply give everyone full control.
Step 9 - Connecting to the Database Server from an ASP.NET Page
To my knowledge there are currently two main ways to connect to a MySQL database server from .NET: MySQL
Connector/ODBC (aka. MyODBC) and MySQL Connector/Net. While the ODBC connector is cross-platform and is
compliant with ODBC standards, the .NET version is generally the better choice when using MySQL with .NET.
The setup files can be downloaded from the MySQL Connector/Net page and installation is straight-forward.
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 24/32
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 25/32
Note: Even though I selected the option to register Connector/NET in the Global Assembly Cache and
when I checked it later I found that it actually was installed there, until I copied the MySql.Data.dll file to
my application's /bin folder I couldn't get the import statement to find the Connector/NET's namespace.
The exact error message was:
BC30466: Namespace or type specified in the Imports 'MySql.Data.MySqlClient'
cannot be found.
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 26/32
I'm not sure what the problem was/is, but I looked around and it seems some others are having the same
issue. So, for the time being, placing another copy of the file from it's installed location (ie. C:\Program
Files\MySQL\MySQL Connector Net 1.0.4\bin\.NET 1.1\) to your application's /bin folder (ie.
C:\Inetpub\wwwroot\bin\) should resolve the issue.
Step 10 - A Sample ASP.NET Page to Query a MySQL Database
So with our MySQL database finally squared away and MySQL Connector/Net installed, we can now start using
MySQL from our ASP.NET web pages. For illustration I'm going to provide a very simple script with no bells and
whistles. You can find fancy database scripts all over the web. The point of this one is simply to show you what you
need to do to connect to MySQL. Don't get me wrong... all that fancy stuff works just fine using MySQL (and usually
with very few changes) but that's just not the point of this script.
MySQL.aspx
<%@ Page Language="VB" debug="true" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "MySql.Data.MySqlClient" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim myConnection As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet
Dim strSQL As String
Dim iRecordCount As Integer
myConnection = New MySqlConnection("server=localhost; user id=15secs;
password=password; database=mydatabase; pooling=false;")
strSQL = "SELECT * FROM mytable;"
myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
myDataSet = New Dataset()
myDataAdapter.Fill(myDataSet, "mytable")
MySQLDataGrid.DataSource = myDataSet
MySQLDataGrid.DataBind()
End Sub
</script>
<html>
<head>
<title>Simple MySQL Database Query</title>
</head>
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 27/32
<body>
<form runat="server">
<asp:DataGrid id="MySQLDataGrid" runat="server" />
</form>
</body>
</html>
In the above script, I've highlighted the places where the script varies from one you would use to perform the same thing
using Microsoft SQL Server or Access. As you can see there's really nothing new here. Instead of importing
System.Data.SQLClient like we would for MS SQL, we import MySql.Data.MySqlClient. Oh and if at any point you
need a reference for the MySql.Data.MySqlClient namespace, you're in luck... it ships with one.
Here's a screen capture of what the script above produces when run against the sample database and table we set up in
this article. Like I said, it may not be pretty, but there's no reason it couldn't be. I'm just keeping it simple for illustration.
Conclusion
In part one of this article I illustrated how to:
Download and install the MySQL Database Server.
Configure the server.
Install MySQL Administrator to make managing the database easier.
Create a new database named "mydatabase".
Create a new table named "mytable" in that database.
Add a couple rows of sample data to that table.
This part illustrated how to do the following:
Add a new MySQL user.
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 28/32
Assign the new user the appropriate permisions to the database.
Connect to the MySQL server from .NET.
Build a simple ASP.NET page to query the database.
I hope this article helped ease your introduction to this great little database server. Whether you are upgrading from
Access or starting from scratch, MySQL is certainly a viable option and despite the fact that it's open-source and not
shipped from Redmond, it really does work great with .NET. And, with the addition of MySQL Administrator,
management is no longer the source of nightmares that it used to be. Hopefully the next time you're looking for a
database server to use as the backend for your .NET application, you'll at least consider using MySQL.
Comments
how to connect mysql 5.0 with asp.net
Posted by Ravi on 03/26/2014 09:35am
how to connect mysql 5.0 with asp.net
Reply
Helpful
Posted by luke on 01/06/2013 03:04pm
This really helps clear things up, thank you!
Reply
Great start
Posted by Luiz Rebelo on 01/02/2013 03:46am
This help me a lot, its a great start.. From here it's delete and update :P. Thanks
Reply
missing pics
Posted by apoorv on 11/28/2012 11:31am
hey some of the pictures are missing. can you please replace them for a better read.
Reply
Thanks..
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 29/32
Posted by baris on 08/01/2012 09:18am
hey..this article is really helpful..thanks buddy...
Reply
Leave a Comment
Your email address will not be published. All fields are required.
Name
Email
Title
Comment
Post Comment
Top White Papers and Webcasts
Privacy & Terms
Type the text
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 30/32
The New Business of Technology:
Extend, Transact and Optimize
The design principles of
SOA make it possible f or
you to develop new
engagements in a way that
optimizes value f or your
organization. IBM provides
a strong application f oundation and business
agility technologies that help you to become a truly
engaging enterprise.
Create Flexible Innovative Apps
for an Amazing Windows
Experience
On-demand Event Event
Date: June 20, 2014 People
are evolving their choice
f or devices, and
developers have to evolve their applications as
well. Building apps continues to get more complex
with the number of dif f erent f orm f actors available
on the market. New f orm f actors include all-in-one
PCs , 2-in-1 PCs, and more modern tablets. For the
best end-user experience, the apps you build
need to be aware of not only the platf orm, but also
the devices and their capabilities. Check out this
upcoming eSeminar and
Most Popular Programming Stories
Today
This Week
All-Time
There have been no articles posted today.
1 Services Made Easy
2 Telerik DevTools Responsive to .NET Developers Mobile Needs
3 Working with Configuration Files and Visual Basic
4 Working with State Machines in the .NET Framework
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 31/32
1 Working with Ajax Helper in ASP.NET MVC
2 Returning Images from ASP.NET Web API
3 Using Custom Action Names in ASP.NET Web API
4 Using SSL in ASP.NET Web API
5 Two Ways of Passing HTML5 Web Storage Data to ASP.NET
More for Developers
Site Map
CodeGuru
HTMLGoodies
Windows Mobile
Discussions
VBforums
DevX
DatabaseJournal
Latest Developer Headlines
IBM, Toyota Partner on Vehicle Application Development Platform
Granular or Cell Level Encryption in SQL Server
Use MvcContrib Grid to Display a Grid of Data in ASP.NET MVC
IBM, Toyota Partner on Vehicle Application Development Platform
Granular or Cell Level Encryption in SQL Server
RSS Feeds
All
C#/.NET
Win Mobile
C++
VB
6/25/2014 A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
http://www.codeguru.com/csharp/.net/net_data/article.php/c19493/A-StepbyStep-Guide-To-Using-MySQL-with-ASPNET--Part-2.htm 32/32
CodeGuru
About
Contact
FAQs
List of Gurus
Sitemap
Topics
Visual C++ / C++
.NET / C#
Visual Basic
Article Submission
Video Submission
Legal
Terms of Service
Licensing and Permissions
Privacy Policy
Advertise
Copyright 2014 QuinStreet Inc. All Rights Reserved.

Você também pode gostar