做校园之星评选系统的类

 

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";                  //用户登录失败
            }
        }
   
   
   
   
    }

}

没有更多推荐了,返回首页