Monday, July 07, 2014

Update Table from Another Table in SQL Server

Update Table from Another Table in SQL Server:  A simple scenario demonstrating how we can update Persons table with data from Employees table using update statement.  A simple but very effective query which can saves you from creating unnecessary cursor for updating from different tables.  By performance wise also Update statement is more efficient compared to creating update cursor.


Update DestTbl
Set DestTbl.ColumnABC = SourceTbl.ColumnXYZ
FROM    Employees SourceTbl
JOIN    Persons DestTbl
ON      DestTbl.KeyCol1 = SourceTbl.KeyCol5


Here:
SourceTbl stands for Source Table
DestTbl stands for Destination Table



Friday, July 04, 2014

C# Object to Json Object String Example

Single C# Object to Json Object String Example

Download:  https://github.com/dotnetguts/json2csharpAndcsharp2json

Install NewtonSoft.Json Nuget Package

C# Object we will be using in following example
class MemberPics
{
    public string PhotoName { get; set; }
    public bool IsModerated { get; set; }
}

Json Object String we will be using in following example
@"{'PhotoName':'mypic.jpg','IsModerated':'false'}"

Code Snippet
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace JsonPrac1
{
    class Program
    {
        static void Main(string[] args)
        {          
            string myJsonString = @"{'PhotoName':'mypic.jpg','IsModerated':'false'}";

            //From Json String to C# Object
            MemberPics cSharpObj = JsonConvert.DeserializeObject(myJsonString);         
            Console.WriteLine("Photo Name: " + cSharpObj.PhotoName + ",  IsModerated: " + cSharpObj.IsModerated);


            //From C# Object to Json String
            string jsonString = JsonConvert.SerializeObject(cSharpObj, Formatting.Indented);
            Console.WriteLine(jsonString);

            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
    }

    class MemberPics
    {
        public string PhotoName { get; set; }
        public bool IsModerated { get; set; }
    }
}


Output











C# Object Array to Json Object String Example

Install NewtonSoft.Json Nuget Package

C# Object we will be using in following example
class MemberPics
{
    public string PhotoName { get; set; }
    public bool IsModerated { get; set; }
}

Json Object String we will be using in following example
@"[
    {'PhotoName':'pic1.png','IsModerated':'true'},
    { 'PhotoName':'pic2.jpg','IsModerated':'true'}
]"

Code Snippet
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace JsonPrac1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myJsonArrayString = @"[{'PhotoName':'pic1.png','IsModerated':'true'},{ 'PhotoName':'pic2.jpg','IsModerated':'true'}]";
         
            //From Json String to C# Object          
            List cSharpObjList = JsonConvert.DeserializeObject>(myJsonArrayString);
         
            foreach (var item in cSharpObjList)
            {
                Console.WriteLine("Photo Name: " + item.PhotoName + ",  IsModerated: " + item.IsModerated);
            }
         
            //From C# Object to Json String
            string jsonArrayString = JsonConvert.SerializeObject(cSharpObjList, Formatting.Indented);
            Console.WriteLine(jsonArrayString);

            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
    }

    class MemberPics
    {
        public string PhotoName { get; set; }
        public bool IsModerated { get; set; }
    }
}

Output















Json String to C# Object Example

Single Json Object String to C# Object Example

Download:  https://github.com/dotnetguts/json2csharpAndcsharp2json

Install NewtonSoft.Json Nuget Package

C# Object we will be using in following example
class MemberPics
{
    public string PhotoName { get; set; }
    public bool IsModerated { get; set; }
}

Json Object String we will be using in following example
@"{'PhotoName':'mypic.jpg','IsModerated':'false'}"

Code Snippet
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace JsonPrac1
{
    class Program
    {
        static void Main(string[] args)
        {          
            string myJsonString = @"{'PhotoName':'mypic.jpg','IsModerated':'false'}";

            //From Json String to C# Object
            MemberPics cSharpObj = JsonConvert.DeserializeObject(myJsonString);         
            Console.WriteLine("Photo Name: " + cSharpObj.PhotoName + ",  IsModerated: " + cSharpObj.IsModerated);

            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
    }

    class MemberPics
    {
        public string PhotoName { get; set; }
        public bool IsModerated { get; set; }
    }
}


Output










Json Object Array String to C# Object List Example

C# Object we will be using in following example
class MemberPics
{
    public string PhotoName { get; set; }
    public bool IsModerated { get; set; }
}

Json Object String we will be using in following example
@"[
    {'PhotoName':'pic1.png','IsModerated':'true'},
    { 'PhotoName':'pic2.jpg','IsModerated':'true'}
]"

Code Snippet
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace JsonPrac1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myJsonArrayString = @"[{'PhotoName':'pic1.png','IsModerated':'true'},{ 'PhotoName':'pic2.jpg','IsModerated':'false'}]";
         
            //From Json String to C# Object          
            List cSharpObjList = JsonConvert.DeserializeObject>(myJsonArrayString);

            foreach (var item in cSharpObjList)
            {
                Console.WriteLine("Photo Name: " + item.PhotoName + ",  IsModerated: " + item.IsModerated);
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
    }

    class MemberPics
    {
        public string PhotoName { get; set; }
        public bool IsModerated { get; set; }
    }
}

Output










Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape