How to access a MySQL database with .NET

An often overlooked combination of scripting technologies is ASP.NET with MySQL (instead of Access [slow] or SQL Server [expensive]). MySQL is a fast and popular database and you can find numerous ISPs which offer it with ASP.NET

If you are installing it on your own server, you need to download MySQL and install it, then run c:\mysql\bin\winmysqladmin.exe. You also need to download the ODBC driver (choose Windows Downloads Driver Installer) and install it. Then use this code to access your MySQL database.

<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<%@ Page Language="C#" %>
<script runat="server">
public void Page_Load(Object sender, EventArgs e) {
DataTable dtRecords = GetDataTable("SELECT * FROM newone");
foreach(DataRow dr in dtRecords.Rows) {
Response.Write(dr["FirstName"].ToString() + " " + dr["LastName"].ToString() + "

private static string GetConnection() {
return “DRIVER={MySQL ODBC 3.51 Driver};Server=localhost;Database=testdatabase”;
public static DataTable GetDataTable(string sql) {
DataTable rt = new DataTable();
DataSet ds = new DataSet();
OdbcDataAdapter da = new OdbcDataAdapter();
OdbcConnection con = new OdbcConnection(GetConnection());
OdbcCommand cmd = new OdbcCommand(sql, con);
da.SelectCommand = cmd;
try {
rt = ds.Tables[0];
catch {
rt = null;
return rt;



56 responses to this post.

  1. Very nice Article and the way of presentation is so simple, i really liked this.

  2. Original post here

  3. Thank you a lot for giving everyone an extremely superb opportunity to check tips from this website. It can be so amazing and also jam-packed with a great time for me personally and my office colleagues to visit your blog no less than three times a week to find out the fresh guidance you will have. And of course, I’m so at all times fascinated for the stunning tips and hints you give. Selected 1 ideas in this post are absolutely the most suitable I’ve ever had.

  4. learnt some interesting points from this post glashutte original watches thanks a lot

  5. I’m extremely inspired along with your writing talents as neatly as with the layout in your blog. Is that this a paid topic or did you modify it your self? Anyway stay up the nice high quality writing, it is rare to look a nice weblog like this one nowadays..

  6. You have mentioned very interesting points! ps decent internet site.

  7. Very interesting info!Perfect just what I was looking for!

  8. Very interesting information!Perfect just what I was looking for!

  9. Web hosting getting cheaper with offering good functionalities.I have tried some best web hosting providers and found them great

  10. This webpage won’t display correctly on my iphone4 – you may want to try and fix that

  11. Web hosting getting cheaper with offering good functionalities.I have tried some Awesome web hosting providers and found them great

  12. Facebook is way to very easy to hack & when you complain they usually do not get your complaint seriously.way too risky.

  13. Thanks for that information you share here. It is good to know these things.
    I have logged into my Squido account a few times, only to discover writers block or simply inability to write. I think it’s time to give it another try.

  14. Although the convenience of email cannot be denied, the act of receiving and reading a snailmail letter is by far the better experience. So kudos to you!
    I understand that the letters are not personalized, but are they physically signed by the sender? That would be such a wonderful touch and a single for which I’d gladly pay more. Perhaps the sender could sign a small percentage, so that a randomly chosen few every single week would have a little extra thrill. That possibility would absolutely make my heart race a bit faster as I tore open the envelope!

  15. I’ll update this post and throw in pinterest for you! give me a second! Let the blogger know they can contact me for help if they need it on watermarking.

  16. This blog site is pretty good! How was it made ?

  17. How did you make your site look this sick! Email me if you can and share your wisdom. Id appreciate it.

  18. Hey there, I just hopped over to your web site through StumbleUpon. Not somthing I might typically read, but I liked your views none the less. Thank you for creating some thing worth reading through.

  19. Could you message me with some hints & tips on how you made your website look this cool , I would be thankful.

  20. When are you going to post again? You really inform a lot of people!

  21. An insightful blog post there mate . Thanks for posting !

  22. It seems to me that this site doesnt load up on a Motorola Droid. Are other folks getting the exact same problem? I like this web site and dont want to have to miss it when Im gone from my computer.

  23. Wow! This site is awesome! How do you make it look like this .

  24. This really answered my problem, thank you!

  25. Wow! This website is amazing! How did you make it look this good .

  26. Brilliant, cheers, I will bookmark you soon.

  27. When I open up your Feed it appears to be to be a ton of nonsense, is the problem on my side?

  28. When I start your Rss feed it seems to be a ton of garbage, is the problem on my side?

  29. Just to let you know your web site appears a little bit different in Safari on my notebook with Linux .

  30. Just to let you know your web-site looks a little bit strange on Safari on my pc using Linux .

  31. Just to let you know your web page looks a little bit strange in Safari on my computer using Linux .

  32. Heya, I just hopped over to your web site thru StumbleUpon. Not somthing I would normally browse, but I enjoyed your thoughts none the less. Thanks for creating something worth reading through.

  33. Between me and my husband we’ve owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I’ve settled down to one line of players. London Olympics Medal Tally

  34. Title…

    magnificent post, very informative. I wonder why the other specialists of this sector don’t notice this. You should continue your writing. I’m confident, you have a great readers’ base already!…

  35. Hi, i feel that i noticed you visited my blog thus i got here to return the desire?.I’m attempting to to find things to improve my site!I guess its ok to make use of some of your ideas!!

  36. resurser…

    […]6 We still cannot quite believe that It was not respectable often be those ch ar[…]…

  37. How to access a MySQL database with .NET…

    Someone necessarily help to make severely articles I’d state. That is the first time I frequented your website page and up to now? I amazed with the research you made to make this particular post amazing. Fantastic job!…

  38. stefan blog…

    […]h It’s difficult to acquire knowledgeable men and women on this topic, howe ie[…]…

  39. This blog was… how do I say it? Relevant!! Finally I have found something that helped me. Many thanks!

  40. resources…

    […]a I don’t waste my free time in watching video lessons except I go for to rea ek[…]…

  41. It’s super weblog, I need to be like you

  42. You have brought up a very good details , thankyou for the post.

  43. blog link…

    […]d Hey there would you mind stating which blog platform you’re using? I’m plan uq[…]…

  44. What a awesome article this is. Look forward to looking this again again.

  45. Title…

    I have been surfing online more than three hours lately, but I never discovered any fascinating article like yours. It’s beautiful price enough for me. In my opinion, if all website owners and bloggers made just right content as you did, the internet w…

  46. My spouse and I stumbled more than here distinct website and believed I need to check items out.

  47. You should take part in a contest for one of the greatest blogs on the internet. I will recommend this blog!

  48. I like this web blog so much, saved to bookmarks .

  49. whoah this blog is great i love reading your posts. Keep up the good work! You know, a lot of people are hunting around for this info, you could help them greatly.

  50. You could certainly see your skills in the paintings you write. The sector hopes for even more passionate writers such as you who are not afraid to say how they believe. At all times follow your heart.

  51. There is certainly a great deal to find out about this topic. I love all of the points you made.

  52. page…

    […]b This is my first time I have visited your site. I found a lot of interestin 1w[…]…

  53. I have been absent for some time, but now I remember why I used to love this website. Thank you, I will try and check back more frequently. How frequently you update your web site?

  54. Hands down, Apple’s app store wins by a mile. It’s a huge selection of all sorts of apps vs a rather sad selection of a handful for Zune. Microsoft has plans, especially in the realm of games, but I’m not sure I’d want to bet on the future if this aspect is important to you. The iPod is a much better choice in that case.

  55. The new Zune browser is surprisingly good, but not as good as the iPod’s. It works well, but isn’t as fast as Safari, and has a clunkier interface. If you occasionally plan on using the web browser that’s not an issue, but if you’re planning to browse the web alot from your PMP then the iPod’s larger screen and better browser may be important.

  56. Thanks for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more clear from this post. I’m very glad to see such great information being shared freely out there.

Comments are closed.

%d bloggers like this: