Использование ASP.NET MVC без ORM

Звучит так, будто вы хотите использовать «вложенный» цикл foreach, но неясно, хотите ли вы, чтобы они записывали в два отдельных файла, или если вы хотите, чтобы они записывали в один и тот же файл. Я сделаю второй:

foreach (List<string> currentList in phase2)
{
     foreach (string currentElement in currentList)
     {
          //change this out to whatever file output technique you use.               
          Console.Write currentElement + ",";
     }
   //starts a new line
   Console.Write "\n"
}

Это должно дать вам представление о том, куда идти (предполагая, что вы хотите написать содержимое каждого списка в стиле столбцов / строк). Я почти настаиваю на использовании разделителя, если у вас нет особых причин не делать этого. Вы можете использовать любой символ, но соблюдение стандартов (,; | [tab]) для разделения столбцов действительно просто обеспечивает совместимость с будущими приложениями.

Если вам не нужен разделитель, просто удалите часть «+»,

.

Замените команды Console.Write вашими файлами вывода.

10
задан DOK 24 May 2009 в 12:12
поделиться

5 ответов

You could create simple classes for the data you want to transfer and then populate a List of objects in your controller from a data reader manually, and then pass this to your View - e.g. (C# but this should be easy to convert)

// open your connection / datareader etc.

List<Customer> customers = new List<Customer>();

while(dataReader.Read())
{
 Customer c = new Customer();
 c.Id = dataReader.GetInt32(0);
 c.Name = dataReader.GetString(1);
 // etc (you might want to use string indexers instead of ints for the get methods)

 customers.Add(c);
}

// close and dispose your datareader / connection etc as usual

return View("List", customers);
12
ответ дан 3 December 2019 в 14:06
поделиться

MVC is about separation of concerns. Passing SqlDataReaders, DataTables, or whatever class that resides in the System.Data namespace to a view is not a good idea. You need to define a model which might talk to the database, and a controller which will pass this model to the view. If your company policy says don't use an ORM then maybe classic WebForms are better suited to your scenario than the MVC pattern.

8
ответ дан 3 December 2019 в 14:06
поделиться

Try using DataTables - DataTable can load data from IDataReader... (I think the method's called Load)

5
ответ дан 3 December 2019 в 14:06
поделиться

You could create your own Data Transfer Object classes and populate instances of them using ADO.Net code. These DTO classes would be simple POCO-style classes that just contained property get/set accessors, no methods. Using POCO objects is arguably preferable to DataSets/DataTables as they are lightweight (no superfluous state) and are more intuitive to work with from an object-oriented perspective.

4
ответ дан 3 December 2019 в 14:06
поделиться

I agree with Rashack. This article explains it in some detail.link text

In a nutshell, here's how to do it using DataTable and DataReader:

private DataTable GetData()
{
    DataTable dt = new DataTable();

    using (SqlConnection connection
             = new SqlConnection("ConnectionString"))
    using (SqlCommand command = new SqlCommand())
    {
        command.Connection = connection;
        command.CommandText = "SELECT * FROM Customers";

        connection.Open();
        using (SqlDataReader reader =
            command.ExecuteReader
                (CommandBehavior.CloseConnection))
        {
            dt.Load(reader);
        }
    }

    return dt;
}

Then, you can read that DataTable into an entity object that you pass around.

I think you'll find this can yield much better performance than using Linq or an ORM.

8
ответ дан 3 December 2019 в 14:06
поделиться
Другие вопросы по тегам:

Похожие вопросы: