//------------------FOR DEMO USE ONLY----------------------------------
//-------- Copyright by Artem Saveliev artem@savelev.com --------------
//---------------------------------------------------------------------
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
namespace tires.DataAccess
{
///
/// Static data access utilities
///
public class Adapter
{
public Adapter()
{
//
// TODO: Add constructor logic here
//
}
///
/// Get Database connection based on web.conf
///
/// Database connection
public static IDbConnection getConn()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnStr"];
return con;
}
///
/// Get Command object, only use to manipulate database manually
///
/// SQL statement to run
///
public static IDbCommand getCommand(string sql){
SqlConnection con = (SqlConnection)getConn();
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
return cmd;
}
///
/// Get DataReader for firehose reading of database
///
/// SQL statement to run
///
public static IDataReader getRs(string sql)
{
SqlCommand cmd = (SqlCommand)getCommand(sql);
SqlDataReader reader;
try
{
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
throw new Exception("Error executing following query: " + sql,e);
}
return reader;
}
///
/// Get DataAdapter to run complex queries
///
/// SQL statement to run
/// IDataAdapter for the command
public static IDataAdapter getAdapter(string sql)
{
SqlDataAdapter ad = new SqlDataAdapter((SqlCommand)getCommand(sql));
return ad;
}
///
/// Fill dataset with data
///
/// SQL statement to run
/// dataset to fill
/// table from result to use (required for typed datasets)
public static void fillDataSet(string sql, DataSet s, string table) {
SqlCommand cmd = (SqlCommand)getCommand(sql);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(s,table);
cmd.Connection.Close();
}
///
/// Fill dataset with data
///
/// SQL statement to run
/// dataset to fill
/// table from result to use (required for typed datasets)
public static void fillDataSet(IDataAdapter ad, DataSet s, string table)
{
((SqlDataAdapter)ad).MissingSchemaAction = MissingSchemaAction.AddWithKey;
((SqlDataAdapter)ad).Fill(s,table);
}
public static void updateDataSet(IDataAdapter ad, DataSet s, string table)
{
((SqlDataAdapter)ad).Update(s,table);
}
public static void FillDataSet(string sql, DataSet ds)
{
SqlCommand cmd = (SqlCommand)getCommand(sql);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(ds);
cmd.Connection.Close();
}
///
/// Get DataSet from SQL query
///
/// SQL statement to run
/// DataSet
public static DataSet getDataSet(string sql)
{
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = (SqlCommand)getCommand(sql);
DataSet custDS = new DataSet();
custDA.Fill(custDS);
custDA.SelectCommand.Connection.Close();
return custDS;
}
///
/// Get one page of SQL result into DataTable
///
/// SQL Statement to run
/// Size of the page to return
/// Element to start page with
/// DataTable comtaining result
public static DataTable getDataTablePage(string sql,int pageSize, int startRecord)
{
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = (SqlCommand)getCommand(sql);
DataSet custDS = new DataSet();
custDA.Fill(custDS,startRecord,pageSize,"data");
custDA.SelectCommand.Connection.Close();
return custDS.Tables["data"];
}
///
/// Get whole SQL result into DataTable
///
/// SQL Statement to run
/// DataTable comtaining result
public static DataTable getDataTable(string sql)
{
SqlDataAdapter custDA = new SqlDataAdapter();
DataTable t;
try
{
custDA.SelectCommand = (SqlCommand)getCommand(sql);
DataSet custDS = new DataSet();
custDA.Fill(custDS,"data");
t = custDS.Tables["data"];
}
catch (Exception e)
{
throw new Exception("Error executing following query: " + sql,e);
}
finally
{
if (custDA.SelectCommand != null) custDA.SelectCommand.Connection.Close();
}
return t;
}
///
/// Get scalar value from the database. Use getText to read large (over 8Kb) text values, because data is split into rows in that case.
///
/// SQL statement to run, has to return one column
/// Object type, cast to your type
public static Object getScalar(string sql)
{
object v;
SqlCommand cmd = (SqlCommand)getCommand(sql);
try
{
v = cmd.ExecuteScalar();
}
catch (Exception e)
{
throw new Exception("Error executing following query: " + sql,e);
}
finally
{
cmd.Connection.Close();
}
return v;
}
///
/// Get scalar value from the database, return null if there is no result. Use getText to read large (over 8Kb) text values, because data is split into rows in that case.
///
/// SQL statement to run, has to return one column
/// Object type, cast to your type
public static Object getScalarEmpty(string sql)
{
object v;
SqlCommand cmd = (SqlCommand)getCommand(sql);
SqlDataReader r;
try
{
r = cmd.ExecuteReader();
}
catch (Exception e)
{
cmd.Connection.Close();
throw new Exception("Error executing following query: " + sql,e);
}
if (r.Read())
{
r.Close();
try
{
v = cmd.ExecuteScalar();
}
catch (Exception e)
{
throw new Exception("Error executing following query: " + sql,e);
}
finally
{
cmd.Connection.Close();
}
}
else
{
r.Close();
v = null;
}
return v;
}
///
/// Get scalar value from the database, return zero if there is no result. Use getText to read large (over 8Kb) text values, because data is split into rows in that case.
///
/// SQL statement to run, has to return one column
/// Object type, cast to your type
public static Object getScalarZero(string sql)
{
Object o = getScalarEmpty(sql);
if (o == null) return 0; else return o;
}
///
/// Get single text value from the database, able to read more than 8Kb
///
/// SQL statement to run, has to return one text column
/// string text value
public static string getText(string sql)
{
IDataReader r = getRs(sql);
string result = "";
while (r.Read())
result += stringValue(r[0]);
r.Close();
return result;
}
///
/// Run query that doesn't return result
///
/// SQL statement to run
public static void executeNonQuery(string sql)
{
SqlCommand cmd = (SqlCommand)getCommand(sql);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception("Error executing following query: " + sql,e);
}
finally
{
cmd.Connection.Close();
}
}
///
/// Escape text for SQL database
///
/// Text to escape
/// Escaped value
public static string escape(string txt)
{
if (txt != null)
return txt.Replace("'","''");
else
return "";
}
///
/// Convert empty (NULL) database value to empty string
///
/// value
/// string represetation
public static string stringValue(object val)
{
if (DBNull.Value == val)
return "";
else
return val.ToString();
}
}
}