Monday, April 28, 2008
On windows files can be locked by different programs. This little gem unlocks your file so that you can delete it.

4/28/2008 10:13 AM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, April 17, 2008
I found out yesterday that my sql server backups were corrupt and so I couldn't restore them (happy me). So I went out on Google and combined a few things to make this

declare @verifystatement nvarchar(250)
declare @backupdevice nvarchar(250)
declare @err int

set @backupdevice =     (SELECT     TOP 1 [physical_device_name]
            FROM     msdb.dbo.backupmediafamily backupmediafamily
                JOIN msdb.dbo.backupset backupset ON backupmediafamily.media_set_id = backupset.media_set_id
                and backupset.backup_start_date = (    SELECT max(backup_start_date)
                                    FROM msdb.dbo.backupset child
                                    WHERE child.database_name = backupset.database_name and child.type = 'D')
                and database_name = 'TexDatabase'
                and backupset.type = 'D')

set @verifystatement = 'restore verifyonly from disk = ''' + @backupdevice + ''''
exec sp_executesql @verifystatement
SELECT @err = @@error

if @err <> 0
        begin
            insert into utils.dbo.tbl_backupverify(BackupDevice,BackupStatus,BackupError, VerifyStatement)
            values (@backupdevice, 'Failed', @err, @verifystatement)
        end
        else
        begin
            insert into utils.dbo.tbl_backupverify(BackupDevice,BackupStatus)
            values (@backupdevice, 'Succes')
        end

Just put it in a Store procedure and run it as a job.


This is the tbl_BackupVerify create table statement.

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tbl_BackupVerify]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tbl_BackupVerify]
GO

CREATE TABLE [dbo].[tbl_BackupVerify] (
    [Id] [int] IDENTITY (1, 1) NOT NULL ,
    [BackupDevice] [nvarchar] (250) COLLATE Latin1_General_CI_AS NOT NULL ,
    [BackupStatus] [nvarchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
    [VerifyDate] [datetime] NOT NULL ,
    [BackupError] [bigint] NULL ,
    [VerifyStatement] [nvarchar] (500) COLLATE Latin1_General_CI_AS NULL 
) ON [PRIMARY]
GO

You could add this as a scheduled job in sql-server.

Just don't forget to check the log table or make it send an email.



4/17/2008 2:00 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
SELECT *
FROM msdb.dbo.backupmediafamily backupmediafamily
JOIN msdb.dbo.backupset backupset ON backupmediafamily.media_set_id = backupset.media_set_id
and backupset.backup_start_date = (SELECT max(backup_start_date)
FROM msdb.dbo.backupset child
WHERE child.database_name = backupset.database_name and child.type = 'D')
and database_name = 'ReplaceWithDatabaseNameHere'
and backupset.type = 'D'


This is Based on something I found here. Thank you mrDenny.

And this works in SQL Server 2000.

4/17/2008 10:38 AM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, April 15, 2008
So I also made the a similar template with structuremap support included.

Wich looks something like this

Imports Nunit.FrameWork
Imports StructureMap

Namespace $NAMESPACE$
    ''' <summary>
     ''' A TestClass
      ''' </summary>
       ''' <remarks></remarks>
     <TestFixture()> _
     Public Class $CLASSNAME$
    
#Region " Setup and TearDown "
        ''' <summary>
         ''' Sets up the Tests
          ''' </summary>
           ''' <remarks></remarks>
         <Setup()> _
          Public Sub Setup()
                       StructureMapConfiguration.UseDefaultStructureMapConfigFile = False
                   StructureMapConfiguration.ScanAssemblies.IncludeTheCallingAssembly()
            End Sub

        ''' <summary>
         ''' Tears down the test. Is executed after the Test is Completed
          ''' </summary>
           ''' <remarks></remarks>
         <TearDown()> _
          Public Sub TearDown()
                ObjectFactory.ResetDefaults()        
             End Sub      
#End Region     

#Region " Tests "
                   ''' <summary>
         ''' A Test
          ''' </summary>
           ''' <remarks></remarks>
             <Test()> _
              Public Sub $Test_Name$()
           
               End Sub
#End Region

     End Class
End Namespace
And of course I also made an XmlFile so you can easily import it.

NunitTestFixtureWithStructureMapSupport.xml (2,44 KB)
4/15/2008 2:52 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
I made a file template for resharper to make it eassier to create a TestClass

it looks like this.

Imports Nunit.FrameWork

Namespace $NAMESPACE$
    ''' <summary>
     ''' A TestClass
      ''' </summary>
       ''' <remarks></remarks>
     <TestFixture()> _
     Public Class $CLASSNAME$
    
#Region " Setup and TearDown "
        ''' <summary>
         ''' Sets up the Tests
          ''' </summary>
           ''' <remarks></remarks>
         <Setup()> _
          Public Sub Setup()
    
            End Sub

        ''' <summary>
         ''' Tears down the test. Is executed after the Test is Completed
          ''' </summary>
           ''' <remarks></remarks>
         <TearDown()> _
          Public Sub TearDown()
            
        End Sub      
#End Region     

#Region " Tests "
                   ''' <summary>
         ''' A Test
          ''' </summary>
           ''' <remarks></remarks>
             <Test()> _
              Public Sub $Test_Name$()
           
               End Sub
#End Region

     End Class
End Namespace
I also made an xmlFile so you can easily import it into Resharper.

NunitTestFixture.xml (2,15 KB)
.Net | Nunit | Resharper
4/15/2008 2:49 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, April 14, 2008
Sometimes we just add references and never use them or use them and then remove it all again. And sometimes it is hard to say which reference we don't use anymore. Well MS has provided us with a button that looks up the unused references and if it finds ione you can remove the unused references.

So how do we do it.

First we right clikc on our project and select properties.



Then we go to the tab references and click on the button Unused References.




This will open a dialog with either a list of unused references (see below) or nothing in it.




And now you can just click remove and the references will be gone.


4/14/2008 1:48 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
Changing the targeted framework of a project in VB.NET 2008 with the Visual Studio IDE goes like this.

First we click right on the project name and choose properties.



Then we select the compile tab and click on the "Advanced Compile Options..." Button.



And last but not least we select the desired framework from the list "Target framework (all configurations)".





4/14/2008 1:32 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
 Sunday, April 13, 2008
Now that I have StructureMap and RhinoMocks in my stack I sometimes need to look for VB.Net examples of how to do things, because sometimes translating them from C# is not all that easy.

This article has helped me mocking events.

4/13/2008 11:05 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
After reading A post by Sergio Pereira I thought about translating his code to VB.Net because as usual all the cool things are written in C#.
So this piece
    using(StreamReader rd = File.OpenText("Data.txt"))  
{
string line = rd.ReadLine();
while(line != null)
{
DoSomething(line);
// do more stuff with the line text

//move on
line = rd.ReadLine();
}
}
Becomes

Dim rd As System.IO.StreamReader = System.IO.File.OpenText("Data.txt")
Using (rd)

Dim line As String = rd.ReadLine()
While (line IsNot Nothing)

dosomething(line)
' do more stuff with the line text

'move on
line = rd.ReadLine()
End While
End Using

and the next bit

    public static class FileUtil  
{
public static void EachLine(string fileName, Action<string> process)
{
using(StreamReader rd = File.OpenText(fileName))
{
string line = rd.ReadLine();
while(line != null)
{
process(line);
line = rd.ReadLine();
}
}
}
}

will translate in

Public Class FileUtil

Public Shared Sub EachLine(ByVal fileName As String, ByVal process As Action(Of String))
Dim rd As System.IO.StreamReader = System.IO.File.OpenText(fileName)
Using (rd)

Dim Line As String = rd.ReadLine()
While (Line IsNot Nothing)

process(Line)
Line = rd.ReadLine()
End While
End Using
End Sub
End Class

And we call that last piece of code like this

FileUtil.EachLine("Data.txt", AddressOf dosomething)

The doSomething method would look something like this

Private Sub dosomething(ByVal line As String)
        MessageBox.Show(line)
    End Sub


And that's it.



4/13/2008 9:59 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
A little example of how xlinq can make life eassier on us. And the pitfalls on how to get it to work.

I have have an xml file named Translation_En.xml.

Translation_En.xml

<?xml version="1.0" encoding="utf-8" ?>
<FormType>
  <Vaccation>Vaccation</Vaccation>
  <Mission>Mission</Mission>
  <Holiday>Holiday</Holiday>
</FormType>


And then the class that will get the data and save it. Don't forget to set references to system.core, system.data.linq, system.xml and system.xml.linq.
Imports System.Linq

Public Class Translation
Private _Vaccation As String
Private _Mission As String
Private _Holiday As String
Private _Language As String

Public Sub New(ByVal Language As String)
_language = Language
End Sub

Public Property Language() As String
Get
Return _Language
End Get
Set(ByVal value As String)
_Language = value
End Set
End Property

Public Property Vaccation() As String
Get
_Vaccation = GetTranslation("Vaccation")
Return _Vaccation
End Get
Set(ByVal value As String)
SaveTranslation("Vaccation",value)
_Vaccation = value
End Set
End Property

Public Property Mission() As String
Get
_Mission = GetTranslation("Mission")
Return _Mission
End Get
Set(ByVal value As String)
SaveTranslation("Mission",value)
 _Mission = value
End Set
End Property

Public Property Holiday() As String
Get
_Holiday = GetTranslation("Holiday")
Return _Holiday
End Get
Set(ByVal value As String)
SaveTranslation("
Holiday",value)
_Holiday = value
End Set
End Property

Private Function GetTranslation(ByVal Element As String) As String
Dim _return As String = Element
Try
Dim _File As XElement = XElement.Load(GetFilename)
Dim _Element As IEnumerable(Of XElement) = From c In _File.Elements(Element) Select c
_return = _Element.Value.ToString
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return _return
End Function

    Private Sub SaveTranslation(ByVal Element As String, ByVal value As String)
Dim _File As XElement = Nothing Dim _Element As IEnumerable(Of XElement) = Nothing Try _File = XElement.Load(GetFilename)
_Element = From c In _File.Elements(Element.ToString) Select c
_Element(0).SetValue(value)
_File.Save(GetFilename)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try End Sub
Private Function
GetFilename() As String
Return "Translation_En.xml"
End Function

 End Class
And now let me explain a bit further.
First of all this class has getters and setters but that is not important ;-)

The most iportant bit is the getranslation function.

this line gets the data from the file and puts it in an Xelement object
Dim _File As XElement = XElement.Load(GetFilename)

The next line takes out the Element we want. We don't need to specify the root element(a misstake I made the first time around.)

Dim _Element As IEnumerable(Of XElement) = From c In _File.Elements(Element) Select c

The last line just takes the value of the element and puts it in a string value.
_return = _Element.Value.ToString

So that is how we get data out of one element.

Now we want to update the file and that's what savetranslation is for, it is so easy to understand if you name your methods correctly.

Again we load the file and get the correct element

_File = XElement.Load(GetFilename)
_Element = From c In _File.Elements(Element.ToString) Select c

Then we set the first elements vale (since we only have one returned to us it has to be the first one).

_Element(0).SetValue(value)

And last but not least we save the file.

_File.Save(GetFilename)

Of course you could make this a lot better. And I will in a next episode.


.Net | linq
4/13/2008 8:36 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback