locked
How to add database connection for ORM RRS feed

  • Question

  • Hi Guys,

    I am beginner of .net framework.I was asked what is ORM in the interview.I told them that what is the use of it.He asked how to connect your database.I answered using server explorer the database can be added.But he was not satisfied with my answer.He expects some advanced level of connecting database.Could anyone please explain how to that.

    Somewhere should we need to give the connection string?

    Sunday, October 22, 2017 8:00 PM

Answers

  • Hi sharmilajoseph,

    Thank you for posting here.

    >> He asked how to connect your database.

    You have two ways to answer the question.

    1. You could use manual method. You use SqlConntection class to connect your database.You could refer to the following link which is about SqlConnection class which is how to connect to SQL Server database and perform some operations for your database :http://www.c-sharpcorner.com/UploadFile/201fc1/introduction-to-sql/
    2. You could also use the database First pattern of Entity Framework to connect an existing database by Server Explorer as you said .It automatically connects to your database and creates some models according to tables  of your database.You could refer  to the following link:https://msdn.microsoft.com/en-us/library/jj206878(v=vs.113).aspx
    3. You could use DataContext Class to connect to a database about LINQ to SQL. I created a demo about using DataContext class by add a reference System.Data.Linq assembly. Please refer to it:
    using System;
    using System.Linq;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Data;
    namespace ConsoleApp01
    {
        class Program
        {
            static void Main(string[] args)
            {
                IDbConnection con=new System.Data.SqlClient.SqlConnection(@"Server=VDI-V-JIANPW\SQLEXPRESS;database= DatabaseName;user=UserName;password=**** $");
                DataContext context = new DataContext(con);
                Table<Person> people = context.GetTable<Person>();
                var result = people.ToArray();
                foreach(var item in result)
                {
                    Console.WriteLine(item.Id + ":" + item.Name + ":" + item.Email);
                }
            }
        }
        [Table(Name = "Person")]
        public class Person
        {
            private int _id;
            private string name;
            private string email;
            [Column(IsPrimaryKey = true, Name  = "Id")]
            public int Id
            {
                set
                {
                    _id = value;
                }
                get
                {
                    return _id;
                }
            }
    
            [Column(Name = "Name")]
            public string Name
            {
                set
                {
                    name = value;
                }
                get
                {
                    return name;
                }
            }
    
            [Column(Name = "Email")]
            public string Email
            {
                set
                {
                    email = value;
                }
                get
                {
                    return email;
                }
            }
        }
    }
    
    

    Here is my result:

    You could refer to  the following link about DataContext class and how to use it.

    DataContext class: https://msdn.microsoft.com/en-us/library/system.data.linq.datacontext(v=vs.110).aspx

    LINQ to SQL :https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/walkthrough-simple-object-model-and-query-csharp

    >> He expects some advanced level of connecting database.

    About it, I didn’t find more methods.

    I’m glad to be of help to you.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by sharmilajoseph Tuesday, October 24, 2017 3:31 PM
    Monday, October 23, 2017 7:12 AM

All replies

  • Hi sharmilajoseph,

    Thank you for posting here.

    >> He asked how to connect your database.

    You have two ways to answer the question.

    1. You could use manual method. You use SqlConntection class to connect your database.You could refer to the following link which is about SqlConnection class which is how to connect to SQL Server database and perform some operations for your database :http://www.c-sharpcorner.com/UploadFile/201fc1/introduction-to-sql/
    2. You could also use the database First pattern of Entity Framework to connect an existing database by Server Explorer as you said .It automatically connects to your database and creates some models according to tables  of your database.You could refer  to the following link:https://msdn.microsoft.com/en-us/library/jj206878(v=vs.113).aspx
    3. You could use DataContext Class to connect to a database about LINQ to SQL. I created a demo about using DataContext class by add a reference System.Data.Linq assembly. Please refer to it:
    using System;
    using System.Linq;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Data;
    namespace ConsoleApp01
    {
        class Program
        {
            static void Main(string[] args)
            {
                IDbConnection con=new System.Data.SqlClient.SqlConnection(@"Server=VDI-V-JIANPW\SQLEXPRESS;database= DatabaseName;user=UserName;password=**** $");
                DataContext context = new DataContext(con);
                Table<Person> people = context.GetTable<Person>();
                var result = people.ToArray();
                foreach(var item in result)
                {
                    Console.WriteLine(item.Id + ":" + item.Name + ":" + item.Email);
                }
            }
        }
        [Table(Name = "Person")]
        public class Person
        {
            private int _id;
            private string name;
            private string email;
            [Column(IsPrimaryKey = true, Name  = "Id")]
            public int Id
            {
                set
                {
                    _id = value;
                }
                get
                {
                    return _id;
                }
            }
    
            [Column(Name = "Name")]
            public string Name
            {
                set
                {
                    name = value;
                }
                get
                {
                    return name;
                }
            }
    
            [Column(Name = "Email")]
            public string Email
            {
                set
                {
                    email = value;
                }
                get
                {
                    return email;
                }
            }
        }
    }
    
    

    Here is my result:

    You could refer to  the following link about DataContext class and how to use it.

    DataContext class: https://msdn.microsoft.com/en-us/library/system.data.linq.datacontext(v=vs.110).aspx

    LINQ to SQL :https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/walkthrough-simple-object-model-and-query-csharp

    >> He expects some advanced level of connecting database.

    About it, I didn’t find more methods.

    I’m glad to be of help to you.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by sharmilajoseph Tuesday, October 24, 2017 3:31 PM
    Monday, October 23, 2017 7:12 AM
  • Thanks Bob:)
    Tuesday, October 24, 2017 3:32 PM