March 17, 2003

The DataTable’s Compute method

The DataTable class has a method, Compute, that executes SQL-like functions on the rows locally stored in the DataTable. It supports functions such as COUNT, SUM, MIN, MAX, AVG and others. Here’s an example to calculate the average salary for the employees stored in the tableEmployees DataTable: Dim maxSalary As

BackupDatabase – Backing-up a SQL Server database

‘ Backup the specified database.’ Note: requires Imports System.Data.SqlClient” Example:’ Dim connString As String = “server=(local); user id=sa; password=; ‘ Database=master;”‘ Try’ BackupDatabase(connString, “MyTestDB”, “D:MyTestDbBackup.bak”)’ Catch ex As Exception’ MessageBox.Show(ex.Message)’ End TrySub BackupDatabase(ByVal connString As String, ByVal dbName As String, _ ByVal backupFile As String) Dim cn As New SqlConnection(connString)

CreateConnString – Using the OLEDB dialog to create an OLEDB connection string

‘ This function opens the OLEDB dialog to create an OLEDB connection string,’ and return the selection’ It requires the OLEDB Service Component 1.0 Type Library to workPublic Function CreateConnString(Optional ByVal parentForm As Form = Nothing) _ As String Dim dataLink As Object = Microsoft.VisualBasic.Interaction.CreateObject _ (“DataLinks”) ‘On Error Resume

RestoreDatabase – Restoring a SQL Server database

‘ Restore the specified database.’ Note: requires Imports System.Data.SqlClient” Example:’ Dim connString As String = “server=(local); user id=sa; password=; ‘ Database=master;”‘ Try’ RestoreDatabase(connString, “MyTestDB”, “D:MyTestDbBackup.bak”)’ Catch ex As Exception’ MessageBox.Show(ex.Message)’ End TrySub RestoreDatabase(ByVal connString As String, ByVal dbName As String, _ ByVal backupFile As String) Dim cn As New SqlConnection(connString)

CreateDatabase – Creating a SQL Server database

‘ Create a SQL Server database with the specified name.’ If the second parameter is True and a DB with the same name is already ‘ present,’ it is dropped before the new DB is created.’ Note: requires Imports System.Data.SqlClient” Example:’ Dim connString As String = “server=(local); user id=sa; password=;

Calculated columns that refer to relationships

The DataSet is a container of multiple DataTables, and it allows to create parent-child relationships between two tables, as shown below: ‘ create a relationship between the Categories and the Products tables,’ against the CatID columnds.Relations.Add(New DataRelation(“CatProducts”, _ ds.Tables(“Categories”).Columns(“CatID”), ds.Tables(“Products”).Columns _ (“CatID”))) This is nice, but even nicer is the