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

/// <summary>
/// DataAccess 的摘要说明
/// </summary>
public class DataAccess
{
    private static OleDbConnection Conn = new OleDbConnection();
    private static OleDbCommand Cmd = new OleDbCommand();

    public DataAccess()
    {

    }

    /// <summary>
    /// 打开连接
    /// </summary>
    public static void Open()
    {
        if (Conn.State.Equals(ConnectionState.Closed))
        {
            Conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source="
    + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Paper.mdb");
            Cmd.Connection = Conn;
            Conn.Open();
        }
    }

    /// <summary>
    /// 关闭连接
    /// </summary>
    public static void Close()
    {
        if (Conn.State == ConnectionState.Open)
        {
            Conn.Close();
            Conn.Dispose();
            Cmd.Dispose();
        }
    }


    public static DataSet GetDataSet(string sql)
    {
        try
        {
            Open();
            OleDbDataAdapter Da = new OleDbDataAdapter(sql, Conn);
            DataSet Ds = new DataSet();
            Da.Fill(Ds);
            return Ds;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message, e);
        }
        finally
        {
            Close();
        }
    }

    public static DataTable GetDataTable(string sql)
    {
        try
        {
            Open();
            OleDbDataAdapter Da = new OleDbDataAdapter(sql, Conn);
            DataTable Dt = new DataTable();
            Da.Fill(Dt);
            return Dt;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message, e);
        }
        finally
        {
            Close();
        }
    }

    //=================================================
    //功能描述：获取某个字段数据
    //输入参数：sql，查询的SQL语句
    //返回值：hang
    //时间：2007.11.10
    //=================================================
    public static string FindString(string sql)
    {

        try
        {
            Open();

            #region
            string strReturn = "";

            //Cmd = new OleDbCommand(sql, Conn);
            //strReturn = Cmd.ExecuteScalar().ToString();

            OleDbDataAdapter Da = new OleDbDataAdapter(sql, Conn);
            DataTable Dt = new DataTable();
            Da.Fill(Dt);
            if (Dt.Rows.Count > 0)
            {
                strReturn = Dt.Rows[0][0].ToString();
            }

            return strReturn;
            #endregion

        }
        catch (Exception e)
        {
            throw new Exception(e.Message, e);
        }
        finally
        {
            Close();
        }



    }
    public static string RunSqlReturn(string sql)
    {
        try
        {
            Open();

            #region
            string strReturn = "";

            //Cmd = new OleDbCommand(sql, Conn);
            //strReturn = Cmd.ExecuteScalar().ToString();

            OleDbDataAdapter Da = new OleDbDataAdapter(sql, Conn);
            DataTable Dt = new DataTable();
            Da.Fill(Dt);
            if (Dt.Rows.Count > 0)
            {
                strReturn = Dt.Rows[0][0].ToString();
            }

            return strReturn;
            #endregion

        }
        catch (Exception e)
        {
            throw new Exception(e.Message, e);
        }
        finally
        {
            Close();
        }
    }

    public static int ExecuteCmd(string sql)
    {
        try
        {
            Open();
            Cmd.CommandType = CommandType.Text;
            Cmd.CommandText = sql;
            Cmd.ExecuteNonQuery();
            return 1;
        }
        catch
        {
            return -1;
        }
        finally
        {
            Close();
        }
    }

    /// <summary>
    /// 统计记录条数
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    public static int CountCmd(string sql)
    {
        try
        {
            Open();
            Cmd.CommandType = CommandType.Text;
            Cmd.CommandText = sql;
            return (int)Cmd.ExecuteScalar();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message, e);
        }
        finally
        {
            Close();
        }
    }

    /// <summary>
    /// 判断管理员
    /// </summary>
    public static void IsAdmin()
    {
        if (HttpContext.Current.Session["UserName"] == null)
        {
            HttpContext.Current.Response.Write(@"<script>alert('你没有登陆或登陆超时!');top.location.href='\\admin/Login.aspx';</script>");
        }
    }

    public static void Message(System.Web.UI.Page page, string nr)
    {

        page.RegisterStartupScript("message", "<script language='javascript' defer>alert('" + nr + "');</script>");
    }

    public static void Message1(System.Web.UI.Page page, string nr, string Url)
    {

        page.RegisterStartupScript("message", "<script language='javascript' defer>alert('" + nr + "'); location='" + Url + "'</script>");
    }

    ///   <summary>   
    ///   判断是否为数字   
    ///   </summary>   
    ///   <param   name="myObj">传递的参数</param>   
    ///   <returns>返回真假值</returns>   
    public static bool isNumber(Object myObj)
    {
        try
        {
            int i = Convert.ToInt32(myObj);
            return (true);
        }
        catch
        {
            return (false);
        }
    }


}
