Buscar contenidos

miércoles, 4 de octubre de 2017

CRUD Entity Framework C#

Link artículo

  
Ejemplo Entity Framework CRUD

  1. using System.Linq;  
  2. using System.Web.Mvc;  
  3. using ExampleCodeFirstApproch.Models;   
  4. namespace ExampleCFA.Controllers  
  5. {  
  6.     public class StudentController : Controller  
  7.     {  
  8.        SchoolContext objContext;  
  9.        public StudentController()  
  10.         {  
  11.             objContext = new SchoolContext();  
  12.         }   
  13.         #region List and Details Students   
  14.         public ActionResult Index()  
  15.         {  
  16.             var studs = objContext.Students.ToList();  
  17.             return View(studs);  
  18.         }   
  19.         public ViewResult Details(int id)  
  20.         {  
  21.             Student stud = objContext.Students.Where(x=>x.StudentId==id).SingleOrDefault();  
  22.             return View(stud);  
  23.         }   
  24.         #endregion   
  25.         #region Create Student   
  26.         public ActionResult Create()  
  27.         {  
  28.             return View(new Student());  
  29.         }   
  30.         [HttpPost]  
  31.         public ActionResult Create(Student stud)  
  32.         {  
  33.             objContext.Students.Add(stud);  
  34.             objContext.SaveChanges();  
  35.             return RedirectToAction("Index");  
  36.         }   
  37.         #endregion   
  38.         #region Edit Student
  39.         public ActionResult Edit(int id)  
  40.         {  
  41.             Student stud = objContext.Students.Where(x => x.StudentId == id).SingleOrDefault();  
  42.             return View(stud);  
  43.         }    
  44.         [HttpPost]  
  45.         public ActionResult Edit(Student model)  
  46.         {  
  47.             Student stud = objContext.Students.Where(x => x.StudentId == model.StudentId).SingleOrDefault();  
  48.             if (stud != null)  
  49.             {  
  50.                 objContext.Entry(stud).CurrentValues.SetValues(model);  
  51.                 objContext.SaveChanges();  
  52.                 return RedirectToAction("Index");  
  53.             }                
  54.             return View(stud);  
  55.         }   
  56.        #endregion   
  57.         #region Delete Student   
  58.         public ActionResult Delete(int id)  
  59.         {  
  60.             Student stud = objContext.Students.Find(id);  
  61.             return View(book);  
  62.         }   
  63.         [HttpPost]  
  64.         public ActionResult Delete(int id, Student model)  
  65.         {  
  66.            var stud = objContext.Students.Where(x => x.StudentId == id).SingleOrDefault();  
  67.            if (stud != null)  
  68.             {  
  69.                 objContext.Students.Remove(stud);  
  70.                 objContext.SaveChanges();  
  71.             }  
  72.             return RedirectToAction("Index");  
  73.         }  
  74.         #endregion  
  75.     }  


lunes, 2 de octubre de 2017

C# Trace a través de Event Viewer EventLog

Link artículo



  using (EventLog eventLog = new EventLog("Application"))
            {
                eventLog.Source = "Application";
                eventLog.WriteEntry("Log message example", EventLogEntryType.Information, 101, 1);
            }

enter image description here


https://carldesouza.com/write-windows-event-viewer-c/

WRITE TO WINDOWS EVENT VIEWER FROM C#

 1 Comment

First, create a new console app:

Next, add:

using System.Diagnostics;

Notice the Event Viewer in Windows has several areas:

  • Windows Logs, which are:
    • Application
    • Security
    • Setup
    • System
    • Forwarded Events

And within each log, there are:

  • Keywords
  • Date and Time
  • Source
  • Event Id
  • Task Category

To write to the application log, use the code:

        static void Main(string[] args)
        {
            string Event = "Application has started";

            using (EventLog eventLog = new EventLog("Application"))
            {
                eventLog.Source = "Application";
                eventLog.WriteEntry(Event, EventLogEntryType.Information);
            }
        }

This will write to the Application log. Note the event id is not found for the new application:

In order to create a new log in the event viewer, use the code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
 
namespace Carl.EventViewerTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string Event = "Application has started";
            string Source = "My App";
            string Log = "App";
 
            if (!EventLog.SourceExists(Source))
                EventLog.CreateEventSource(Source, Log);
 
            using (EventLog eventLog = new EventLog("App"))
            {
                eventLog.Source = "My App";
                eventLog.WriteEntry(Event, EventLogEntryType.Information);
            }
        }
    }
}

Notice there is a new log, “App” created under Applications and Service Logs:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.