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();
}
}
}
No comments:
Post a Comment