Alexander Shlenchack
Alexander Shlenchack

Reputation: 3869

What the best way working with MySQL in MVC 3?

Now, for every model in my MVC 3 app I create data class (in separate directory) were store a code which working direct with MySQL:

public Models.Inflation Select(int id)
            {
                Models.Inflation inflation = new Models.Inflation();
                MySqlConnection connection = new MySqlConnection(ConnectionString.ConnectionStringMySQL);
                MySqlCommand command = new MySqlCommand("SELECT " +
                    "inflation.monthInf, " +
                     "inflation.indexInflation " +
                    "FROM " +
                    "inflation " +
                    "WHERE id=@id " +
                    "ORDER BY inflation.monthInf ASC",
                    connection);
                command.Parameters.AddWithValue("@id", id);
                MySqlDataReader reader = null;
                try
                {
                    connection.Open();
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        inflation.ID = id;
                        inflation.Date = DateTime.Parse(reader["monthInf"].ToString());
                        inflation.IndexInflation = decimal.Parse(reader["indexInflation"].ToString());
                    }
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                    connection.Close();
                }
                return inflation;
            } 

But this method very tedious (for every Model class I must create Select, Insert, Delete, Edit...). How to improve the development and not using Entity Framework (ORM difficult to connect in my hosting).?

Upvotes: 1

Views: 169

Answers (1)

Harvinder
Harvinder

Reputation: 89

You can use Microsoft Enterprise libraries. And in later stage if would like to migrate from MySql to SqlServer datebase you can do this by just changing your connection string.

Upvotes: 1

Related Questions