Home » C# Codes, Tech Corner

DataBase Access in dot net

4 June 2009 No Comments Posted By:LG

“DBAccess” class(Download code)
                For making the sqlserver connection easier a user defined class “DBAccess.cs” is used in which all the database activities are done.The “GetTable( )” will help you execute a select query.This method returns a DataTable “dt” .Now there is another method called “ExecuteQuery( )“, which also takes as input a string,this method is used to execute insert and update queries.Eventhough both methods can be used interchangably i prefer this way,because the “ExecuteQuery( )” returns an integer value which will give us the idea of the number of rows affected.
                 Now there is one private method “GetConnection( )” inside the class,this is used to check the state of a connection,that is whether the created connection is open or close.This method returns the SqlConnection Object “Sqlcon“.Now inside the constructor of the class the connetcio nstring is created and this is how a connection between visual studio and Sql server is established.Server = dot implies the same machine acts as the server.Uid,pwd indicates the user id and password of the sqlserver simultaneously.And database gives the name of the database where our tables are created.In our example “test” is the database name.Now the namespaces imported varies according to situation.”System” is the root namespace.Namespace “Data” is used for getting structure of Datatable,dataset etc.

Related posts:

  1. TreeView Navigation Control in VisualStudio2008

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>