【问题标题】:Linking of asp dot net using linq?使用linq链接asp dot net?
【发布时间】:2013-02-05 00:27:31
【问题描述】:

这是我的代码:

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 对我来说是非常新的,我不明白其中的任何一个

【问题讨论】:

  • 很抱歉,解释大量代码不是本网站的目的。如果您对学习 C# 和 Linq 感兴趣,那么您应该尝试大量的博客和书籍以及其他资源。如果您有关于编码问题的非常具体且经过充分研究的问题,请咨询 Stack Overflow,我们很乐意提供帮助。

标签: c# asp.net linq hyperlink


【解决方案1】:

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

阅读这些工具

【讨论】:

    猜你喜欢
    • 2011-01-27
    • 2016-03-02
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 2021-04-11
    • 2020-06-14
    相关资源
    最近更新 更多