I have been asked to do this project using queues, but my problem is that I would like to know if datagridview data can be saved in memory without having to go to a database program.
I have been asked to do this project using queues, but my problem is that I would like to know if datagridview data can be saved in memory without having to go to a database program.
Very good,
I understand that what you want to store in memory are Client-type objects. Assuming you have a client class with this aspect:
public class Cliente
{
private int codigo;
private string nombre;
private string apellidos;
private string dni;
private int telefono;
private string direccion;
...
}
You can store objects of this class in a queue that looks like this:
Queue<Cliente> colaClientes = new Queue<Cliente>();
Greetings
First clarification: during the execution of an application the variables remain persistent in memory (unless they are released either by the "scope" scope or by Dispose). So you can add and remove items from your datagrid and they will remain during execution.
That said another option among many is to persist in a file. In this example, I serialize a list of clients in an xml file. I hope it serves as a reference.
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
namespace SerializacionConsola
{
class Program
{
static void Main(string[] args)
{
//Creo un cliente
Cliente chapulin = new Cliente()
{
Nombre = "Chapulin",
Apellido = "Colorado",
Codigo = 123
};
List<Cliente> cartera = new List<Cliente>();
cartera.Add(chapulin);
//Almaceno el listado en un archivo XML.
string pathArchivo = "./carteraClientes.xml";
Guardar(cartera, pathArchivo);
//Obtengo los datos que se almacenaron en el archivo.
var registrados = Obtener(pathArchivo);
//Los muestro por pantalla
foreach (Cliente item in registrados)
{
Console.WriteLine(item.Codigo + " : " + item.Nombre + " " + item.Apellido);
}
Console.ReadKey();
}
/// <summary>
/// Obtiene los datos desde el archivo XML.
/// </summary>
/// <param name="path">Ruta al archivo XML</param>
/// <returns></returns>
static List<Cliente> Obtener(string path)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path", "el path al archivo xml no puede ser nulo o vacio");
List<Cliente> listado = null;
//Deserialize
using (var reader = XmlReader.Create(path))
{
var serializer = new XmlSerializer(typeof(List<Cliente>));
listado = (List<Cliente>)serializer.Deserialize(reader);
}
return listado;
}
/// <summary>
/// Almacena los datos en un archivo xml.
/// </summary>
/// <param name="lista">Origen de datos</param>
/// <param name="path">Ruta al archvio XML</param>
static void Guardar(List<Cliente> lista, string path)
{
if (lista == null || lista.Count == 0)
throw new ArgumentNullException("listado", "El listado de clientes no puede ser nulo o vacio.");
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path", "El path al archivo xml no puede ser nulo o vacio");
using (var writer = XmlWriter.Create(path))
{
var serializer = new XmlSerializer(typeof(List<Cliente>));
serializer.Serialize(writer, lista);
}
}
}
/// <summary>
/// Entidad que representa al cliente
/// </summary>
public class Cliente
{
public int Codigo { get; set; }
public string Nombre { get; set; }
public string Apellido { get; set; }
}
}