2013-02-20 74 views
-6

这里是我的代码:使用linq连接asp点网络?

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页面database..there是链接像MyDataClassesDataContext,GVEmp.DataSource某些事情,这是非常新的给我,我不明白其中的任何

+6

我很抱歉,但解释的代码大块是不是这个网站的目的,阅读起来。如果您了解C#和Linq的兴趣,那么您应该尝试使用许多博客,书籍和其他资源。当你有一个关于编码问题的*非常具体的*和经过深入研究的问题时,询问堆栈溢出,我们将很乐意提供帮助。 – 2013-02-20 05:00:53

回答