static void Main(string[] args) { #region Leo de SQL List <Televisor> listaTelevisores = new List <Televisor>(); SqlConnection conexion = new SqlConnection(Properties.Settings.Default.Conexion2); //Establezco una conexion al SQL Server. Properties.Settings.Default.Conexion -> hace referencia a la conexion establecida en los settings del proyecto. SqlCommand comando = new SqlCommand(); //Establezco un comando a ejecutar sobre la base de datos comando.CommandType = CommandType.Text; //Establezco el tipo de comando a realizar. /*CommandType.StoredProcedure: ejecuta un script creado previamente (Ejemplo una consulta). * CommandType.TableDirect: Nombre de la tabla * CommandType.Text: Instruccion a ejecutar sobre la base de datos. * */ comando.CommandText = "SELECT * FROM Televisores"; //Establezco el comando en si comando.Connection = conexion; //Le asigno al comando la conexion sobre la cual va a trabajar. conexion.Open(); //Abro la Conexion SqlDataReader lector = comando.ExecuteReader(); //Objeto que lee linea por linea la base de datos while (lector.Read()) //Leo una linea y se le asigna al objeto lector { //Televisor televisor = new Televisor((int)lector["codigo"], (string)lector["marca"], (float)lector["precio"], (int)lector["pulgadas"], (string)lector["pais"]); Televisor televisor = new Televisor(lector.GetInt32(0), lector.GetString(1), lector.GetDouble(2), lector.GetInt32(3), lector.GetString(4)); //Leo la base de datos listaTelevisores.Add(televisor); Console.WriteLine("{0}-{1}-{2}-{3}-{4}", lector["codigo"], lector["marca"], lector["precio"], lector["pulgadas"], lector["pais"]); } conexion.Close(); //Cierro la conexion al server #endregion #region XML //Serializo la lista de teles en un XML. XmlSerializer serializer = new XmlSerializer(typeof(List <Televisor>)); XmlTextWriter writer = new XmlTextWriter("Televisores.xml", Encoding.UTF8); XmlTextReader reader = new XmlTextReader("Televisores.xml"); serializer.Serialize(writer, listaTelevisores); writer.Close(); List <Televisor> listaDeserializada = (List <Televisor>)serializer.Deserialize(reader); reader.Close(); #endregion #region DataTable conexion.Open(); lector = comando.ExecuteReader(); DataTable dataTable = new DataTable("Televisores"); //Creo una nueva datatable dataTable.Load(lector); //Cargo los datos de la base de datos en el DataTable. Recibe un ejercicio SqlDataReader. conexion.Close(); dataTable.WriteXmlSchema("Televisores_esquema.xml"); //Serializo la estructura del datatable en un xml. dataTable.WriteXml("Televisores_dt.xml"); //Serializo los datos de un xml en el datatable DataTable dataTableRead = new DataTable(); dataTableRead.ReadXmlSchema("Televisores_esquema.xml"); dataTableRead.ReadXml("Televisores_dt.xml"); //Agrego tele a la DataTable Televisor televisorAgregar = new Televisor(1245, "Samsung", 15000, 32, "Japon"); Console.WriteLine(televisorAgregar.Insertar()); #endregion Console.ReadKey(); }
static void Main(string[] args) { SqlConnection conexion = new SqlConnection(Properties.Settings.Default.conexion);//conexion es la variable que creamos en la configuracion SqlCommand comando = new SqlCommand(); List <Televisor> televisores = new List <Televisor>(); comando.CommandText = "SELECT *FROM Televisores"; //estos dos van de la mano comando.CommandType = System.Data.CommandType.Text; comando.Connection = conexion; //es una propiedad conexion.Open(); //abre conexion SqlDataReader lector = comando.ExecuteReader(); while (lector.Read()) { Console.Write(lector["pais"] + " - "); //Console.Write(lector[0] + " - "); Televisor tele = new Televisor(lector.GetInt32(0), lector.GetString(1), lector.GetDouble(2), lector.GetInt32(3), lector.GetString(4));//leo lo de la base de datos y lo agrego a la lista de televisor // Televisor tele = new Televisor((int)lector["id"],(string) lector["marca"], (double)lector["precio"], (int)lector["pulgadas"], (string)lector["pais"]);//todos devuelven object , ahora tenemos que castearlo televisores.Add(tele); } conexion.Close(); //cierra conexion XmlSerializer sr = new XmlSerializer(typeof(List <Televisor>)); // nole pasamos un solo televisr sino la LISTA para que serialize una coleccion de objetos XmlTextWriter xw = new XmlTextWriter("Televisor.xml", Encoding.UTF8); XmlTextReader xr = new XmlTextReader("Televisor.xml"); sr.Serialize(xw, televisores); xw.Close(); // lo cerramos pa que no pinche List <Televisor> listaTelevisorRead; listaTelevisorRead = (List <Televisor>)sr.Deserialize(xr); xr.Close(); conexion.Open(); lector = comando.ExecuteReader(); //genero replica DataTable dataTable = new DataTable("Televisor"); //representacion en memoria de una tabal en base de datos dataTable.Load(lector); //ya lo agrega al data table conexion.Close(); //volvemos a cerrar dataTable.WriteXmlSchema("Televisores_esquema.xml"); //guardamos el esquema dataTable.WriteXml("Televisore_dt.xml"); //leemos el esquema DataTable dataTable2 = new DataTable(); dataTable2.ReadXmlSchema("Televisores_esquema.xml");//primero leemos el esquema dataTable2.ReadXml("Televisore_dt.xml"); Televisor telenueva = new Televisor(5, "sony", 22000, 45, "Estados Unidos"); Console.WriteLine(telenueva.Insertar()); Console.ReadKey(); }