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;
using System.Data.OleDb;

/// <summary>
/// BaseClass 的摘要说明 51as-px
/// </summary>
public class BaseClass
{
	public BaseClass()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

  public static   string strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

   public static  string ConnString ;
    //private static string GetConnString()
    //{
    //    string ConnString = @"provider=microsoft.jet.oledb.4.0;data source=";
    //    string DataFileFullName =+ @"~\App_Data\" + @"stry_exercise.mdb";
    //    ConnString += DataFileFullName;
    //    return ConnString;
    //}
    public static string getConn()
    {
     
        return strConn;
    }

    public static SqlConnection DBCon()
    {
        return new SqlConnection("server=.;database=db_ExamOnline;uid=sa;pwd=sa");
    }

    public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
    {

        OleDbCommand cmd = new OleDbCommand();

        using (OleDbConnection conn = new OleDbConnection(connString))
        {
            PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }
    }
    private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, CommandType cmdType, string cmdText, OleDbParameter[] cmdParms)
    {

        if (conn.State != ConnectionState.Open)
            conn.Open();

        cmd.Connection = conn;
        cmd.CommandText = cmdText;

        if (trans != null)
            cmd.Transaction = trans;

        cmd.CommandType = cmdType;

        if (cmdParms != null)
        {
            foreach (OleDbParameter parm in cmdParms)
                cmd.Parameters.Add(parm);
        }
    }

    public static int ExecuteNonQuery(string connString, string cmdText, params OleDbParameter[] cmdParms)
    {
        OleDbCommand cmd = new OleDbCommand();

        using (OleDbConnection conn = new OleDbConnection(connString))
        {
            PrepareCommand(cmd, conn, null, CommandType.Text, cmdText, cmdParms);

            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;

        }
    }


    public static OleDbConnection DBACon()
    {
     // string   strConn="Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\\Inetpub\\wwwroot\\mcks\\App_Data\\stry_exercise.mdb;";
       // string strConn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Inetpub\\wwwroot\\mcks\\App_Data\\stry_exercise.mdb;";

      //  return new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ConnString);

        return new OleDbConnection(strConn);
    }

    public static void BindDG(GridView dg, string id, string strSql, string Tname)
    {
       // SqlConnection conn = DBCon();
        OleDbConnection conn = DBACon();
       // SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
        OleDbDataAdapter sda = new OleDbDataAdapter(strSql, conn);
        DataSet ds = new DataSet();
        sda.Fill(ds, Tname);
        dg.DataSource = ds.Tables[Tname];
        dg.DataKeyNames = new string[] { id };
        dg.DataBind();
        conn.Close();
    }

    public static DataSet getDataset(string sql, string Tname)
    {
       
        OleDbConnection conn = DBACon();
        conn.Open();
        OleDbDataAdapter sda = new OleDbDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        sda.Fill(ds, Tname);


        conn.Close();
        return ds;
    }

    #region  执行SQL语句
    /// <summary>
    /// 说明：ExecSQL用来执行SQL语句。
    ///	返回值：操作是否成功(True\False)。
    ///	参数：sQueryString SQL字符串
    /// 创建日期：2006-1-22
    /// 创建人：张耀庭
    /// </summary>
    public Boolean ExecSQL(string sQueryString)
    {
        OleDbConnection conn = DBACon();
       // SqlConnection con = new SqlConnection(SqlConn);
       conn.Open();
       OleDbCommand dbCommand = new OleDbCommand(sQueryString, conn);
        try
        {
            dbCommand.ExecuteNonQuery();
            conn.Close();
        }
        catch (System.Exception e)
        {
            conn.Close();
            return false;
        }
        return true;
    }
    #endregion
    public static void OperateData(string strsql)
    {
      //  SqlConnection conn = DBCon();
        OleDbConnection conn = DBACon();
        conn.Open();
      //  SqlCommand cmd = new SqlCommand(strsql, conn);
        OleDbCommand cmd = new OleDbCommand(strsql, conn);
      
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    //================如果是学生登录=========================================
    public static bool CheckStudent(string studentNum, string studentPwd)
    {
      //  SqlConnection conn = DBCon();
        OleDbConnection conn = DBACon();
        conn.Open();
       // SqlCommand cmd = new SqlCommand("select count(*) from tb_Student where StudentNum='" + studentNum + "' and StudentPwd='" + studentPwd + "'", conn);

        OleDbCommand cmd = new OleDbCommand("select count(*) from Student where 姓名='" + studentNum + "' and 密码='" + studentPwd + "' and 学生=1", conn);
     
        int i = Convert.ToInt32(cmd.ExecuteScalar());
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
        conn.Close();
    }
    //================如果是教师登录=========================================
    public static bool CheckTeacher(string teacherNum, string teacherPwd)
    {
       // SqlConnection conn = DBCon();
        OleDbConnection conn = DBACon();
        conn.Open();
     //   SqlCommand cmd = new SqlCommand("select count(*) from tb_Teacher where TeacherNum='" + teacherNum + "' and TeacherPwd='" + teacherPwd + "'", conn);
        OleDbCommand cmd = new OleDbCommand("select count(*) from Student where 姓名='" + teacherNum + "' and 密码='" + teacherPwd + "' and 学生=0", conn);
        
        int i = Convert.ToInt32(cmd.ExecuteScalar());
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
        conn.Close();
    }
    //================如果是管理员登录=========================================
    public static bool CheckAdmin(string adminNum, string adminPwd)
    {
       // SqlConnection conn = DBCon();
        OleDbConnection conn = DBACon();
        conn.Open();
      //  SqlCommand cmd = new SqlCommand("select count(*) from tb_Admin where AdminNum='" + adminNum + "' and adminPwd='" + adminPwd + "'", conn);
        OleDbCommand cmd = new OleDbCommand("select count(*) from user where user='" + adminNum + "' and passwd='" + adminPwd + "'", conn);
     
        
        int i = Convert.ToInt32(cmd.ExecuteScalar());
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
        conn.Close();
    }
}
