DataBase Access in dot net
“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:








Leave your response!