Thursday, September 18, 2008

Isolation levels in SQL Server 2005

Read Uncommitted:
  • Least restrictive isolation level.
  • Transactions only ensure that they do not read physically corrupt data.
    Dirty reads, Nonrepeatable reads, Phantom reads are possible.

Read Committed with snapshot:
New to SQL Server 2005
When a transaction references rows that have been updated by another transaction, read operations retrieve the version of the row that existed when the snapshot transaction started.
Dirty reads are prevented.
Nonrepeatable reads, Phantom reads are possible.

Read Committed with locking:
Default isolation level used by SQL Server 2005.
Read operations cannot access data that other transactions are currently modifying.
Dirty reads are prevented.
Nonrepeatable reads, Phantom reads are possible.

Repeatable Read:
The first transaction locks all the rows that its reading so that other transactions cannot update the data.
Dirty reads, Nonrepeatable reads are prevented.
Phantom reads are possible.

Snapshot:
New to SQL Server 2005
Read operations retrieve the version of the row that existed when the snapshot transaction started as it does not acquire locks.
Dirty reads, Nonrepeatable, Phantom reads are prevented.

Serializable:

Most restrictive isolation level.
Transactions are completely isolated from each other. This is implemented by Database engines frequently by locking large numbers of rows or entire tables.
Dirty reads, Nonrepeatable, Phantom reads are prevented.


Saturday, September 6, 2008

How to use transaction in .Net

The following code gives an example of using local transaction in ado.net

using System.Transactions;

bool success = false;
SqlConnection conn = null;
SqlTransaction transaction = null;
try
{
conn= new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;);
conn.Open();
transaction = conn.BeginTransaction(System.Data.IsolationLevel.Serializable);
//Serializable isolation prevents dirty reads, phantom reads, non-repeatable reads by locking large numbers of rows or entire tables.

using (SqlCommand commanda = new SqlCommand(somesql1, conn, transaction))
{
int rowsUpdateda = commanda.ExecuteNonQuery();
if (rowsUpdateda > 0)
{
using (SqlCommand commandb = new SqlCommand(somesql2,conn, transaction))
{
int rowsUpdatedb = commandb.ExecuteNonQuery();
if (rowsUpdatedb > 0)
{
success = true;
}
}
}
}
}
finally
{
if (success)
transaction.Commit();
else
transaction.Rollback();
if (conn!= null)
connection.Close();
}


To use distributed transaction:

//A TransactionOptions object is created to specify the isolation level or timeout for the transaction
TransactionOptions options = new TransactionOptions();
options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
options.Timeout = new TimeSpan(0, 2, 0);

//A Transaction scope defines a block of code that participates in a local or distributed transaction
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
{
using (SqlConnection conna= new SqlConnection(connstring))
{
using (SqlCommand commanda = new SqlCommand(sqla, conna))
{
int rowsUpdateda = commanda.ExecuteNonQuery();
if (rowsUpdateda > 0)
{
using (SqlConnection connb= new SqlConnection(connb))
{
using (SqlCommand commandb = new SqlCommand(sqlb,connb))
{
int rowsUpdatedb = commandb.ExecuteNonQuery();
if (rowsUpdatedb > 0)
{
transactionScope.Complete(); }
}
commandb.Dispose ;// Dispose the second command object.
}
connb.Close;
// Dispose (close) the second connection.
}
} commandb.Dispose; // Dispose the first command object.
} connb.Close ;
// Dispose (close) the first connection.
} TransactionScope.Dispose; // Dispose TransactionScope object, to commit or rollback

Wednesday, September 3, 2008

Google's Browser Chrome beta version is out

Google has finally released it much awaited web browser Chrome beta version. I hope it doesn't crash like IE and is more stable.

It's highlighted features are :
1.Multiprocess based. Each tab has its own process, if one of the tab crashes it will not close the entire window like IE.
2. With it's task manager you can find out which website is using the most memory.
3.Plug-ins appear as a separate process in the task manager
4.Webkit is the open source rendering engine behind Chrome
5.Address bar offers suggestions for both search and top pages

For trying it out refer to http://www.google.com/chrome/index.html?hl=en&brand=CHMG&utm_source=en-hpp&utm_medium=hpp&utm_campaign=en

I am liking it.

To store connection string in application configuration file

Open up web.config file and inside the root element add the element.


connectionString="Data Source=(local); Initial Catalog=AdventureWorks; Integrated Security=SSPI; Persist Security Info=False" />

Connecting to different Data sources using ADO.Net

Connect to a SQLServer datasource

using System.Data.SqlClient

string connectionString = "Data Source=(local); Initial Catalog=Adventureworks; " + "Integrated Security=SSPI; Persist Security Info=False";

SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

using (connection)
{
// use connection.
}

Connect to an OLE DB Data Source
using System.Data.OleDb

string connectionString = "Provider=SQLOLEDB; Data Source=(local); Initial Catalog=Northwind; " + "Integrated Security=SSPI; Persist Security Info=False";

OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();

using (connection)
{
// use connection.

}

Connect to ODBC data source
using System.Data.Odbc

string connectionString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:\\nwind.mdb; " + "Trusted_Connection=yes";

OdbcConnection connection = new OdbcConnection(connectionString);
connection.Open();

using (connection)
{
//use connection

}

Connect to Oracle data source
using System.Data.OracleClient

string connectionString = " Data Source=MyOracleServer;Integrated Security=yes";

OracleConnection connection = new OracleConnection(connectionString);
connection.Open();

using (connection)
{
//use connection

.}


Note : If you don't want the data source to return security-sensitive information, i.e password, when the connection is open or has ever been in an open state, set persist Security Info to be false(default value).

.Net Framework 2.0 update not found. The win32manifest will not be embedded

Are you getting the following error after installing VS2008 on a vista machine:
.Net Framework 2.0 update not found. The win32manifest will not be embedded while opening a VB.Net Project or
Required file 'alink.dll with IAlink3' could not found while opening a C# project.

To fix the issue you need to install
NetFX2.0-KB110806-v60000-x86.msu from the installation disk of VS2008 . After that repair VS2008 and reboot your pc.
 
Google