Database component: It consists of following files:
i) IDbCommand.cs
ii) AbstractCommand.cs
iii) MSSqlDbComponent.cs
Build the above files and create : mssqldbcomponent.dll
file name: IDbCommand.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace teacherone.mssqldbcomponent
{
interface IDbCommand
{
void Execute();
}
}
file name: AbstractCommand.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Xml;
namespace teacherone.mssqldbcomponent
{
public class AbstractCommand
{
private SqlConnection connection;
private SqlTransaction transaction;
protected string command;
protected string commandtype;
protected string result;
protected string connectionString;
public void setCommand(string pcommand, string pcommandtype)
{
command = pcommand;
commandtype = pcommandtype;
}
public void Flush()
{
command = null;
commandtype = null;
result = null;
}
public void Dispose()
{
command = null;
commandtype = null;
result = null;
connectionString = null;
if (transaction != null)
{
transaction.Dispose();
transaction = null;
}
if (connection != null)
{
connection.Close();
connection.Dispose();
connection = null;
}
}
public void Execute()
{
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
SqlCommand sqlcommand = new SqlCommand(command, connection);
sqlcommand.Transaction = transaction;
if (commandtype.Equals("INSERT") ||
commandtype.Equals("UPDATE") ||
commandtype.Equals("DELETE"))
{
result = "" + sqlcommand.ExecuteNonQuery();
}
else
{
XmlReader xmlreader = sqlcommand.ExecuteXmlReader();
if (xmlreader.Read())
{
result = xmlreader.ReadOuterXml();
}
else
{
result = null;
}
xmlreader.Close();
xmlreader = null;
sqlcommand = null;
}
command = string.Empty;
commandtype = string.Empty;
}
public void StartTransaction()
{
connection = new SqlConnection(connectionString);
connection.Open();
transaction = connection.BeginTransaction();
}
public void EndTransaction()
{
transaction.Commit();
transaction.Dispose();
}
public void RollBackTransaction()
{
transaction.Rollback();
transaction.Dispose();
}
}
}
file name: MSSqlDbComponent.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace teacherone.mssqldbcomponent
{
public class MSSqlDbComponent : AbstractCommand, IDbCommand
{
public MSSqlDbComponent()
{
}
public void SetConnectionString(string connstr)
{
connectionString = connstr;
}
public string GetResult()
{
return result;
}
}
}
Testing of database component
Create the table.
USE [SomeDatabaseDB]
GO
/****** Object: Table [dbo].[classified] Script Date: 01/01/2008 01:01:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[classified](
[classifiedid] [int] NULL,
[classifieddesc] [ntext] NULL,
[classifiedtype] [int] NULL,
[createdate] [datetime] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Client program to test the database component:
file name: TestClient.cs
While compiling TestClient.cs make reference to mssqldbcomponent.dll
(it was created using IDbCommand.cs, AbstractCommand.cs and MSSqlDbComponent.cs)
using System;
using System.Collections.Generic;
using System.Text;
using teacherone.mssqldbcomponent;
namespace TestClient
{
class Program
{
static void Main(string[] args)
{
string connectionstring =
@"Data source=computername\SQLEXPRESS;Initial Catalog=SomeDatabaseDB; Integrated Security=SSPI";
MSSqlDbComponent dbcomponent = new MSSqlDbComponent();
dbcomponent.SetConnectionString(connectionstring);
/// Demo: Inser command
string insertcommand = " insert into classified (classifiedid, classifieddesc, " +
" classifiedtype,classifiedtype. createdate) " +
" values " +
" (2,'test descr',2,'11/12/2008') " ;
dbcomponent.setCommand(insertcommand, "INSERT");
dbcomponent.Execute();
/// Demo: select command
string selectcommand = " select * from classified FOR XML AUTO, TYPE ";
dbcomponent.setCommand(selectcommand, "SELECT");
dbcomponent.Execute();
string result = dbcomponent.GetResult();
Console.WriteLine(result);
Console.WriteLine("Hit enter to continue...");
Console.Read();
}
}
}
|