C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




A Class for Database Access in C#


Posted Date: 13 Mar 2008    Resource Type: Code Snippets    Category: ADO.NET

Posted By: Dharmaraj       Member Level: Diamond
Rating:     Points: 5



The code sample below presents a class DataManager for database access. It contains features to execute queries (scalar and non-scalar). To do so, it connects to the database every time and disconnects as soon as the result is obtained.

The query to be executing is passed while instantiating the class. Execution is started by calling the ExecuteQuery() methods.



using System;
using System.Data;
using System.Data.SqlClient;

public class DataManager
{
public DataManager()
{
}

public static DataTable ExecuteQuery(string query)
{
string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

try
{
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataSet ds = new DataSet();
adapter.Fill(ds);

return ds.Tables[0];
}
finally
{
if ( connection.State == ConnectionState.Open )
connection.Close();
}
}


public static void ExecuteNonQuery(string query)
{
string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

try
{
SqlCommand cmd = new SqlCommand();
cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;

cmd.ExecuteNonQuery();
}
finally
{
if ( connection.State == ConnectionState.Open )
connection.Close();
}
}

public static object ExecuteScalar(string query)
{
string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

try
{
SqlCommand cmd = new SqlCommand();
cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;

return cmd.ExecuteScalar();
}
finally
{
if ( connection.State == ConnectionState.Open )
connection.Close();
}
}
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Executing Queries with ADO.NET  .  Database Access with ADO.NET  .  Database Access in C# with ADO.NET  .  Database Access in C#  .  Class for Database Access  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Mysql Connection Strings
Previous Resource: Query a SQL Server Database in C# with ADO.NET
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

teleconferencing service

Contact Us    Privacy Policy    Terms Of Use