This is the simple user define SQL Database Class. It is useful to avoid redundancy of the code.
Database Class In C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class _Database
{
public SqlConnection con;
public SqlCommand com = null;
public SqlDataReader dr = null;
public SqlDataAdapter da = null;
public DataSet ds = null;
public String glQuery = "";
public String glnonQuery = "";
public const int EXECUTE_NONE_QUERY = 1;
public const int EXECUTE_QUERY = 2;
public const int DATA_ADAPTER = 3;
// connection for data list paging
public SqlConnection globalcon = new SqlConnection("server=**;uid=**;pwd=***;Database=**");
public _Database() { }
public void Query(String SqlQuery)
{
this.glQuery = this.Get_DataBasePrefix(SqlQuery);
}
private String Get_DataBasePrefix(String str)
{
return str.Replace("#__", "GD_").ToString();
}
public void ConnectDB()
{
try
{
con = new SqlConnection();
con.ConnectionString = "server=*****;uid=**;pwd=****;Database=***";
con.Open();
}
catch (Exception ex)
{
}
}
public void CloseDB()
{
this.con.Close();
this.con.Dispose();
}
public bool Read()
{
return this.dr.Read();
}
public void ExecuteQuery(int Mode)
{
try
{
if (Mode == EXECUTE_NONE_QUERY) // Excutenonequery
{
this.com = new SqlCommand(this.glQuery, this.con);
this.com.ExecuteNonQuery();
}
else if (Mode == EXECUTE_QUERY) // Execute Query
{
this.com = new SqlCommand(this.glQuery, this.con);
this.dr = this.com.ExecuteReader();
}
else if (Mode == DATA_ADAPTER) // Data Adapter
{
this.da = new SqlDataAdapter(this.glQuery, this.con);
this.ds = new DataSet();
this.da.Fill(this.ds);
}
}
catch (Exception ex)
{
}
}
public bool HasRows()
{
if (this.dr.HasRows)
{
return true;
}
else
{
return false;
}
}
}
Comments
Post a Comment