-6

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class First : System.Web.UI.Page
{ MyDataClassesDataContext mdc;


protected void Page_Load(object sender, EventArgs e)
{
    mdc = new MyDataClassesDataContext();
    if (!IsPostBack)
    {
        LoadData();
    }
}

private void LoadData()
{
    mdc = new MyDataClassesDataContext();
    var empls = from em in mdc.Emps select em;

    GVEmp.DataSource = empls;
    GVEmp.DataBind();

    var ddlempls = from em in mdc.Emps
            select new
            {
                em.EmpID,
                em.Ename
            };

    DDLEmp.DataSource = ddlempls;
    DDLEmp.DataTextField = "Ename";
    DDLEmp.DataValueField = "EmpID";
    DDLEmp.DataBind();
    DDLEmp.Items.Insert(0, "Select");
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
    Emp em = new Emp();
    em.Ename = TxtName.Text;
    em.Sal = Int32.Parse(TxtSal.Text);

    mdc.Emps.InsertOnSubmit(em);
    mdc.SubmitChanges();
    LoadData();
    LabDisp.Text = "Record Added";
}
protected void DDLEmp_SelectedIndexChanged(object sender, EventArgs e)
{ Emp empl = mdc.Emps.Single(em => em.EmpID ==Int32.Parse(DDLEmp.SelectedItem.Value));

    if (empl != null)
    {
        TxtName0.Text = empl.Ename;
        TxtSal0.Text = empl.Sal.ToString();
    }
    else
    {
        LabDisp.Text = "Data not found";
    }
}
protected void LBUpd_Click(object sender, EventArgs e)
{
 Emp empl = mdc.Emps.Single(em => em.EmpID ==Int32.Parse(DDLEmp.SelectedItem.Value));

    if (empl != null)
    {
        empl.Ename = TxtName0.Text;
        empl.Sal=Int32.Parse(TxtSal0.Text);
        mdc.SubmitChanges();
        LoadData();
        LabDisp.Text = "record updated";
    }
    else
    {
        LabDisp.Text = "Data not found";
    }
}
protected void LBDel_Click(object sender, EventArgs e)
{
 Emp empl = mdc.Emps.Single(em => em.EmpID == Int32.Parse(DDLEmp.SelectedItem.Value));

    if (empl != null)
    {
        mdc.Emps.DeleteOnSubmit(empl);
        mdc.SubmitChanges();
        LoadData();
        LabDisp.Text = "record deleted";
    }
    else
    {
        LabDisp.Text = "Data not found";
    }
}
}

谁能帮忙解释一下这段代码……我的一个朋友把这个和一个配置文件发给我,把一个asp页面和数据库链接起来……有些东西MyDataClassesDataContext,GVEmp.DataSource对我来说很新,我不明白其中任何一个

4

1 回答 1

0

看起来代码正在使用 EntityFrameWork 或 Linq2SQL 左右......这些链接可能会有所帮助

阅读这些工具

于 2013-02-20T05:04:29.980 回答