开发者

Connecting an ASP.NET MVC application to MySQL

How can I point my ASP.NE开发者_C百科T MVC application to a MySQL database?


Once you download the MySQL ADO.NET Connector it's a simple matter of referencing the assembly in your project and writing the queries, the same way you would do in any other application, nothing specific MVC:

using (var connection = new MySqlConnection(ConnectionString))
using (var cmd = connection.CreateCommand())
{
    connection.Open();
    cmd.CommandText = "SELECT name FROM foo;";
    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            string name = reader.GetString(reader.GetOrdinal("name"));
            // TODO: do something with the name ...
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜