Handling SMO SqlBackup Timeout

March 27, 2009

I hope I have finally tracked down a bug in automssqlbackup which caused an exception when creating backups of huge databases (DB ~40GB, backup file ~4GB): The SqlBackup operation simply timed out as the default command timeout is set to 10 minutes.

I found that there is a property called StatementTimeout in the ServerConnection class, so that you can set the timeout to infinity like so:

$conn = New-Object Microsoft.SqlServer.Management.Common.ServerConnection($dbhost)
$conn.StatementTimeout = 0
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server($conn)

$bk = New-Object Microsoft.SqlServer.Management.Smo.Backup
$bk.Action = [Microsoft.SqlServer.Management.Smo.BackupActionType]::Database
$bk.Database = $dbname
$bk.Initialize = $False
$bk.Incremental = -not $full
$bk.Devices.AddDevice($backupfile, Microsoft.SqlServer.Management.Smo.DeviceType]::File)
$bk.SqlBackup($srv)

SELECT from temp table in Stored Procedure with LINQ to SQL

March 26, 2009

I started with LINQ and MVC recently, and found the following restriction in LINQ to SQL:

Thus the LINQ to SQL code generator does not support the following:

Stored procedures that produce results based on temporary table.

While this statement is true for generated code, you can still manually work around the problem.

When you create a LINQ to SQL item, VS creates a .dbml and a .designer.cs file for the database connection specified. The .cs file contains the DataContext, which is declared as a public partial class, which means we can easily extend it.

Suppose we have a database called MyDB and a stored procedure MyProc which collects data in a temporary table and returns this data using a SELECT * FROM #Temp.

To declare the #Temp record in C#, we need to create a separate .cs file (e.g. MyDBExt.cs).

Start with a C# class mapping to the #Temp table (taken from one of my projects):

[Table]
public partial class MyProc_Result
{
    public MyProc_Result()    {   }

    [Column]
    public int User_OID;
    [Column]
    public string User_ID;
    [Column]
    public int ItemCount;
    [Column]
    public string Description;
}

What’s important here is that the class has the [Table] attribute, and each field has a [Column] attribute from the System.Data.Linq.Mapping namespace. No getters and setters necessary, as in the generated class file.

Next, extend the partial class from the original .designer.cs file and create an additional method for your SP:

[Function(Name = "dbo.MyProc")]
public IEnumerable<MyProc_Result> MyProcResultset( 
    .. copy parameters from original declaration .. )
{
    IExecuteResult result = this.ExecuteMethodCall(this, 
        ((MethodInfo)(MethodInfo.GetCurrentMethod())), ..params..);
    return result.ReturnValue as IEnumerable<MyProc_Result>;
}

Since overloaded methods are distinguished by the types of their parameters, and not by their return type, you have to change the method name for your extension. (Optionally, you can derive your own class and override the original method)


C# T-SQL Stored Procedure Wrapper with dbscript

March 25, 2009

As my article on generating a C# wrapper for T-SQL Stored Procedures has recently become one of the most popular posts, I thought of providing an implementation within dbscript.

From the various different possibilities (generate from a Project Versions’ schema information, define an XSL on the Project Version’s XML) I chose the simplest solution was to create two Code Snippets.

The Code Snippet “Stored Procedure Wrapper (2000)” queries sysobjects, syscolumns and systypes, as outlined in the post.

The second Code Snippet, “Stored Procedure Wrapper”, queries the INFORMATION_SCHEMA views ROUTINES and PARAMETERS to achieve the same goal.

The generated C# code can be copied directly from the browser window into Visual Studio.

The Code Snippets can be downloaded here along with version 0.95 of dbscript, and will be included in future versions.


New Features in dbscript 0.95

March 23, 2009

The latest version 0.95 of dbscript adds new functionality:

Data Diagram generation

dbscript 0.95 adds the capability to generate Data Diagrams from imported and uploaded SQL Server database schemas.

This allows you to get a quick overview of a database schema. It is also useful if you plan to manually create a data diagram in Management Studio, and need some visual help of how to layout the diagram.

ScrewTurn Wiki Page Provider

In addition to the ScrewTurn documentation generator introduced in dbscript 0.94, the latest version includes a Page Provider which you add in the Administration panel of your ScrewTurnWiki installation.

Configure the Documentation Generator plugin with the same ConnectionString you use in your dbscript web application.

Next, create a Documentation Generator of type ScrewTurn Page Provider, and define its contents. After you generate, ScrewTurn Wiki displays the selected Project Version information. (You may need to restart the ScrewTurn web application)

Summary of Output generated by dbscript

This is a list of output samples generated by dbscript based on MS AdventureWorks sample database:

Value Scripts

C# const int declarations of a dbscript Table

VB.Net Const Integer declarations of a dbscript Table

Project Version / Database Schema

Data Diagram of MS AdventureWorks database

Documentation Generators

MediaWiki Documentation of MS AdventureWorks database

Single Object documentation in MediaWiki format

Single HTML File documentation of MS AdventureWorks

ScrewTurn Wiki Documentation of MS AdventureWorks database

ScrewTurn Wiki Page Provider Documentation of MS AdventureWorks database

The latest version of dbscript is available for download here.


Sending PowerShell Transcript by Mail

March 22, 2009

PowerShell provides the Start-Transcript and Stop-Transcript commandlets to record logging information in a log file.

Creating the log file

The simplest way to create a log file based on the current date is like this:

$now = Get-Date
$logfile = "c:\path\to\log\dir\file-" + $now.ToString("yyyy-MM-dd") + ".log"
Start-Transcript -path $logfile -force

Any output in the PowerShell console will now also be copied to the logfile.

To end logging, simply use

Stop-Transcript

Sending plain text email

After the log file has been closed, we parse it line-by-line, and append each line to a StringBuilder. Finally, the string contents of the StringBuilder is passed to an SmtpClient object to be sent:

$log = Get-Content $logfile

$body = New-Object System.Text.StringBuilder
foreach($line in $log)
{
    [void] $body.AppendLine($line.ToString())
}

$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body.ToString())

Sending HTML email

Depending on the email client you use, mails containing plain text log files need not necessarily be displayed with a fixed-width font. To force monospace fonts, we need to enclose the text inside a <pre> tag within HTML, and explicitly create an HTML message object:

$body = New-Object System.Text.StringBuilder
[void] $body.AppendLine("<pre>");
foreach($line in $log)
{
    [void] $body.AppendLine($line.ToString())
}
[void] $body.AppendLine("</pre>");

$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $body.ToString())
$msg.IsBodyHTML = $true
$smtp.Send($msg)

Of course, the variables $emailFrom, $emailTo, $subject, and $smtpServer have to be defined according to your needs.


Generating Data Diagrams with dbscript

March 21, 2009

New Screenshots

I uploaded a set of new screenshots of the upcoming version 0.95 of dbscript.

Data Diagram

dbscript 0.95 adds the capability to generate Data Diagrams from imported and uploaded SQL Server database schemas:

AdventureWorks data diagram generated by dbscript

AdventureWorks data diagram generated by dbscript

This diagram gives a quick overview of tables and their relations, which makes it easier to create a full data diagram with Management Studio (or Enterprise Manager).

The latest version of dbscript is available for download here.


Forum open now

March 11, 2009

I set up a forum for all of my software that I describe in this blog:

Please feel free to register and post.


Generate ScrewTurn Documentation of MS SQL Database and Values

March 5, 2009

In addition to creating MediaWiki content, dbscript 0.94 now also supports ScrewTurn wikis to generate database documentation.

The steps to setup the documentation generator are essentially the same as described here and here for MediaWiki wikis, except that the respective ScrewTurn alternatives (generator type, XSLs) have to be selected.

To allow the built-in bot to post content to the ScrewTurn wiki, the wiki’s web.config has to be modified to disable viewstate validation:

<pages enableViewState="true" enableEventValidation="false" />

Follow this link to browse the ScrewTurn documentation of AdventureWorks as generated by dbscript.

The latest version of dbscript is available for download here.


New Features in dbscript 0.94

March 4, 2009

The latest version 0.94 of dbscript adds new functionality:

Multi-user capability

A user/role model has been implemented which allows the following definitions:

  • System administrator roles
  • Project-specific roles (project administrator, project user)
  • User/Role assignments

Each user can be assigned to several roles. A role is either a sysadmin role, or assigned to one or more projects. Within a project, a role is defined as project administrator or project user role. Users inherit sysadmin, project admin, and project user capability from their assigned roles.

Documentation Generators

New documentation generators create documentation as

Value Scripts

Adding to existing functionality creating C# const declarations:

  • C# public static const classes are now declared partial
  • new C# public static class with string ToString(int) to convert an int identifier into its string representation
  • VB.Net public const declarations
  • define optional WHERE condition on generated values

Database Schema Checks

Added the following checks on database tables:

  • Tables without Primary Key
  • Tables without Unique Constraint
  • Tables without Foreign Key Constraints
  • Unreferences Tables
  • Isolated Tables

The latest version of dbscript is available for download here.


Setting PowerShell window width

March 4, 2009

Start-Transcript is a PowerShell commandlet which causes PowerShell to log all output to a file. It uses the current window size (i.e. column count) to calculate line breaks in the generated file.

The PowerShell window is 120 characters wide by default. Thus a log file generated by Start-Transcript will reflect this 120 character limit.

You can even change the PowerShell window size using the Get-Host commandlet:

$h = get-host
$win = $h.ui.rawui.windowsize
$win.width  = 120  # change to preferred width
$h.ui.rawui.set_windowsize($win)

However, if PowerShell is invoked from a batch file being executed in cmd, the PowerShell window is only 80 characters wide, since the cmd window is (typically) 80 characters. Calling set_WindowSize will have no effect (apart from an error message), because the PowerShell window cannot have a greater size than the parent window.

To adjust the cmd window width before executing a PowerShell script, use the good old MODE command from DOS era:

mode con cols=120
powershell path-to\myscript.ps1

PowerShell will now inherit the desired window width, and line breaks in the log occur where you expect them from working in the PowerShell window.


Follow

Get every new post delivered to your Inbox.