.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

SQL Helper Class

Posted By:Athira Sreejith       Posted Date: December 31, 2008    Points: 15    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Here am giving SQL Helper.In my project am accessing Connection as well as all methods from here.Please go through this..


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace VetCareWeb
{
public class VetCareLeaveHelper
{
private string strConnectionString = string.Empty;

public VetCareLeaveHelper()
{
strConnectionString = ConfigurationManager.AppSettings["VetcareLeave"].ToString();
}

public object ExecuteScalar(string query)
{
SqlConnection cnn = new SqlConnection(strConnectionString);
SqlCommand cmd = new SqlCommand(query, cnn);
if (query.StartsWith("SELECT") | query.StartsWith("select"))
{
cmd.CommandType = CommandType.Text;
}
else
{
cmd.CommandType = CommandType.StoredProcedure;
}
cnn.Open();
object retval = cmd.ExecuteScalar();
cnn.Close();
return retval;
}

public SqlDataReader ExecuteReader(string query)
{
SqlConnection cnn = new SqlConnection(strConnectionString);
SqlCommand cmd = new SqlCommand(query, cnn);
if (query.StartsWith("SELECT") | query.StartsWith("select"))
{
cmd.CommandType = CommandType.Text;
}
else
{
cmd.CommandType = CommandType.StoredProcedure;
}
cnn.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}

public int ExecuteNonQuery(string query)
{
SqlConnection cnn = new SqlConnection(strConnectionString);
SqlCommand cmd = new SqlCommand(query, cnn);
if (query.StartsWith("INSERT") | query.StartsWith("insert") | query.StartsWith("UPDATE") | query.StartsWith("update") | query.StartsWith("DELETE") | query.StartsWith("delete"))
{
cmd.CommandType = CommandType.Text;
}
else
{
cmd.CommandType = CommandType.StoredProcedure;
}
int retval;
try
{
cnn.Open();
retval = cmd.ExecuteNonQuery();
}
catch (Exception exp)
{
throw exp;
}
finally
{
if (cnn.State == ConnectionState.Open)
{
cnn.Close();
}
}
return retval;
}

public DataSet ExecuteDataSet(string query)
{
SqlConnection cnn = new SqlConnection(strConnectionString);
SqlCommand cmd = new SqlCommand(query, cnn);
if (query.StartsWith("SELECT") | query.StartsWith("select"))
{
cmd.CommandType = CommandType.Text;
}
else
{
cmd.CommandType = CommandType.StoredProcedure;
}
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend