Daily Archives: March 20, 2014

Exception calling "SqlBackup" with "1" argument(s)

We use the PowerShell script below to backup our SQL Server databases:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $dbInstance
$backup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup")
$backup.Action = "Database"
$backup.BackupSetDescription = "Full backup of " + $dbName
$backup.BackupSetName = $dbName + " backup"
$backup.Database = $dbName
$backup.MediaDescription = "Disk"
$backup.Devices.AddDevice("$localSqlBackupPath", "File")
$backup.SqlBackup($server)

This script runs fine for years, however recently it started to fail. It successfully backed up most databases, however the backup occasionally failed on some other databases which were previously backed up successfully. I found this error in the log:

Exception calling "SqlBackup" with "1" argument(s): 
"Backup failed for Server 'MyServer\MySqlInstance'. " At D:\Backups\BackupSite.ps1:151 char:22 + $backup.SqlBackup <<<< ($server) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

After a long investigation it turned out, that the error has nothing to do with how we call the SqlBackup function, instead the real issue is that it timeouts after 10 minutes. I turned off the timeout monitoring via the StatementTimeout property of the ServerConnection object and error is gone:

$server.ConnectionContext.StatementTimeout = 0

 

Technorati-címkék: ,

Grouping files in Solution Explorer

A nice feature of Visual Studio Web Essentials is that it can process your file right after you saved it. For example it can compile your TypeScript file to JavaScript, and even create the map file for it. Or it can generate the CSS from your LESS file including the minified version.

This feature is really useful, if this kind of transformation is not part of your build process, and in this case it is very logical that the generated files are added to the project file. Unfortunately Visual Studio does not always recognize the connection between these files, and does not group them in the Solution Explorer window. If you are brave enough, you can edit the .csproj file and force the Solution Explorer window to show the connecting files in a nested hierarchy. The trick is the DependentUpon element, for example this code:

<TypeScriptCompile Include="js\Main.ts" />
...
<Content Include="js\Main.js">
    <DependentUpon>Main.ts</DependentUpon>
</Content>
<Content Include="js\Main.js.map">
    <DependentUpon>Main.ts</DependentUpon>
</Content>

results this:

solution-explorer-group-ts

You can even create a deeper hierarchy:

<None Include="css\default.less" /> 
... 
<Content Include="css\default.css"> 
    <DependentUpon>default.less</DependentUpon> 
</Content> 
<Content Include="css\default.min.css"> 
    <DependentUpon>default.css</DependentUpon> 
</Content>

The above code results this display in the Solution Explorer:

solution-explorer-group-less

 

Technorati-címkék: