Pages

Tuesday, October 9, 2018

Entity Framework Connectivity VS2015


/****** Object:  Table [dbo].[Employee]    ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Employee1](
[HREmpId] [numeric](18, 0) NOT NULL,
[FirstName] [nchar](10) NULL
) ON [PRIMARY]

GO


------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;


public class MyContext:DbContext

{
    public MyContext() :base("DefaultConnection")
    {
        //Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());
    }

     virtual public DbSet<Employee> employee { get; set; }
}

---------------------------------------------------------------------------------------------------------------------
using System.ComponentModel.DataAnnotations;


public class Employee
{
    [Key]
    public decimal HREmpId { get; set; }
    public string FirstName { get; set; }
 

}

-------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (MyContext m = new MyContext())
        { var s="";
            foreach (var a in m.employee)
            {
                s = s + a.FirstName;
            }
            Response.Write(s);
        }
     }
}

-------------------------------------------------------------------------------------------------------------------------
<connectionStrings>
    <add name="DefaultConnection"
      connectionString="Data Source=localhost\SQLSERVER;Initial Catalog=LearnEF;Persist Security Info=True;User ID=sa;password=123456"
      providerName="System.Data.SqlClient"/>
  </connectionStrings>

Monday, October 1, 2018

ReadXml c#

using System;
using System.Text;
using System.Xml;

namespace ReadXml
{
    class Program
    {
        static void Main(string[] args)
        {           
            XmlReader xmlReader = XmlReader.Create("xml file location");
            while(xmlReader.Read())
            {
                if((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Cube"))
                {
                    if(xmlReader.HasAttributes)
                        Console.WriteLine(xmlReader.GetAttribute("currency") + ": " + xmlReader.GetAttribute("rate"));                   
                }
            }
            Console.ReadKey();
        }
    }
}

How to read XML in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

            // Start with XmlReader object

            //here, we try to setup Stream between the XML file nad xmlReader

            using (XmlReader reader = XmlReader.Create(@"D:\xmlFile\demo.xml"))

            {

                while (reader.Read())

                {

                    if (reader.IsStartElement())

                    {

                        //return only when you have START tag



                        switch (reader.Name.ToString())

                        {

                            case "Name":

                            Response.Write("Name of the Element is : " + reader.ReadString());

                                break;



                            case "Location":

                            Response.Write("Your Location is : " + reader.ReadString());

                                break;

                        }

                    }

               

                }

            }

         

        }

    }
---------------------------------XML file--------------------------
<?xml version="1.0" encoding="utf-8"?>

<Students>

  <Student>

    <Name>suneel</Name>

    <Location>Delhi</Location>

  </Student>

  <Student>

    <Name>Sadhna</Name>

    <Location>location4</Location>

  </Student>

  <Student>

    <Name>Name2</Name>

    <Location>location2</Location>

  </Student>

  <Student>

    <Name>Name3</Name>

    <Location>location3</Location>

  </Student>

  <Student>

    <Name>Name4</Name>

    <Location>location4</Location>

  </Student>

</Students>

https://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part
https://www.codeproject.com/Articles/487571/XML-Serialization-and-Deserialization-Part