现在,对于我的MVC 3应用程序中的每个模型,我创建了数据类(在单独的目录中)存储了一个可直接与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;
}但是这种方法非常乏味(对于每个Model类我都必须创建Select,Insert,Delete,Edit ...)。如何改进开发并且不使用实体框架(ORM难以在我的托管中连接)。