Gokul's Blog


Leave a comment

SQL CLR & CTE’s

http://blogs.msdn.com/jpapiez/archive/2005/09/26/474059.aspx

http://pierrerebours.com/blog/context-connection-and-sqlclr-functions

http://www.google.com/search?rlz=1C1GGLS_enUS301US303&sourceid=chrome&ie=UTF-8&q=logging+in+SQL+CLR

http://www.developer.com/net/net/article.php/3556571

http://www.sqlskills.com/BLOGS/BOBB/post/Seeing-errors-in-callers-SQLCLR-error-6522-and-T-SQL-TRY-CATCH.aspx

http://www.stephenforte.net/PermaLink,guid,cf62cd2a-d014-4793-ba3c-3c98335118c1.aspx


Leave a comment

File Comparison in C#

http://support.microsoft.com/kb/320348

http://dotnet-snippets.com/dns/c-compare-files-SID209.aspx

Method 1:

// compare. A return value of 0 indicates that the contents of the files
// are the same. A return value of any other value indicates that the 
// files are not the same.
private bool FileCompare(string file1, string file2)
{
     int file1byte;
     int file2byte;
     FileStream fs1;
     FileStream fs2;

     // Determine if the same file was referenced two times.
     if (file1 == file2)
     {
          // Return true to indicate that the files are the same.
          return true;
     }

     // Open the two files.
     fs1 = new FileStream(file1, FileMode.Open);
     fs2 = new FileStream(file2, FileMode.Open);

     // Check the file sizes. If they are not the same, the files 
        // are not the same.
     if (fs1.Length != fs2.Length)
     {
          // Close the file
          fs1.Close();
          fs2.Close();

          // Return false to indicate files are different
          return false;
     }

     // Read and compare a byte from each file until either a
     // non-matching set of bytes is found or until the end of
     // file1 is reached.
     do
     {
          // Read one byte from each file.
          file1byte = fs1.ReadByte();
          file2byte = fs2.ReadByte();
     }
     while ((file1byte == file2byte) && (file1byte != -1));

     // Close the files.
     fs1.Close();
     fs2.Close();

     // Return the success of the comparison. "file1byte" is 
     // equal to "file2byte" at this point only if the files are 
        // the same.
     return ((file1byte - file2byte) == 0);
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Method  2 :  (http://dotnet-snippets.com/dns/c-compare-files-SID209.aspx)

private bool CompareFiles(string File1, string File2)
{
        FileInfo FI1 = new FileInfo(File1);
        FileInfo FI2 = new FileInfo(File2);

        if (FI1.Length != FI2.Length)
               return false;

        byte[] bytesFile1 = File.ReadAllBytes(File1);
        byte[] bytesFile2 = File.ReadAllBytes(File2);

        if (bytesFile1.Length != bytesFile2.Length)
               return false;

        for (int i = 0; i <= bytesFile2.Length - 1; i++)
        {
               if (bytesFile1[i] != bytesFile2[i])
                       return false;
        }
        return true;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }


Leave a comment

Get Xpath Value from XMLTextReader

The below code is used to get Xpathvalue from a xmltextreader object. 
Thanks to Naresh(my-colleague) for this quick snippet.
      void TestOne(XmlTextReader reader,string xpathQuery)
        {
            System.Console.WriteLine("TestOne");
            XPathDocument xdoc = new XPathDocument(reader);
            XPathNavigator nav = xdoc.CreateNavigator();
            XPathNodeIterator nodeItor = nav.Select(xpathQuery);
             //"STUFF/TYPE1/CENSUS[@COUNTRY='USA' and @YEAR='1930']/PAGE");
            nodeItor.MoveNext();
            TraverseSiblings(nodeItor);
            System.Console.WriteLine();
        }

         void TraverseSiblings(XPathNodeIterator nodeItor)
        {
            XPathNodeIterator igor = nodeItor.Clone();
            PrintNode(igor.Current);
            igor.Current.MoveToNext();
            bool more = false;
            do
            {
                PrintNode(igor.Current);
                more = igor.Current.MoveToNext();
            } while (more);
        }

         void PrintNode(XPathNavigator nav)
        {
            System.Console.WriteLine(nav.Name + ":" + nav.Value +
                " Type : " + nav.NodeType.ToString());
        }
 
 
One more code sample which I have implemented 
            XPathNavigator nav;
            XPathDocument doc;
            XPathNodeIterator NodeIter;
            String rv=string.Empty;

            doc = new XPathDocument(reader);
            nav = doc.CreateNavigator();
            //sXpathQuery = @"//EmployeeName/attribute::SourceID";
            System.Diagnostics.Debug.WriteLine("Source Id:  {0}", nav.Evaluate(sXpathQuery).ToString());
                NodeIter = nav.Select(sXpathQuery);
                while (NodeIter.MoveNext())
                {
                    rv=NodeIter.Current.InnerXml;

                    System.Diagnostics.Debug.WriteLine("------------item start------------------");
                    System.Diagnostics.Debug.WriteLine(rv);
                    System.Diagnostics.Debug.WriteLine("-------------item end-------------------");
                    //Console.WriteLine(NodeIter.Current.Value);
                    //Console.WriteLine("////////////////////////////////");

                    if (!string.IsNullOrEmpty(rv))
                        break;
                }
Other interesting links

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }


Leave a comment

Get value from Registry settings in C#

The below method is used to get the connection string from Registry or BizTalk Config file

public static string GetConnectionString()
        {
//            RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"Software\MYcompany\Connstring");
//            String sConn = (String)reg.GetValue("ConnStr", "Integrated Security=SSPI;Data Source=.;Initial Catalog=BizTalkMsgBoxDb");
//            reg.Close();
            string sConn;
            // Read from the BTSNTSvc.exe.config
            sConn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString();
            if (sConn.Length <= 0)
            {
                  throw;
            }
            return sConn;
        }