Posts in category: 'MySQL'

Sun/MySQL and .NET

Woke up this morning to the Sun/MySQL news; my initial thought was "uh oh". My big concern is whether Sun will continue to support languages-that-are-not-Java as well as MySQL has been doing.

But my concerns were at least partially laid to rest when MySQL’s CEO Marten Mickos explicitly called out their support for lots of different languages (including C#) and said they’ll continue to do so in the future. That’s good to hear, although as soon as the ink dries on the acquisition, it won’t be solely up to Marten to determine how much support Sun provides for a competing technology stack that Scott McNealy once referred to as "a joke". In fairness, that 2003 quote did more to reveal McNealy’s own ignorance regarding his competitors’ products than anything else, and of course he’s no longer calling the shots at Sun on a day-to-day basis. But still.

This is a concern for everyone because MySQL comprises the foundations of the technology stacks of so many companies, including Approver.com (which uses .NET and MySQL). It’s also a key component in the stacks of many of Platform Associates clients including Alfresco and others. On his blog, Alfresco’s CTO John Newton is raving about the opportunities the deal will provide. If he’s happy, then I’m happy.

Database Queries with Windows Powershell

I haven’t done a serious learning project for a while so this week I decided to play around with Windows Server 2008 and PowerShell. I’ll post some thoughts about Server 2008 soon (at first glance it looks terrific once you get past the fact that IIS is now a "role" instead of something you install). But I wanted to post a PowerShell example first because I wanted to do something with it that I couldn’t find information about anywhere — specifically, perform a database query and dump the contents to the console. (Later we’ll get fancy and dump it to HTML or send it somewhere via email, etc.)

PowerShell can invoke any .NET object, so I knew it was possible to do database stuff with a PowerShell script, but I couldn’t find any examples anywhere, so here’s what I cooked up via trial and error.

The first step is to create a database connection:


[system.reflection.assembly]::LoadWithPartialName("MySql.Data")
$cn = New-Object -TypeName MySql.Data.MySqlClient.MySqlConnection

The syntax to load a .NET assembly in PowerShell is pretty awkward; couldn’t they have provided a Get-Assembly cmdlet? (I suspect that thousands of PowerShell users have already written that one already.) Also, you have to know the assembly name of your ADO.NET provider (which, for MySql, is "MySql.Data," not "MySql.Data.MySqlClient," which was my first guess). The thing to remember is that loading an assembly is syntactically different than including a namespace even though the objective is sort of the same.

Fortunately this is the only part of the process that I didn’t get right the first time, and I had it resolved after a few minutes of experimentation.

I should mention that I’m using the MySql data provider because we use MySql on Approver.com, but you can obviously use any ADO.NET provider you want here. This assumes that your ADO.NET provider is installed in the GAC; if you need to make an explicit reference to an assembly in the file system the syntax is different.

Next, open the database connection:

$cn.ConnectionString = "SERVER=localhost;DATABASE=mydb;UID=xxx;PWD=yyy"
$cn.Open()

If you want, you can check the value of $cn.State here to make sure it worked. Then execute the query:

$cm = New-Object -TypeName MySql.Data.MySqlClient.MySqlCommand
$sql = "SELECT ID, FirstName, LastName FROM Person LIMIT 50"
$cm.Connection = $cn
$cm.CommandText = $sql
$dr = $cm.ExecuteReader()

ADO.NET programmers know that there are lots of ways to do this that require fewer lines of code, but I like to assign everything out explicitly using properties of the ADO.NET Connection and Command objects.

Finally we iterate over the results and close the connection:

while ($dr.Read())
{
    write-host $dr.GetInt32(0) " " $dr.GetString(1) " " $dr.GetString(2)
}
$cn.Close()

This is not too different than what a C# developer is accustomed to, particularly reading out the data. I like that you can just pass a big list of values to write-host without having to explicitly concatenate or convert to strings here. PowerShell figures out what you want to do and does the right thing.

On Approver.com we have a bunch of utilities that we use to monitor system health and site activity (like how many people registered that day, whether people are discovering and using certain features of the site, etc.). Every web site should crunch these numbers frequently (otherwise you have no idea whether the work you’re doing matters to your users).

I think I’m going to eventually migrate our various statistical and maintenance utilities to Powershell. The power of the .NET framework combined with the convenience of script is going to come in handy for a lot of stuff.

Cgen: Simplest .NET/MySQL Relational Wrapper That Could Possibly Work

Last week I attended and spoke at the MySQL conference. Some of the talks, particularly the ones that targeted system administrators rather than developers were over my head, but that’s useful information in itself — it serves as a reminder that sometime soon I’ll need to either learn the finer points of big-time MySQL system administrator or hire somebody who does.

It was particularly fun giving a talk on .NET to MySQL guys just two weeks after I finished giving a talk on MySQL to a room full of .NET guys. It was also fun seeing my client Alfresco and hanging out with them in their booth, where they were doing demos and fielding lots of inquiries.

I’m just now getting around to firing up the blog-o-matic in earnest after four weeks of more or less non-stop conference-going, but I wanted to mention that I’ve released the tool that we use to create the data access layer. It’s called Cgen, and it lives here.

The idea behind Cgen is that you create an XML document that represents your database schema, and Cgen (which is packaged as a command-line tool written in C#) spits out a bunch of classes that make it easy to do the basic create, read, update, and delete operations that represent 80% of all database-driven applications. (For the remaining 20% of the data access code you’d need to write, the tool generates subclasses that you can add your own code to.) You can then compile these classes as a .DLL or, if you’re making an ASP.NET 2.0 web application you can just dump the classes into the application’s App_Code folder and .NET will figure out how to compile the classes.

We use code generated by Cgen as our data access layer for Approver.com. It has worked splendidly and saves us a bunch of time. At the same time, Cgen doesn’t have a ton of bells and whistles. I realize that this is probably the one millionth object/relational code generator in the history of software development. I took a look at other object/relational mappers like SubSonic, and I used NHibernate on an intranet project I did at Yahoo in 2005. But for Approver I wanted a tool so simple that you didn’t spend more time learning the tool than you would have spent writing the code yourself. I also needed something with good support for .NET 2.0 and MySql (which was difficult to find in March 2006 when I started using MySQL).

At MySQL Conference This Week

I am spending today preparing for my session at the MySQL conference on Thursday. I’ll be speaking on "MySQL and .NET in the Real World" which will cover my experience creating Approver.com and will also include a run-through of the Visual Studio integration with MySQL.

Last year I created a little command-line tool that generates MySQL data access classes for .NET 2.0. It’s the tool that generates the data access layer for Approver.com. I am going to open-source this tool this week, most likely before my talk on Thursday, and I will do a demo of the tool as part of my talk at the conference. This tool is very simple — it doesn’t do as much as, say, SubSonic or NHibernate. But it does automatically generate about 80-90% of the data access code used in Approver.com, and it has a few ease-of-use characteristics that I wanted — mainly a low learning curve, explicit support for MySQL and .NET 2.0 and no dependencies on external libraries other than what you find in the framework.

I need to do an intro/documentation page that describes the tool. I’ll get to that in the next day or two, then I’ll post here when it’s available.

New Version of MySQL .NET Connector

A while back I posted that the 5.x line of MySQL connectors was a bit too garden-fresh for production use. Fortunately Reggie and team have quickly released a new version: here are the details.

From the release notes it looks like they might have resolved the problem that I was having. I’m still using the 1.0.9 connector for now but I’ll be testing the new 5.0.5 version as soon as I get some time in hopes that I can show off MySQL/Visual Studio integration at VSLive in a few weeks.

Update: Reggie says that 5.0.5 isn’t soup yet, either: details here.

MySQL .NET Connector Madness

As I’ve been writing about here for a while, my big learning project of 2006 was to pick up MySQL and to build a web site using ASP.NET with MySQL as the back-end. This went really well, better than I could have expected. For someone who has experience with SQL Server, the learning curve isn’t too bad, and MySQL clearly did a lot of things with developer productivity in mind.

One of the things that I try to do when I write articles or do talks on technologies is to use the latest versions of everything, so when I started preparing my upcoming VSLive talk, I upgraded everything on my dev boxes, including the server, the ADO.NET providers, etc.

MySQL provides two sets of "connectors" for .NET developers. (.NET developers know these as "ADO.NET providers".) The connectors in the 1.0 line are more stable, but they don’t support the new features of ADO.NET that came out in .NET 2.0, which means that stuff like declarative data sources as well as the MySQL Visual Studio add-in won’t work — all this stuff expects to see the 5.x connector installed in the global assembly cache.

Unfortunately, the 5.x line of connectors isn’t ready for prime time yet — some folks (including me) have had problems consistently connecting to the server with the latest 5.0.x connector. So yesterday I backed out the upgrade and I’m using the 1.0.9 connector for now. I should mention that this version of the connector is also live in production on Approver.com right now and it’s 100% solid.

To their credit, Reggie and the MySQL team do a great job of providing information and feedback when issues like this come up, as they inevitably do — I feel like I can get way more information from MySQL than I ever could from Microsoft or Oracle in situations like this. So I’ll hang in there, and if they can get another rev of the 5.x connector I’ll give that a whirl before my VSLive talk.

Update: They released a new version of the 5.x connector that may fix this problem.

Outline of My MySQL Talk at VSLive

Here’s a list of slides I’m developing for the MySQL talk I’m giving at VSLive next month. This talk is intended for developers who are proficient with .NET and some kind of relational database but with with little or no exposure to MySQL. I am pretty sure that the talk is going to be 60 minutes in duration, so 30-40 slides should be about right.

Did I leave anything important out? Let me know in comments — thanks!

  1. What is MySQL?
  2. Meta: About Open Source Software
  3. Obtaining MySQL
  4. Installing MySQL
  5. Configuration
  6. Jeffrey’s my.ini File
  7. Gotcha: Security Settings
  8. Command-Line Tools
  9. MySQL GUI Tools
  10. Visual Studio Integration
  11. MySQL Administrator
  12. MySQL Query Browser
  13. Creating a schema
  14. Pluggable Storage Engines
  15. Data Types
  16. Enumerations
  17. Gotcha: Set DATETIME Defaults
  18. Unicode Support
  19. MySQL Query Manager
  20. MySQL ODBC Driver ("it exists")
  21. MySQL ADO.NET Provider
  22. Creating a DataReader
  23. DataReader with Parameters
  24. DataSet
  25. Subselects
  26. Use LIMIT N instead of TOP N
  27. Stored Procedures
  28. Transactions
  29. Triggers
  30. Clustering
  31. Replication
  32. Architecting for Scalability
  33. References

Installing the MySQL ADO.NET Connector into the Global Assembly Cache

This morning I sat through a MySQL webinar on Visual Studio tools for MySQL presented by the most excellent Reggie Burnett.

One of the questions that came up after the talk pertained to installation problems on Windows. (I should preface all this by saying that MySQL, the database server, has a good installer and is a snap to get running on Windows.)

I don’t usually use MySQL’s pre-compiled installers on Windows — now that I sorta know what I’m doing, I find it’s usually faster to set up MySQL and its attendant GUI tools without installers. And MySQL supports the notion of downloadable zips without binary installer cruft, which I really like, particularly in situations where you just need to yank one file out of the distribution (like documentation) without having to go through a whole big install.

But for those of us who eschew installers, there’s a trick with the MySQL tools for Visual Studio: the installer for the Visual Studio add-in apparently expects to find the MySQL ADO.NET provider (which MySQL refers to as "Connector/Net") in the Global Assembly Cache. If it doesn’t find the provider in the GAC, the add-in won’t run.

I was dimly aware of the existence of the GAC but I’d never needed to do anything with it myself in the real world (aside from mentioning it in my books). This isn’t a huge inconvenience, though. First you just need to locate a command-line utility called gacutil.exe on your machine (mine’s in the SDK directory under Visual Studio 8, but yours might be somewhere different depending on what tools you’re using).

Next you’ll need to locate MySql.Data.dll on your machine. This file can be located anywhere if you’re difficult like me and you don’t use an installer.

So, the entire command line I needed to use to install the MySQL provider in the GAC was:

"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" /i "C:\Program Files\MySQL\Connector.NET 5.0.3\Driver\bin\net-2.0\release\MySql.Data.dll"

(The /i switch means "install".)

This is certainly not brain surgery but hopefully putting all this in writing will save others some time.

A MySQL IPO in 2007?

Link: GigaOM » MySQL prepping for an IPO.

"Long a favorite of web developers, MySQL saw serious growth in 2006 and added 2,500 new customers, and also introduced MySQL Enterprise. There seems to be a renewed interest in open source IPOs….For starters, the corporations have taken a shine to the open source3 software.

Secondly, most companies with IPO dreams have been around for a while and have been able to build solid businesses around their offerings."

This is very good news for our friends from Uppsala and for open source businesses in general. For years the database market has been ripe for disruption and now it seems like MySQL is approaching a serious tipping point. I’ll be watching this closely.

Speaking at VSLive in March

I am extremely excited to be making my hopefully-triumphant return to the VSLive conference in San Francisco in March.

I started speaking at VSLive in 1997 (back when it was called VBITS). I spoke at most of the VSLive conferences in the U.S. in Europe through 2001 when I went to work for eBay. I even took time out from my honeymoon in 2000 to speak at the VBITS conferences in London and Stockholm (you can imagine how difficult it was to convince my fiancee to add two European capitals to our trip).

VSLive has been one of my favorite conferences to speak at for a couple of reasons. One is its practical focus — it’s about getting things done in the real world instead of talking in grand strategic terms about what technology means, etc. I also really like the mix of Microsoft insiders combined with the regional directors and other consultants — it seems to keep the agenda honest and grounded.

I’ve also made some great friends over the years at these conferences, particularly Andrew Brust (who I met at VBITS Berlin in 1997, the night before he was carted off to the hospital after suffering a fall onstage), Chris Kinsman, who I co-authored two books on ASP.NET with, and Deborah Kurata, who I had an extremely productive working relationship with for a few years in the late 90s.

I have been pitching talks on open-source tools to VSLive for a bunch of years. In 2001 I tried to get them to let me do a talk on NUnit. I was really passionate about unit testing around this time and I was crushed that they didn’t have room for my talk on the schedule that year. Unfortunately, a lot of Microsoft platform developers didn’t get unit testing religion until a unit-testing tool was included with Visual Studio four years later — and only then if you shelled out the big bucks for Visual Studio Team System. NUnit is still a great tool, by the way, and it’s 100% free — I still use it and highly recommend it.

Anyway, I’ve been spending a lot of my time in the last five months coding an ASP.NET application using MySQL, so I figured it might make sense to share what I’ve learned about the pros and cons of MySQL with VSLive attendees. So I’m delighted to announce that my March VSLive talk will be on Programming MySQL with .NET.

I am not sure if this is the first VSLive talk to cover an open-source product, but I am pretty sure that it will be the first MySQL talk at VSLive, which makes it infinitely more exciting for me. I am going to bust my croutons to make this talk as good as the ASP talk I gave around 1999 (in which I demonstrated a common security flaw in ASP and three or four people in the audience unexpectedly jumped up, opened their cell phones and ran out of the room in a panic — that was pretty fun).

If you’re using MySQL with .NET, or you’re just interested in learning more about it, I’d love to hear your feedback and suggestions in comments. My plan is to cover the basics of a data-driven app using ASP.NET and MySQL, hit some of the subtle syntactical differences between MySQL and SQL Server, and then cover some goodies like MySQL stored procedures. If there’s something else on that list you’d like to see, let me know.

MySQL Gotchas for .NET Developers

I think I’ve mentioned a few times on the blog that I’ve been using the combination of .NET 2.0 and MySQL for Approver.com. I made this choice after very careful consideration. I spent about a month looking at Postgresql and MySQL earlier this year and came to the conclusion that either one is a good choice for Windows developers today; MySQL edged out Postgresql mainly because of the strength of its developer ecosystem.

Yet when I tell people in Silicon Valley that I’m using C# and MySQL together, they look at me like I just stepped off a flying saucer. I am here to say that using MySQL is 110% righteous on Windows and you don’t have to sacrifice much of anything (including GUI tools, which were one of my big requirements) to use it.

I am going to do more to evangelize the .NET and MySQL combination because it’s worked so well for us to date. To start, I’m going to describe two gotchas I’ve run into while developing Approver.com. Neither of these are things that I’d consider to be major problems, although they did require a bit of research and testing to figure out.

1. Date values. MySQL has a concept of a date that equals zero, but something in the .NET stack (ADO.NET? System.DateTime?) does not. So, the workaround is to always set your DateTime fields to default to ‘0001-01-01 00:00:00′ instead of the normal MySQL default, which is ‘0000-00-00 00:00:00′.

2. Unsigned integers. This one bit me over the weekend when I was trying to create a table that stores IP addresses for a <mumble mumble> we’re planning to release soon. The 1.07 version of the ADO.NET provider for MySQL (which is, by the way, provided by and officially supported by MySQL AB under the name "MySQL Connector .NET") has a bug where it barfs on unsigned integers. There are two workarounds for this: treat the column as a signed integer (System.Int32), or upgrade to 1.08 of MySQL Connector, which handles unsigned integers correctly. (The signed integer workaround won’t work for the use case of storing an IP address, so I upgraded to 1.08.)

I also tried MySQL Connector 5.0 beta when investigating this problem and discarded it because it broke a lot of other stuff in my app — notably, the 5.0 beta forces you to check for null values for all nullable fields in a DataReader, which I think is a mistake, particularly since it’s different than the behavior in the 1.07 connector and from most other ADO.NET providers I’ve used. Is this nullability check some new .NET 2.0 thing that is there because there are nullable types in the framework now? Hmmph. Anyway.

The MySQL ADO.NET provider is open source, by the way (just like the rest of MySQL), so if you ever wanted to see what a soup-to-nuts ADO.NET provider looks like, you can download the source and pry at it to your heart’s content.

Update: In the comments,
Reggie points out that the SQL Server and Oracle ADO.NET providers
exhibit the null assignment behavior with DataReaders as well, so the MySQL folks changed their
provider’s behavior to mimic what the other guys do. Throwing an error when assigning a null value still seems
unnecessary to me since the DataReader should just know what to do when
assigning a null field to a String, but I’m sure someone will be along at some point to let me know why this behavior is sensible and correct, even though it’s really awkward for developers.

Also: I mentioned that the change from sensible handling of null types to strict handling didn’t start with the 5.0 provider; it was introduced in 1.08 (as I discovered yesterday when my application started throwing up after encountering unexpected nulls). Not a huge deal, though, since I have my own object-relational mapping tool that I use specifically for situations like this.