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.Text;
public interface INews
{
 
    DataSet Getnews(string sLeibie,int nCount);
    SqlDataReader Getcorp();
    SqlDataReader Getproduct(int nProductID);
    SqlDataReader GetnewsByID(int nMynewsID);
    //公共打工数据库函数
    SqlDataReader SqlGetinfo(string sCmd);
    DataSet DsGetinfo(string sCmd);
    int Executeinfo(string sCmd);
    //加密
    string Myencode(string spass);
}
/// <summary>
/// Mynews 的摘要说明
/// </summary>
public class Mynews:INews
{
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
	public Mynews()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    //SQL reader
    public SqlDataReader SqlGetinfo(string sCmd)
    {
        string cmdText = sCmd;
        SqlDataReader dr = null;
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        return dr;
    }
    //DataSet 
    public DataSet DsGetinfo(string sCmd)
    {
        string cmdText = sCmd;
        SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);
        DataSet ds = new DataSet();
        try
        {
            myConnection.Open();
            da.Fill(ds, "aa");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
        return ds;
    }
    //Execute
    public int Executeinfo(string sCmd)
    {
        int nResult = -1;
        string cmdText = sCmd;
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            nResult = myCommand.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
        return nResult;
    }
    
    //按ID显示产品
    public SqlDataReader Getproduct(int nProductID)
    {
       
        string cmdText = "SELECT ProductID,Title,Content,Imgurl,Showorder,Hit,CONVERT(varchar(12) ,Createdate, 111 )  AS Mydate FROM Product WHERE ProductID=" + nProductID;
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        SqlDataReader dr = null;
        try
        {
            myConnection.Open();
            dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        return dr;
    }
    //打开企业简价数据库
    public SqlDataReader Getcorp()
    {
        string cmdText = "SELECT * FROM Corp";
        SqlDataReader dr = null;
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        return dr;
    }
    //打开新闻数据库
    public DataSet Getnews(string sLeibie,int nCount)
    {
        
        string cmdText = "SELECT TOP "+nCount+" MynewsID,Title,Createdate FROM Mynews WHERE Leibie='" + sLeibie + "' ORDER BY Createdate DESC";
        SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);
        DataSet ds = new DataSet();
        try
        {
            myConnection.Open();
            da.Fill(ds, "aa");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
        return ds;
    }
    //按ID取新闻
    public SqlDataReader GetnewsByID(int nMynewsID)
    {
        string cmdText = "SELECT *  FROM Mynews WHERE MynewsID=" + nMynewsID;
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        SqlDataReader dr = null;
        try
        {
            myConnection.Open();
            dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        return dr;
    }
    //加密
    public string Myencode(string spass)
    {
        Encoding utfstr = Encoding.UTF7;
        byte[] utfstrByte = utfstr.GetBytes(spass.Trim());
        string mima = BitConverter.ToString(utfstrByte);
        return mima;
    }
    
}
