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

/// <summary>
/// DBHelper 的摘要说明
/// </summary>
public static class DBHelper
{
    private static SqlConnection connection;

    public static SqlConnection Connection
    {
        get
        {
            string connectionString = "Data Source=222.73.47.32;Initial Catalog=XluoAJaxTest;User ID=xluoup;Pwd=123";
            //string connectionString = ConfigurationManager.ConnectionStrings["DBConfig"].ToString();
            if (connection == null)
            {
                connection = new SqlConnection(connectionString);
                connection.Open();
            }
            else if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }
            else if (connection.State == ConnectionState.Broken)
            {
                connection.Close();
                connection.Open();
            }
            return connection;
        }

    }
    /// <summary>
    /// 执行无参的sql语句
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    public static int ExecuteCommand(string sql)
    {
        SqlCommand command = new SqlCommand(sql, Connection);
        int result = command.ExecuteNonQuery();
        return result;
    }
    /// <summary>
    /// 执行带参的sql语句
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public static int ExecuteCommand(string sql, params SqlParameter[] values)
    {
        SqlCommand command = new SqlCommand(sql, Connection);
        command.Parameters.AddRange(values);
        int result = command.ExecuteNonQuery();
        return result;
    }
    /// <summary>
    /// 执行无参sql语句，返回查询记录数
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    public static int GetScalar(string sql)
    {
        SqlCommand command = new SqlCommand(sql, Connection);
        return Convert.ToInt32(command.ExecuteScalar());
    }
    /// <summary>
    /// 执行带参sql语句，返回查询记录数
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public static int GetScalar(string sql, params SqlParameter[] values)
    {
        SqlCommand command = new SqlCommand(sql, Connection);
        command.Parameters.AddRange(values);
        return Convert.ToInt32(command.ExecuteScalar());
    }
    /// <summary>
    /// 执行无参sql语句，返回Reader
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    public static SqlDataReader GetReader(string sql)
    {
        SqlCommand command = new SqlCommand(sql, Connection);
        SqlDataReader Reader = command.ExecuteReader();
        return Reader;
    }
    /// <summary>
    /// 执行有参sql语句，返回Reader
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
    {
        SqlCommand command = new SqlCommand(sql, Connection);
        command.Parameters.AddRange(values);
        SqlDataReader Reader = command.ExecuteReader();
        return Reader;
    }
    public static DataTable GetDataSet(string safeSql)
    {
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand(safeSql, Connection);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        return ds.Tables[0];
    }

    public static DataTable GetDataSet(string sql, params SqlParameter[] values)
    {
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand(sql, Connection);
        cmd.Parameters.AddRange(values);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        return ds.Tables[0];
    }


    public static object XluoGetScalar(string sql)
    {
        SqlCommand command = new SqlCommand(sql, Connection);
        return command.ExecuteScalar();
    }

    public static bool Exists(string sql)
    {
        SqlCommand command = new SqlCommand(sql, Connection);
        int i = Convert.ToInt32(command.ExecuteScalar());
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}
