Tuesday, November 19, 2013

SQL query runs fast but stored procedure equivalent runs slow

If a SQL stored procedure is running slow, but the query is quick, try declaring local variables to replace the parameters in the stored procedure.

* Not sure why this works, please leave a comment if you have any info.

Replace the stored procedure paramters with local variables

ALTER PROCEDURE [dbo].[SlowProcedure]
(
@PARAM_begindate SMALLDATETIME = NULL,
@PARAM_enddate SMALLDATETIME = NULL
)
AS
--Add @PARAM_ variables to speed up query

DECLARE @begindate SMALLDATETIME,
                  @enddate SMALLDATETIME

SELECT  @begindate = @PARAM_begindate,
                @enddate = @PARAM_enddate
...

SELECT *
FROM [Table]
WHERE Date BETWEEN @begindate AND @enddate

Friday, November 15, 2013

Use XSD to parse XML

1.) Find xsd.exe utility to convert .xsd into .cs class
   - xsd.exe should be found here: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\

2.) Run xsd.exe against the file to be converted.
   - xsd /c yourfile.xsd
   - This will turn the .xsd file into a C# class

3.) Now, with the C# class, you are able to deserializing the XML into an instance of your new object.

XmlSerializer serializer = new XmlSerializer(typeof(yourClass));
string filename = Path.Combine(FilePath, "theXml.xml");
yourClass myClass = serializer.Deserialize(new FileStream(filename, FileMode.Open)) as yourClass;
if (myClass != null)
{
   //...
}

Thursday, October 24, 2013

C# implementation of SQL IN statement


SQL functionality to mimic:
SELECT * FROM table WHERE number IN (16, 30, 78, 80)

C# example that will work similarly.
List<int> Numbers = new List<int> { 16, 30, 78, 80 };
int number = 78;
if (Numbers.Contains(number))
{
    //...
}

Note: To make the functionality more similar, a class could be created inheriting from List which exposes an "IN" method which would simply encapsulate the List.Contains method.

Sunday, October 20, 2013

C# copy a Queue so initial values remain.

public static T DeepClone<T>(T obj)
{
    using (var ms = new MemoryStream())
    {
        var formatter = new BinaryFormatter();
        formatter.Serialize(ms, obj); ms.Position = 0;
       
        return (T) formatter.Deserialize(ms);
    }
}

//The class object must be marked as Serializable.

Friday, September 13, 2013

HTTP Post using Javascript

HTTP POST to a website using Javascript and Microsoft XMLHTTP
<html>
<head>
<script type="text/javascript">
  function HTTPPostButton_onclick()
  {
    var url = "...";
    var method = "POST";
    var dataToSend = "<Details>...</Details>";

    var connect = new ActiveXObject("Microsoft.XMLHTTP");
    connect.Open(method, url, false);
    connect.setRequestHeader("Content-Type", "text/xml");
    connect.send(dataToSend);
    document.getElementById("result").innerHTML=connect.responseText;
  }
</script>
</head>

<input type="button" value="Test" onclick="return HTTPPostButton_onclick()" />
<span id="result"></span>
</html>

Tuesday, September 10, 2013

C++ Convert number to base 2

A method to convert a BigInt to base 2


int* Base2Conversion(BigInt num)
{    int totBits = num.bits();
    int *i = new int[totBits];
    int idx = 1;
 
    do{
        i[totBits - idx] = num%2;
        num /= 2;
        idx++;
    }
    while(num > 0);

return i;
}
 
 
 //Use of the method above
BigInt num = 453453463465767897809789787687456765476;
int totBits = num.bits();
int* binary = Base2Conversion(num); BigInt result = 1;
for(int i = 1; i < totBits; i++)
{
    result *= result;

    if(binary[i] == 1)
    {
        result *= 5;
    }
}
 
 

Wednesday, July 24, 2013

DNS issues

When experiencing DNS issues, the following command will display what the current IPs in cache to access a resource.


Open command prompt:
C:\Users\user> ipconfig /displaydns.

To output the results to a file instead of the Console, simply add a greater than and file name.

C:\Users\user> ipconfig /displaydns >TestDNS.txt

If a problem is seen in the results, the local cache can be cleared with the following:
C:\Users\ser> ipconfig /flushdns