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 MyCodeSnippet.DAL
{
public class DataBase
{
#region "member variable"
static SqlConnection conn = new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123");
private SqlCommand cmd;
private SqlDataAdapter da;
#endregion
#region "member function"
public static void ExecuteNonQuery(string query)
{
try
{
SqlCommand cmd = new SqlCommand(query, conn);
cmd.CommandType = CommandType.Text;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
}
}
public static DataTable ExecuteQuery(string query)
{
DataTable dt = new DataTable();
try
{
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
da.Fill(dt);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
return dt;
}
#endregion
}
}
No comments:
Post a Comment