So, like any good programmer, I have the camel on my bookshelf where the allmighty Larry asserts (p. 609) that laziness is the first great virtue of a programmer. Not to be a virtue-less programmer, I was off the teh googlez to search for an EXIF library.
I figured that it's bound to have come up before and lo and behold I found many a library. Most did pretty much what I wanted, but this is for a web app and every single library left a lock on the file after it was done executing. I guess I could have restarted the app pool after every page load, but that seemed a bit excessive.
I poked around MSDN and saw that it wasn't that difficult to read it by myself.
So, I wrote my own chunk of code:
b = new System.Drawing.Bitmap(fileInfo.FullName)
Dim encoding As New System.Text.ASCIIEncoding()
for each pi in b.PropertyItems
if pi.ID = 40091 then ' I only need the title
description = System.Text.Encoding.Unicode.GetString(pi.value)
exit for
end if
Next
and viola, EXIF data.
Sidebar here: I am a c# guy. I cannot stand using vb. but, DotNetNuke is written in vb so, I don't have much of a choice (I'm not rolling a new class for 15 lines of code)
But, with the EXIF data came the aforementioned file lock.
Back to MSDN to poke around at the Image Class. Skip down a bit and lookie lookie it implements IDisposible. Oh boy, is it really this easy?
yes, in fact it is.
using b = new System.Drawing.Bitmap(fileInfo.FullName)
Dim encoding As New System.Text.ASCIIEncoding()
for each pi in b.PropertyItems
if pi.ID = 40091 then
description = System.Text.Encoding.Unicode.GetString(pi.value)
exit for
end if
Next
End Using
Game, set, match.
Rob Bittner's Programming blog. I'm not sure how this is going to evolve, but I'm going to make every effort to keep it rolling..
Friday, August 17, 2012
Friday, February 17, 2012
Backup Script (2.0)
So, as promised, I am posting my new backupscript. I've completely reworked it. You know call it with a specific database, backup type (log, full, or differential), and a backup path.
Here's the basic logic:
If we try to backup tempdb, bail (You can't back up tempdb)
If you try to backup master with differential or log, bail
If you try to take a log backup of a non full recovery db, bail
(I dont have any bulk logged DBs, but I probably should change this)
Get the DB Guid (This was for databases that I restored with the same name, but were different databases altogether. I using the Database Name to query the history. the GUID is much more robust.
Force the BackupPathRoot to have a trailing \ - So the path works properly.
Get the last full backup information
If there was never a full backup, switch the backup type to full
If the last full is not in the backup path root, or is missing, switch to full (I'm thinking about changing this to only happen if we're taking a differential)
If we're taking a log backup, Check the log chain (just need to check from the last full or the last differential)
In the backup path root, make a subfolder for the database if it doesn't exist.
Backup the database with the following format: %BackupPathRoot%\%ServerName%\%DatabaseName%\%DatabaseName% YYYYMMDD HHMMSS %BackupType%.bak
so, there ya go...
here's the link : BackupScript
Here's the source:
Here's the basic logic:
If we try to backup tempdb, bail (You can't back up tempdb)
If you try to backup master with differential or log, bail
If you try to take a log backup of a non full recovery db, bail
(I dont have any bulk logged DBs, but I probably should change this)
Get the DB Guid (This was for databases that I restored with the same name, but were different databases altogether. I
Force the BackupPathRoot to have a trailing \ - So the path works properly.
Get the last full backup information
If there was never a full backup, switch the backup type to full
If the last full is not in the backup path root, or is missing, switch to full (I'm thinking about changing this to only happen if we're taking a differential)
If we're taking a log backup, Check the log chain (just need to check from the last full or the last differential)
In the backup path root, make a subfolder for the database if it doesn't exist.
Backup the database with the following format: %BackupPathRoot%\%ServerName%\%DatabaseName%\%DatabaseName% YYYYMMDD HHMMSS %BackupType%.bak
so, there ya go...
here's the link : BackupScript
Here's the source:
USE AdministrationIF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME='BackupDatabase')
DROP PROC BackupDatabase
GOCREATE PROC BackupDatabase
@database SYSNAME,
@backuptype VARCHAR(20),
@backupPathRoot VARCHAR(255) WITH ENCRYPTION
AS/*
--Test Data
declare
@database sysname,
@backuptype varchar(20),
@backupPathRoot varchar(255)
set @database = 'robtest'
set @backuptype = 'log'
set @backupPathRoot='C:\Backup'
--*/SET NOCOUNT ON
DECLARE
@databaseGuid UNIQUEIDENTIFIER,
@debugmessages INT,
@DifferentialBackupLocation NVARCHAR(260),
@DifferentialBackupSetId INT,
@FileExists INT,
@filename VARCHAR(255),
@folder VARCHAR(255),
@FullBackupLocation NVARCHAR(260),
@FullBackupSetId INT,
@now DATETIME,
@LogMessage VARCHAR(MAX),
@BackupLogID INT
--select @BackupLogID=MAX(id) from BackupLog -- Just for TestingSET @debugmessages = 0-- Dont Back up TempDBIF (@database = 'TempDB') RETURN-- Only Full backups for MasterIF (@database = 'Master' AND @backuptype <> 'Full') RETURN-- Don't take Log backups when the database isn't in full recoveryIF (@backuptype = 'Log' AND EXISTS (SELECT 1 FROM sys.databases WHERE recovery_model_desc <> 'FULL' AND name=@database))
RETURN-- Get the Database GuidSELECT @databaseGuid = database_guid FROM sys.database_recovery_status WHERE database_id = DB_ID(@database)-- Make sure the @backupPathRoot has a trailing \IF (@backupPathRoot NOT LIKE '%\') SET @backupPathRoot = @backupPathRoot + '\' -- Get the last full backup location and idSELECT @FullBackupLocation=physical_device_name, @FullBackupSetId=backup_set_idFROM msdb.dbo.backupmediafamily fJOIN msdb.dbo.backupset s ON f.media_set_id=s.media_set_idWHERE backup_set_id=(
SELECT MAX(msdb.dbo.backupset.backup_set_id)
FROM msdb.dbo.backupset
WHERE msdb.dbo.backupset.TYPE = 'D'
AND database_name=@database
AND database_guid=@databaseGuid )
IF (@FullBackupLocation IS NULL) BEGIN SET @backuptype='Full' EXEC BackupLogEntry 'BackupDatabase', @database, 'No Full backup exists' END
ELSE IF ( @FullBackupLocation NOT LIKE @backupPathRoot + '%') BEGIN SET @backuptype='Full' EXEC BackupLogEntry 'BackupDatabase', @database, 'Full backup not in the correct folder' END
ELSE BEGIN
EXEC MASTER..xp_fileexist @FullBackupLocation, @FileExists output
IF (@FileExists = 0) BEGIN
SET @backuptype='Full'
SET @LogMessage = 'Full Backup Location (' + @FullBackupLocation + ') missing from the folder'
EXEC BackupLogEntry 'BackupDatabase', @database, @LogMessage
END ELSE BEGIN
IF (@debugmessages=1) EXEC BackupLogEntry 'BackupDatabase', @database, 'Full backup exists'
END
END
-- Check to make sure the files are in the correct locationIF (@backuptype = 'Log') BEGIN
--Get differential backup location
SELECT @DifferentialBackupLocation=physical_device_name, @DifferentialBackupSetId=backup_set_id
FROM msdb.dbo.backupmediafamily f
JOIN msdb.dbo.backupset s ON f.media_set_id=s.media_set_id
WHERE backup_set_id=(
SELECT MAX(msdb.dbo.backupset.backup_set_id)
FROM msdb.dbo.backupset
WHERE msdb.dbo.backupset.TYPE = 'I'
AND backup_set_id > @FullBackupSetId
AND database_name=@database
AND database_guid=@databaseGuid
)
-- Check differential backup location
IF (@DifferentialBackupLocation IS NOT NULL) BEGIN
IF (@debugmessages=1) BEGIN SET @LogMessage = 'Checking Differential Backup Location (' + @DifferentialBackupLocation + ')' EXEC BackupLogEntry 'BackupDatabase', @database, @LogMessage END
IF ( @DifferentialBackupLocation NOT LIKE @backupPathRoot + '%' ) BEGIN
SET @LogMessage = 'Differential Backup (' + @DifferentialBackupLocation + ') in wrong folder'
EXEC BackupLogEntry 'BackupDatabase', @database, @LogMessage
SET @DifferentialBackupLocation = NULL
END
ELSE BEGIN
EXEC MASTER..xp_fileexist @DifferentialBackupLocation, @FileExists output
IF (@FileExists = 0) BEGIN
SET @LogMessage = 'Differential Backup (' + @DifferentialBackupLocation + ') missing'
EXEC BackupLogEntry 'BackupDatabase', @database, @LogMessage
SET @DifferentialBackupsetId = NULL
END ELSE BEGIN
IF (@debugmessages=1) EXEC BackupLogEntry 'BackupDatabase', @database, 'Differential backup found'
END
END
END
IF (@debugmessages=1) EXEC BackupLogEntry 'BackupDatabase', @database, 'Checking Log Chain'
DECLARE cur CURSOR FOR
SELECT physical_device_name
FROM msdb.dbo.backupmediafamily f
JOIN msdb.dbo.backupset s ON f.media_set_id=s.media_set_id
WHERE backup_set_id > COALESCE(@DifferentialBackupsetId, @fullbackupsetid)
AND database_name=@database
AND database_guid=@databaseGuid
AND TYPE = 'L'
OPEN cur
FETCH next FROM cur INTO @filename
WHILE @backuptype='log' AND @@FETCH_STATUS=0 BEGIN
IF (@debugmessages=1) BEGIN SET @LogMessage = 'Checking Log Backup Location (' + @filename + ')' EXEC BackupLogEntry 'BackupDatabase', @database, @LogMessage END
IF (@filename NOT LIKE @backupPathRoot + '%') BEGIN
SET @backuptype = ''
SET @LogMessage = 'Differential Backup (' + @filename + ') in wrong folder'
EXEC BackupLogEntry 'BackupDatabase', @database, @LogMessage
END ELSE BEGIN
EXEC MASTER..xp_fileexist @filename, @FileExists output
IF (@FileExists = 0) BEGIN
SET @backuptype = ''
SET @LogMessage = 'Differential Backup (' + @filename + ') missing'
EXEC BackupLogEntry 'BackupDatabase', @database, @LogMessage
END
END
IF @backuptype = '' BEGIN
IF @fullbackupsetid IS NULL SET @backuptype='Full'
ELSE SET @backuptype='Differential'
END
FETCH next FROM cur INTO @filename
END
CLOSE cur
DEALLOCATE curEND
SET @now = GETDATE()SET @folder = @backupPathRoot + @@SERVERNAME + '\' + @database + '\'SET @folder = REPLACE(@folder,'/','')EXEC MASTER..xp_create_subdir @folderSET @filename = @folder + @database + ' ' + REPLACE(CONVERT(VARCHAR,@now,121),':','-') + ' ' + @backuptype + '.bak'SET @filename = REPLACE(@filename,'/','')
SET @LogMessage = 'Taking a ' + @BackupType + ' Backup, Filename: ' + @filenameEXEC BackupLogEntry 'BackupDatabase', @database, @LogMessage
IF @backuptype = 'Log'
BACKUP LOG @database TO DISK=@filename ELSE IF @backuptype = 'Differential'
BACKUP DATABASE @database TO DISK=@filename WITH differentialELSE IF @backuptype = 'Full'
BACKUP DATABASE @database TO DISK=@filename GO--alter database robtest set recovery full
--BackupDatabase 'RobTest','Differential','C:\Backup'
Wednesday, February 15, 2012
Backup Script - Needs to be updated
Ack, It's been a while since I've updated my SQL backup script. I've completely reworked it. I'm in class this week, but will post it early next week.
Friday, January 27, 2012
Integrate Resharper Unit Test Runner after a successful build
Whilst trying to use TDD properly, I noticed that I'd have to build my solution and then run my unit tests. Now, most of the unit test runners will build your project at the same time, but it still was a little less convenient than I'd like.
I scraped together a few snippets from teh interwebs and came up with the following to be added to the VS Macros: Pop open the Macros IDE (Tools - Macros - Macros IDE) and in the Project Explorer choose the Environment Events Module. Then Add the following code:
Private SolutionBuildSuccess As Boolean
Private Sub BuildEvents_OnBuildBegin( _
ByVal Scope As vsBuildScope, _
ByVal Action As vsBuildAction _
) Handles BuildEvents.OnBuildBegin
SolutionBuildSuccess = True
End Sub
Private Sub BuildEvents_OnBuildProjConfigDone( _
ByVal Project As String, _
ByVal ProjectConfig As String, _
ByVal Platform As String, _
ByVal SolutionConfig As String, _
ByVal Success As Boolean _
) Handles BuildEvents.OnBuildProjConfigDone
If Success = False Then
SolutionBuildSuccess = False
End If
End Sub
Private Sub BuildEvents_OnBuildDone( _
ByVal Scope As EnvDTE.vsBuildScope, _
ByVal Action As EnvDTE.vsBuildAction _
) Handles BuildEvents.OnBuildDone
If SolutionBuildSuccess Then
Dim win As Window
win = DTE.ActiveWindow
DTE.ExecuteCommand("ReSharper.ReSharper_UnitTest_RunSolution")
win.Activate()
End If
End Sub
Going through each of these:
SolutionBuildSuccess: This is just a boolean that will be true if every project builds. No sense in even running tests if you can't even compile
BuildEvent_OnBuildBegin: This just resets the SolutionBuildSuccess at the begin of each build
BuildEvents_OnProjConfigDone: So, there are 2 events when a build finishes. One fires when the complete build process is done (OnBuildDone). This one, however, does not carry the result of the project build(s). So, there is OnProjConfigDone which fires for each project that builds and has the result. This will just set the SolutionBuildSuccess to false if any of the projects fail to build.
BuildEvents_OnBuildDone: This one fires when the build is all done. So, I need to check to see if all the projects built successfully. Then I save the currently active window. Finally we run the tests and make that previously saved active window active again. The reason for that is that after I'm done building and running the tests, generally I'm ready to code more. So, instead of remembering that I have to smash ctrl-tab (I think) to jump back, I took the lazy approach and put it into the step.
Now, I remember back in the old days when F6 was the command to build stuff. I'm not sure what one of my tomfooleries changed it, but it did. So, I re-assigned it back to being my keyboard shortcut for build.
Any Comments or suggestions will probably be ignored, but feel free to leave them anyway..
I scraped together a few snippets from teh interwebs and came up with the following to be added to the VS Macros: Pop open the Macros IDE (Tools - Macros - Macros IDE) and in the Project Explorer choose the Environment Events Module. Then Add the following code:
Private SolutionBuildSuccess As Boolean
Private Sub BuildEvents_OnBuildBegin( _
ByVal Scope As vsBuildScope, _
ByVal Action As vsBuildAction _
) Handles BuildEvents.OnBuildBegin
SolutionBuildSuccess = True
End Sub
Private Sub BuildEvents_OnBuildProjConfigDone( _
ByVal Project As String, _
ByVal ProjectConfig As String, _
ByVal Platform As String, _
ByVal SolutionConfig As String, _
ByVal Success As Boolean _
) Handles BuildEvents.OnBuildProjConfigDone
If Success = False Then
SolutionBuildSuccess = False
End If
End Sub
Private Sub BuildEvents_OnBuildDone( _
ByVal Scope As EnvDTE.vsBuildScope, _
ByVal Action As EnvDTE.vsBuildAction _
) Handles BuildEvents.OnBuildDone
If SolutionBuildSuccess Then
Dim win As Window
win = DTE.ActiveWindow
DTE.ExecuteCommand("ReSharper.ReSharper_UnitTest_RunSolution")
win.Activate()
End If
End Sub
Going through each of these:
SolutionBuildSuccess: This is just a boolean that will be true if every project builds. No sense in even running tests if you can't even compile
BuildEvent_OnBuildBegin: This just resets the SolutionBuildSuccess at the begin of each build
BuildEvents_OnProjConfigDone: So, there are 2 events when a build finishes. One fires when the complete build process is done (OnBuildDone). This one, however, does not carry the result of the project build(s). So, there is OnProjConfigDone which fires for each project that builds and has the result. This will just set the SolutionBuildSuccess to false if any of the projects fail to build.
BuildEvents_OnBuildDone: This one fires when the build is all done. So, I need to check to see if all the projects built successfully. Then I save the currently active window. Finally we run the tests and make that previously saved active window active again. The reason for that is that after I'm done building and running the tests, generally I'm ready to code more. So, instead of remembering that I have to smash ctrl-tab (I think) to jump back, I took the lazy approach and put it into the step.
Now, I remember back in the old days when F6 was the command to build stuff. I'm not sure what one of my tomfooleries changed it, but it did. So, I re-assigned it back to being my keyboard shortcut for build.
Any Comments or suggestions will probably be ignored, but feel free to leave them anyway..
Labels:
Programming,
Resharper,
Visual Studio
Location:
500 W. Big Beaver Rd, Troy, MI 48084
Subscribe to:
Posts (Atom)