Tuesday, February 25, 2014

Find DLL location in Global Assembly Cache (GAC)

This method will give the location of an assembly from GAC



private string FindDLLInGAC(string dll)
{    foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
    {        if (a.ManifestModule.ScopeName.Equals(dll))
             return a.Location;
    }     return null; //Not found
}

//Usage
string asmLocation = FindDLLInGAC("TheDLL.dll");
Assembly asm = Assembly.LoadFile(asmLocation);
var thetype = asm.GetType(theType, true, false);

Monday, February 24, 2014

Load Assembly to dynamically create an instance of a class


This will load an assembly and dynamically create an instance of a class.


...
string asmLocation = Path.Combine(AssemblyDirectory(), "TheDLL.dll");
Assembly asm = Assembly.LoadFile(asmLocation);
var theType = asm.GetType("TheType", true, false);
IRunnable runnable = Activator.CreateInstance(theType) as IRunnable;

runnable.Execute();
...

 
private string AssemblyDirectory()
{    string codeBase = Assembly.GetExecutingAssembly().CodeBase;
    UriBuilder uri = new UriBuilder(codeBase);
    string path = Uri.UnescapeDataString(uri.Path);
           
    return Path.GetDirectoryName(path);
}

Tuesday, January 28, 2014

SQL Reference two database instances in a query


Within a SQL query or stored procedure, multiple database tables can be referenced.  The full database name just needs to be specified in the query.

 

SELECT *

FROM Appointment a

INNER JOIN TableLog.dbo.Appointment al ON al.appointmentid=a.appointmentid

WHERE a.appointmentid = @appointmentid

C# use a Type in a generic method definition

To use a Type within a generic method initialization, it can be accomplished with:


MethodInfo method = typeof(NameOfClass).GetMethod("MethodName", BindingFlags.Public | BindingFlags.Static);

method = method.MakeGenericMethod(type);
 
string results = (string)method.Invoke(null, new object[] { props[key] });

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.