C# Reboot or Shutdown windows

10. February 2011 18:22

Here a quick guide to show you how to reboot a windows computer from C#

Its actually reeally very simple.

With the following code you can create your own User32 class to call windows api directly.


public static class User32
        public static extern bool ExitWindowsEx(ExitWindowsFlags uFlag, UInt32 dwReserved);

        public static bool ExitWindowsEx(ExitWindowsFlags uFlag)
            return ExitWindowsEx(uFlag, 0);


Note: you will also need 'using System.Runtime.InteropServices;' at the top of the C# file.

You will probably also want to define all the options that are avilable to use with the call.


    public enum ExitWindowsFlags {
        EWX_LOGOFF = 0,
        EWX_SHUTDOWN = 0x1,
        EWX_REBOOT = 0x2,
        EWX_FORCE = 0x4,
        EWX_POWEROFF = 0x8,
        EWX_FORCEIFHUNG = 0x10,
        EWX_RESTARTAPPS = 0x40


The rest is now really simple.


static void Main(string[] args)
            if (User32.ExitWindowsEx(ExitWindowsFlags.EWX_REBOOT) == false)


E-mail Kick it! DZone it! del.icio.us Permalink

ASP.NET Gridview Bound Yes No

6. February 2011 16:47

This is a simple solution for a really common problem. I assume that most people do the same as as most other programmers and have a common shared libaray of code between all their project that they add to and use the components as required.


The problem with a database and a bit field when binded to a gridview normally isnt very end user friendly. So to make it end user friendly people tend to either use a case statement inside sql to turn the "1" or "0" into a "Yes" or "No". Or they repeat the same code all over the place in mutliple projects using the datarow bound event handler where they use the bit fields.


Here is an alternative solution. In your shared project lib. Add the following class (adding the correct namespace etc..)


public class BoundYesNo : BoundField
        protected override string FormatDataValue(object dataValue, bool encode)
                bool tmp = (bool)dataValue;

                if (tmp)
                    return "Yes";
                return "No";
            catch (Exception ex)
                return ex.Message;


When you then add the reference to your web config you can then use the above code just like any other bound field on a gridview in asp.net. Now you have something in all your projects that only require you to write asp code and less c#

E-mail Kick it! DZone it! del.icio.us Permalink

MSSQL Levenshtein

5. February 2011 11:31

This is a great extention to have for mssql. There is more information about what the function does on the levenshtein wiki page in short it will give you a number of changes (add / delete / change) to get from the first string to the second string.

For example: "Hello World" and "Hello Worlds" has a distance of 1. This can be great if you are trying to do searches where you want to pick up on typo's or spelling mistakes.

The c# code for the extenstion is


[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = false)]
    public static SqlInt32 Levenshtein(SqlString S1, SqlString S2)
        if (S1.IsNull || S2.IsNull)
            throw (new ArgumentNullException());

        int n = S1.Value.Length;
        int m = S2.Value.Length;

        int[,] d = new int[n + 1, m + 1];
        int cost = 0;

        if (n == 0)
            return m;
        if (m == 0)
            return n;

        for (int i = 0; i <= n; i++)
            d[i, 0] = i;

        for (int j = 0; j <= m; j++)
            d[0, j] = j;

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                if (S1.Value[i-1] == S2.Value[j-1])
                    cost = 0;
                    cost = 1;

                d[i, j] = System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);

        return d[n, m];


Once compiled you can create a function for mssql woth the following code.


CREATE Function Levenshtein(@S1 nvarchar(512), @S2 nvarchar(512))
	RETURNS int as EXTERNAL NAME SqlExtensions.UserDefinedFunctions.Levenshtein

You can read more about enabling clr support in mssql here

E-mail Kick it! DZone it! del.icio.us Permalink

MS SQL - Kill connections to a database

2. February 2011 19:33

There can be times that you need to kill all connections to a data so that you can perform emergency maintenance. Or for some suitable reason either way you want to disappear the current people who are logged in.

So here is a little stored procedure that will do just that.

CREATE PROCEDURE [dbo].[KillConnectionsdb] @dbname varchar(MAX)
	DECLARE @spid int
	DECLARE @sql varchar(MAX)

		SELECT spid FROM sys.sysprocesses P
			JOIN sys.sysdatabases D ON (D.dbid = P.dbid)
			WHERE LTRIM(RTRIM(D.Name)) = @dbname
			AND P.spid != @@SPID

	OPEN cur

		INTO @spid
		PRINT CONVERT(varchar, @spid)

		SET @sql = 'KILL ' + RTRIM(@spid)
		PRINT @sql

			INTO @spid

	CLOSE cur


It is really simply to use.

Just call the stored procedure with in the following way where 'Testing' happens to be the name of the database you want to kill off all the connections.

EXEC KillConnectionsdb @dbname = 'Testing'

Note: the stored procedure will avoid attempting to kill its own connection in the process.

E-mail Kick it! DZone it! del.icio.us Permalink

MS SQL Convert unix timestamp to date time

1. February 2011 18:51


It's something that all of a sudden when you think you need it. You cant think of how to do it.

So here is a quick function that turns a unix timestamp into a datetime


CREATE FUNCTION [dbo].[UNIXToDateTime] (@timestamp int)
	RETURNS datetime
	DECLARE @ret datetime
	SELECT @ret = DATEADD(second, @timestamp, '1970/01/01 00:00:00')
	RETURN @ret

An example on how to use it


SELECT dbo.UNIXToDateTime(dbo.DateTimeToUNIX(GETDATE()))

You may also want to see the convert to unix timstamp function

E-mail Kick it! DZone it! del.icio.us Permalink