Tuesday, March 25, 2008

Working with Stored procedures

Stored procedure helps to make your work easy. With the help of these you just have to supply some parameters only.

The Code snippet using storedprocedures describes a way to use stored procedures.

Working with Transactions

Whenever you have to update more than one table or destinations then it must have to sure that the operation done successfully because some time it has been seen that one table updated but due to some error another(s) not. To overcome this problem you have Transactions.

In ADO.Net transactions are initiated by calling BeginTransaction() methods on the database connection object.

Transaction isolation levels


Isolation Level(s)

Description

ReadCommitted

Its default for SQL Server. It ensures that data written by one transaction will only be accessible in a second transaction after the first transaction commits.

ReadUnCommitted

It permits transaction to read data within the database, even data that have not yet been committed by another transaction.

RepeatableRead

It extends the ReadCommitted level, ensures that if the same statement issued within the transaction, regardless of other potential updates made to the database, the same data will always be returned.

Serializable

It is the most exclusive transaction level, which in effect serializes access to data within the database. With this level, phantom rows can never show up, so a SQ statement issued within a serializable transaction will always retrieve the same data.

Bellow is the code snippet to show transaction in action:

string myConStr = "server=(local); integrated security=SSPI;database=HRnPAYROLL";

using (SqlConnection myCon = new SqlConnection(myConStr))
{
//Open connection object
myCon.Open();
SqlTransaction nTran = myCon.BeginTransaction();
//some code for work
nTran.Commit();
}

Monday, March 24, 2008

Difference between abstract classes and interfaces

Very basic question but important to point out.
  • Abstract classes can have concrete methods while interfaces have no methods implemented.
  • Interfaces do not come in inheriting chain, while abstract classes come in inheriting chain.
  • Interfaces have only pure virtual abstract method, while abstract classes may have non-abstract methods.
  • All members are public [by default] in interfaces but you have to declare public members in abstract classes.

How to locate names of all columns in a table?

I found the following query is very much worth-full when you are looking for names of all columns in a table

SELECT
(o.name) AS [Table Name],
o.type,
c.name AS [Col Name],
s.name AS [Col Type],
c.prec,
c.scale,
c.isnullable
FROM
dbo.sysobjects AS o
INNER JOIN
dbo.syscolumns AS c
ON
c.id = o.id
INNER JOIN
dbo.systypes AS s
ON
c.xtype = s.xtype
WHERE
(
o.type = 'U'
and o.name = 'TableName (for ex: PointProgramDetail)' -- Comment out this one for columns of all tables
)
order by c.name

Also, you can use sys.tables and sys.views for getting tables/views info from DB (new feature of SQL Server 2005). I have found that these 2 tables are helpful in getting info about tables or views in DB.

Testing a Private Assembly

Lets start to create a client application for privateassembly, Open a notepad and write following code [till now you have not created any client application using visual studio, you will do the same very soon in ASP.Net section]. For this example lets create a client application using Console application from templates pane [windows applications are beyond the scope of the book].

  • In Solution explorer change the class name from Program to mathClassClient
  • Using private assembly.

  • Add CSharp.AStepAhead.privateAssembly namespace
  • Now, important step is to add assembly reference, right click on the project
    in solution explorer and click on the Add Reference
    [you can choose the
    same from Project menu]

  • Adding Assembly Reference

  • Create an object of mathClass Class
  • Build the project or press F5
  • Important Important:Version information of an assembly is stored in its MANIFEST
    : Concept of Versioning is applicable only to GAC [Global Assembly Cache], because
    private assemblies are lying in their individual folders.

  • Output of Client application :
  • Output:Client Application
    Important Important:
    1. Revise source of above project [privateAssembly project] to
    understand the code. In this project, you saw that there is nothing extraordinary
    operations, just accepting integer values and on the behalf of these values result
    will retaining by overriding ToString() method. Yes, you can override this virtual
    method as per your requirement, as done in this example.
    2. Now, in Client application, this is a Console Based application
    project, as you are going for Asp.Net so, Windows applications are beyond the
    scope of the book
    .

Creating a Private Assembly

In Visual Studio .NET there is always an assembly whether you choose a class library
project or an exe project. Now, let's start some practical work.

  • Start Visual Studio 2005

  • Choose a new C# project [File -> New Project]

  • Choosing new C#:project
  • From template pane choose Class Library Template

  • Set the Location : F:\myWrittings\CSharpBook\Source Codes\

  • Name : privateAssembly

  • Check full code of private
    assembly
    .

  • In Solution explorer change class name from Class1 to mathclass

  • Now right click on mathclass and click on view code

  • Change the name of namespace from privateAssembly to CSharp.AStepAhead.privateAssemby

  • After writing codes of the assembly, now you have to provide some information to
    your assembly as follows:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; // General Information about an assembly is
controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("privateAssembly")]
[assembly: AssemblyDescription("This is an Example of Book: C#-A Step Ahead
Series")]
[assembly: AssemblyConfiguration("Free I.T. Education Series - A Step Ahead")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("A Step Ahead Series")]
[assembly: AssemblyCopyright("Copyright © A Step Ahead 2007")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d5389a36-3aa1-4e22-b3df-4cacd5e96531")]

  • For above, you have to click on AssemblyInfo.cs under properties from Solution Explorer
  • The above information tells about the assembly name, title its short description
    and company who had developed it.
  • For more detail check table assembly attributes.
  • Write codes to provide the working for client application.
  • Now build solution from Build -> Build Solution or Ctrl + Shift + B
  • If build succeeded, it means you have created an assembly
  • You can check the assembly using ILDASM

  • How to view an Assembly

    To view the assembly means to view the IL code, ILDASM converts the whole exe or
    dll into IL Code. To start the same:


    • Go to C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin and then double click
      on ildasm.exe

    • Open SDK command prompt and type ildasm


    • Invoking ILDASM


    • Now in ildasm open assembly File -> Open [or ctrl+O]



    • Opening ILDASM


    • Double click on the MANIFEST



    • Viewing Assembly MANIFEST


    • If you want to view mathclass, just double click on it



    • Viewing Class


    • For further information about the methods just do the same as you did in above case


    • Viewing Method





    What is MANIFEST


    This is a very important part of an assembly. It describes the full information
    of an assembly, so, in general words it contains metadata of an assembly. Manifest
    is a container of metadata of an assembly, which contains the followings:



    • Name, version info, culture info etc.

    • Identity of security

    • Scope of assembly

    • Resolve references to resources and classes.

    Site for beginners

    My another entry in teh Technical World is my recently designed site

    This is good for fresher. Visit here.

    I am Echinacea


    I am an
    Echinacea


    What Flower
    Are You?


    Last day, I have visited this site and got as 'I am Echinacea'.

    My First Post

    This is a newer one blog of mine.
    With the great response of my another blog and site I have decided to utilize some more hours to do this task.