ArryList應用

c979170768發表於2011-11-06
 
 

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

    public static readonly string cnnstring = ConfigurationManager.ConnectionStrings["con"].ConnectionString;

    public static ArrayList a(string s)
    {
        using (SqlConnection con=new SqlConnection(cnnstring))
        {
            using (SqlCommand cmd=new SqlCommand("select * from usertable",con))
            {
                SqlDataReader myreader;
                con.Open();
                myreader = cmd.ExecuteReader();
                ArrayList list = new ArrayList();
                  person p =null;
                while (myreader.Read())
                {
                    if (s=="1")
                    {
                        p = new person(myreader[1].ToString(), myreader[2].ToString());
                        list.Add(p); 
                    }
                    else if (s=="2")
                    {
                        list.Add(myreader[1].ToString() + "," + myreader[2].ToString());  
                    }

                 
                }
                return list;
            }
        }
    }

}

public class person
{
    private string personname = "";

    public string Personname
    {
        get { return personname; }
        set { personname = value; }
    }
    private string personpwd = "";

    public string Personpwd
    {
        get { return personpwd; }
        set { personpwd = value; }
    }

   public person(string name,string pwd)
    {
        personname = name;
        personpwd = pwd;
    }

}

以上是一個類檔案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (RadioButton1.Checked)
        {
            ArrayList list = Class1.a("2") as ArrayList;
            foreach (var item in list)
            {
                Response.Write(item + ";</br>");
            }
        }
        else {
            ArrayList list = Class1.a("1") as ArrayList;
            foreach (person item in list)
            {
                Response.Write("Name:" + item.Personname + "Password:" + item.Personpwd + ";</br>");
            }
       
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {


         ArrayList list=null;
        if (RadioButton1.Checked)
        {
            list = Class1.a("1") as ArrayList;
        }
        else
        {
           list = Class1.a("2") as ArrayList;


        }
        GridView1.DataSource = list;
            GridView1.DataBind();
    }
}

 

相關文章