ไฟล์ class นี้อยู่ใน App_Code นะครับ ผมตั้ง Buid Action เป็น Compile
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
namespace Pun
{
public class sampleClass
{
public static SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["daradbConnectionString"].ConnectionString);
public static List<Dictionary<string, string>> DaraNew()
{
List<Dictionary<string, string>> list = new List<Dictionary<string,string>>();
try
{
conn.Open();
SqlCommand strSQL = new SqlCommand("SELECT top 2 * FROM [member] ORDER BY [id_member] DESC", conn);
SqlDataReader dr = strSQL.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Dictionary<string, string> tmp = new Dictionary<string,string>();
int i = 0;
while (i < dr.FieldCount)
{
tmp.Add(dr.GetName(i).ToString(), dr.GetValue(i).ToString());
i++;
}
list.Add(tmp);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("error : " + ex.Message);
}
finally
{
conn.Close();
}
return list;
}
}
}
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.Data;
using System.Web.Security;
namespace Pun
{
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
sampleClass.DaraNew();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Project
{
public partial class view: System.Web.UI.Page
{
public List<Dictionary<string, string>> arrnew;
public List<Dictionary<string, string>> arrpopular;
public List<Dictionary<string, string>> arrall;
public Dictionary<string, string> arr_favorite;
protected void Page_Load(object sender, EventArgs e)
{
cIndex index = new cIndex();
arrnew = index.DaraNew();
arrpopular = index.DaraPopular();
arrall = index.DaraAll();
arr_favorite = Session["arr_favorite"] as Dictionary<string, string>;
}
}
}