Wednesday, July 12, 2006

SQL Server Transactions - Level - beginner

Taken from SQL Server help pages

Transactions

A transaction is a sequence of operations performed as a single logical unit of work. A logical unit of work must exhibit four properties, called the ACID (Atomicity, Consistency, Isolation, and Durability) properties, to qualify as a transaction:

Atomicity

A transaction must be an atomic unit of work; either all of its data modifications are performed, or none of them is performed.

Consistency

When completed, a transaction must leave all data in a consistent state. In a relational database, all rules must be applied to the transaction's modifications to maintain all data integrity. All internal data structures, such as B-tree indexes or doubly-linked lists, must be correct at the end of the transaction.

Isolation

Modifications made by concurrent transactions must be isolated from the modifications made by any other concurrent transactions. A transaction either sees data in the state it was in before another concurrent transaction modified it, or it sees the data after the second transaction has completed, but it does not see an intermediate state. This is referred to as serializability because it results in the ability to reload the starting data and replay a series of transactions to end up with the data in the same state it was in after the original transactions were performed.

Durability

After a transaction has completed, its effects are permanently in place in the system. The modifications persist even in the event of a system failure.


Explicit Transactions

An explicit transaction is one in which you explicitly define both the start and end of the transaction. Explicit transactions were also called user-defined or user-specified transactions in earlier versions of Microsoft® SQL Server™.

DB-Library applications and Transact-SQL scripts use the BEGIN TRANSACTION, COMMIT TRANSACTION, COMMIT WORK, ROLLBACK TRANSACTION, or ROLLBACK WORK Transact-SQL statements to define explicit transactions.

BEGIN TRANSACTION

Marks the starting point of an explicit transaction for a connection.

COMMIT TRANSACTION or COMMIT WORK

Used to end a transaction successfully if no errors were encountered. All data modifications made in the transaction become a permanent part of the database. Resources held by the transaction are freed.

ROLLBACK TRANSACTION or ROLLBACK WORK

Used to erase a transaction in which errors are encountered. All data modified by the transaction is returned to the state it was in at the start of the transaction. Resources held by the transaction are freed.




Sunday, June 11, 2006

ASP.NET tips - Level - Intermediate

How many times you have created a Object and wished that you could access the Session variables, Request , response object.
ASP.NET make it possible with

HttpContext.Current

This return the context for every request.
with this you can access the Session, Request, Response object.

One more things this context have is the Items HashTable
You can insert the key, value pair at the beginning of the request ( e.g. in a page ) and access it later on ( e.g. in the business object)

Pros : very easy to handle. No more passing too many parameters
Cons : It is for ASP.NET only. So if the object is being used in Windows apps too, you can not use this feature.


e.g. I have a control that export to excel I have created the object ExportHelper

public static void Export(
Control ctlToRender,
string StrFileName)
{
// Set the Response object
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Buffer= true;
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", String.Format("inline;filename={0}.xls",StrFileName) );
response.Write(RenderControl(ctlToRender));
response.End();
}


public static string RenderControl(Control ctlToRender)
{
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
ctlToRender.RenderControl(oHtmlTextWriter);
return oStringWriter.ToString();
}

In the page you add a button and add this to eventHandler

private void BtnExportToExcel_Click(object sender, System.EventArgs e)
{
ExportHelper.Export(DGSearch, "MyExport");
}
Where DGSearch is the Datagrid you want to export to export.

You see how I am able to use the Response object in my Export Helper object