做校園之星評選系統的類

c979170768發表於2011-11-14
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Collections;
/// <summary>
///Class1 的摘要說明
/// </summary>
public class Class1
{
    public Class1()
    {
        //
        //TODO: 在此處新增建構函式邏輯
        //
    }

    public static readonly string cnnstring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;  //連線串

    public static int exenum(string sqlcmd, params SqlParameter[] pass)
    {
        using (SqlConnection con = new SqlConnection(cnnstring))
        {
            using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
            {
                Addcmd(pass, cmd);
                con.Open();
                return cmd.ExecuteNonQuery();
            }
        }


    }  //增刪改

    private static void Addcmd(SqlParameter[] pass, SqlCommand cmd)
    {
        foreach (SqlParameter item in pass)
        {
            cmd.Parameters.Add(item);
        }
    }   //引數的新增

    public static DataTable exetable(string sqlcmd, params SqlParameter[] pass)
    {
        using (SqlConnection con = new SqlConnection(cnnstring))
        {
            using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
            {
                Addcmd(pass, cmd);
                con.Open();
                using (SqlDataReader myreader = cmd.ExecuteReader())
                {

                    DataTable dt = new DataTable();
                    dt.Clear();
                    dt.Load(myreader);
                    return dt;
                }
            }
        }


    }    //查詢

    public static object setup(string sqlcmd, params SqlParameter[] pass)
    {
        using (SqlConnection con = new SqlConnection(cnnstring))
        {
            using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
            {
                con.Open();
                Addcmd(pass, cmd);
                return cmd.ExecuteScalar();
            }
        }
    }   //這裡是難證是否登入成功

    public static string checkuser(string sqlcmd, string username, params SqlParameter[] pass)
    {
        string sign = "NO";
        using (SqlConnection con = new SqlConnection(cnnstring))
        {
            using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
            {
                Addcmd(pass, cmd);
                con.Open();
                SqlDataReader myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    if (myreader[0].ToString() == username)
                    {
                        sign = "Ok";
                    }
                }
                return sign;
            }
        }


    }   //檢查使用者是否存在

    public static void BindDG(GridView dg, string id, string strSql, string Tname)
    {

        ArrayList list = new ArrayList();
        SqlConnection conn = new SqlConnection(cnnstring);
        SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
        DataSet ds = new DataSet();
        Random rd = new Random();
        int num = Convert.ToInt32(setup("select count(*) from question"));
        int totle = 0;
        while (true)
        {
            int next = rd.Next(1, num);
            if (!list.Contains(next))
            {
                list.Add(next);
                //sda.Fill(ds, Tname);                             //填充資料
                //dg.DataSource = ds.Tables[Tname];               //把這一個表繫結一起來
                //dg.DataKeyNames = new string[] { id };          //設定主主鍵
                //dg.DataBind();
                totle++;
            }

            if (totle >= 6)
            {
                break;
            }
        }

 

 

        //SqlConnection conn = new SqlConnection(cnnstring);
        //SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
        //DataSet ds = new DataSet();
        //sda.Fill(ds, Tname);                             //填充資料
        //dg.DataSource = ds.Tables[Tname];               //把這一個表繫結一起來
        //dg.DataKeyNames = new string[] { id };          //設定主主鍵
        //dg.DataBind();                                 //資料繫結
    }

    public static string ret(string sqlcmd,params SqlParameter[] pass)
    {
        using (SqlConnection con = new SqlConnection(cnnstring))
        {
            using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
            {
                Addcmd(pass, cmd);
                con.Open();
                SqlDataReader myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    if (myreader[5].ToString() == "False")
                    {
                        return "1";           //使用者沒有評過
                    }
                    else
                    {
                        return "2";          //使用者評過了
                   
                    }
                }
                return "3";                  //使用者登入失敗
            }
        }

    }

    public static string retname(string sqlcmd, params SqlParameter[] pass)
    {
        using (SqlConnection con = new SqlConnection(cnnstring))
        {
            using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
            {
                Addcmd(pass, cmd);
                con.Open();
                SqlDataReader myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    if (myreader[5].ToString() == "False")
                    {
                        return "1";           //使用者沒有評過
                     }
                    else
                    {
                        return "2";          //使用者評過了
                    }
                }
                return "3";                  //使用者登入失敗
            }
        }
   
   
   
   
    }

}

相關文章