Friday, August 16, 2013

Find rocord(s) from below table which contains last characters as 'aaa' in Sql?

Find rocord(s) from below table which contains last characters as 'aaa'?

ProductID ProductName
1            Fan aab
2            Fan abc
3            Fan g a
4            Fan g aaa

SELECT     ProductId,SUBSTRING(ProductName, CHARINDEX('aaa',
ProductName, 1), LEN(ProductName)) AS Expr1, CHARINDEX('aaa',
ProductName, 1) AS indexid
FROM         Products
WHERE     (RIGHT(ProductName, 3) = 'aaa')

Result: 4 aaa 7

Write a query for find Rank value/Duplicated values using with condition in sql ?

;with abc(pid,pname,PPID)
as
(
SELECT     ProductID, ProductName,ROW_NUMBER() OVER(partition BY
ProductName ORDER BY ProductName) AS PPID
FROM         Products
)
--DELETE FROM ABC WHERE PID=2
SELECT * FROM ABC

Linq to SQL Like Operator

Linq to SQL Like Operator:

 Using String.StartsWith or String.Endswith

var query = from c in Customers
            where c.City.StartsWith("Lo")
            select c;

var query = from c in Customers
            where c.City.StartsWith("L") && c.City.Contains("n")
            select c;

var query = from c in ctx.Customers
            where SqlMethods.Like(c.City, "L_n%")

            select c; 

NetPeerTCPBinding / Peer to Peer binding in WCF

Using WCF NetPeerTcpBinding we can develop a peer-to-peer networking applications that use a TCP-level peer-to-peer mesh infrastructure.
Example for the Peer-to-Peer Application: IM, File Sharing Application, Games etc.
In Traditional Client Server model we have single and 1 to N number of clients making request to the server which is configured in centralized location. 

In Traditional Client/Server application.


In peer to peer (p2p) application each participate (node) acts as both a client and a server to the other participants in the network.

In peer – to- peer network environment peers rely on name resolution systems to resolve each other’s network locations (addresses, protocols, and ports) from names.  Peer to peer name resolution has been complicated by transient connectivity and shortcomings in the Domain Name system (DNS).
The Microsoft windows Peer –to – Peer networking platform solves this problem with the Peer name resolution protocol (PNRP).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace P2pWCF
{
    [ServiceContract]
    public interface IChat
    {
        [OperationContract(IsOneWay=true)]
        void SendMessage(string who, string message);
   
    }
    class ChatImp:IChat
    {
        public void SendMessage(string who, string message)
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("{0} Says : {1}", who, message);
            Console.ForegroundColor = ConsoleColor.Green;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace P2pWCF
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Your name");
            string name = Console.ReadLine();
            Console.WriteLine("Opening P2P Server host...");
            ServiceHost svc = new ServiceHost(typeof(ChatImp));
            svc.Credentials.Peer.MeshPassword = "paramesh@123";
            svc.Open();

            //Both Server and Client binding should match

            NetPeerTcpBinding np2p = new NetPeerTcpBinding();
            np2p.Security.Mode = SecurityMode.None;
            EndpointAddress ep = new EndpointAddress("net.p2p://ParamiChat");
            IChat pr = ChannelFactory<IChat>.CreateChannel(np2p, ep);
            pr.SendMessage(name,string.Format("{0} Joined the Mesh network.",name));

            string msg=string.Empty;
            while(msg!="Q")
            {
                Console.ForegroundColor=ConsoleColor.Yellow;
                msg=Console.ReadLine();
                pr.SendMessage(name,msg);
            }
            svc.Close();
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="P2pWCF.ChatImp">
        <endpoint address="net.p2p://ParamiChat" binding="netPeerTcpBinding"
                  bindingConfiguration="NewBinding" contract="P2pWCF.IChat"></endpoint>
      </service>    </services>
   
    <bindings>
      <netPeerTcpBinding>
        <binding name="NewBinding">
          <security mode="None"></security>
          <resolver mode="Pnrp"></resolver>
        </binding>
      </netPeerTcpBinding>     
    </bindings>

    <client>
      <remove contract="IMetadataExchanage" name="sb"/>
      <endpoint address="net.p2p://ParamiChat" binding="netPeerTcpBinding"
                bindingConfiguration="NewBinding" contract="P2pWCF.IChat" name="Client">      </endpoint>
     
    </client>
  </system.serviceModel>
</configuration>