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;

/// <summary>
/// CDatabase 的摘要说明
/// </summary>
public class CDatabase
{

    public CDatabase()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    private static string DATA_BASE_TYPE = "MSSQL"; //or "ACCESS"
    public static SqlConnection SQLconn;

    public static SqlConnection wtoconn()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["Custmerconn"].ToString());
        return conn;
    }

    public static bool DBUpdate(string sqlstr)
    {
        //适用于 delete,update,insert into.
        if (DATA_BASE_TYPE == "MSSQL") //MSSQL执行
        {
            SqlConnection conn = wtoconn();
            conn.Open();
            SqlCommand scd = new SqlCommand(sqlstr, conn);
            scd.ExecuteNonQuery();
            conn.Close();

            return true;
        }
        else //如果是ACCESS就执行别的语句
        {
            return false;
        }

    }

    public static object DBSelect(string sqlstr)
    {

        if (DATA_BASE_TYPE == "MSSQL") //MSSQL执行
        {
            SQLconn = wtoconn();
            SqlDataReader RD;
            SQLconn.Open();
            SqlCommand scd = new SqlCommand(sqlstr, SQLconn);
            RD = scd.ExecuteReader();
            return RD;

        }
        else
        {
            return null;
        }
    }


    public static string DBReadData(string sqlstr)
    {
        if (DATA_BASE_TYPE == "MSSQL") //MSSQL执行
        {
            SQLconn = wtoconn();
            SqlDataReader RD;
            SQLconn.Open();
            SqlCommand scd = new SqlCommand(sqlstr, SQLconn);
            RD = scd.ExecuteReader();
            string re;
            if (RD.Read())
            {
                re = Convert.ToString(RD[0]);
            }
            else
            {
                re = "-1";
            }

            SQLconn.Close();
            return re;


        }
        else
        {
            return null;
        }

    }

    public static void CloseConn() //关闭连接
    {

        if (DATA_BASE_TYPE == "MSSQL") //MSSQL执行
        {
            SQLconn.Close();
        }
        else
        {
            return;
        }
    }


    public static object DBFillSelect(string sqlstr) //以数据集的方式读取数据
    {
        SQLconn = wtoconn();
        DataSet ds = new DataSet(); //数据集,用于存放离线数据 
        SqlDataAdapter Sqlda = new SqlDataAdapter(sqlstr, SQLconn);
        Sqlda.Fill(ds); //填充数据集的第一个表格
        SQLconn.Close();
        return ds;
    }

    public static string[] DBReader(string sqlstr,int n)
    {
            string[] va = new string[50];
            SQLconn = wtoconn();
            SqlDataReader RD;
            SQLconn.Open();
            SqlCommand scd = new SqlCommand(sqlstr, SQLconn);
            RD = scd.ExecuteReader();
            string re;
            if (RD.Read())
            {
                for (int i = 0; i < n; i++)
                {
                    va[i] = RD[i].ToString();
                }
            }
            SQLconn.Close();
            return va;
    }


}
