Introduction

Usage Guide

Sample code

Class Reference

Feedback

Project Homepage

Stitch.Database Sample Code

Sample select statement
The example below shows a SELECT based statement being dynamically created for SQL Server.

// Create connection to SQL Server
//  Server name: Localhost
//  Database name: Northwind

DBConnection dbConnection = new DBConnection(DatabaseType.MSSQL,
	"localhost",
	"username",
	"password",
	"Northwind");

// Query the database

DBResult dbResult = dbConnection.SelectQuery("SELECT * FROM 
					tblBooks WHERE 1");
DBRow dbRow = dbResult.FetchArray();

// Display the results

while (dbRow != null)
{	
	Console.WriteLine("Book: " + dbRow["BookName"]);
	Console.WriteLine("Author: " + dbRow["AuthorName"]);
	dbRow = dbResult.FetchArray();
}

Sample Insert statement
The example below shows an INSERT based statement being dynamically created for Microsoft Access.

// Create connection to Access file AuthorData.mdb

DBConnection dbConnection = new DBConnection(DatabaseType.Access,
	"AuthorData.mdb");

// Query the database

int LinesAffected = dbConnection.NonSelectQuery("INSERT INTO tblBooks 
	(Author, Title) 
	VALUES
	('Paul Stovell', 
	'The Big Book of Everything')");

Stored Procedures
As of Stitch.Database 1.1.0.0, Stored Procedures are supported for both Access and SQL Server. Parameters to procedures are passed using the DBParameter class.

// Create connection to SQL Server
//  Server name: Localhost
//  Database name: Northwind

DBConnection dbConnection = new DBConnection(DatabaseType.MSSQL,
	"localhost",
	"username",
	"password",
	"Northwind");

// Call stored procedure "spGetBooks"

string BookName = "The Big Book of Everything";
DBResult dbResult = dbConnection.StoredProcedure("spGetBooks",
        new DBParameter("@BookName",     // Parameter name
                SqlDbType.Char,          // Type
                BookName.Length,         // Size
                BookName));              // Value

DBRow dbRow = dbResult.FetchArray();

// Display the results

while (dbRow != null)
{	
	Console.WriteLine("Book: " + dbRow["BookName"]);
	Console.WriteLine("Author: " + dbRow["AuthorName"]);
	dbRow = dbResult.FetchArray();
}


Copyright (C) Paul Stovell, 2004. All rights reserved.
This product is subject to the terms and conditions detailed in the licence agreement.