MVC中的CRUD操作与实体框架

时间:2013-01-07 13:25:39

标签: sql-server asp.net-mvc-4

您好我是MVC的初学者,我正在CRUD使用MVC SQLEntity Framework尝试实现LINQ-SQL操作类。我在使用insert并获得所选表格的details时做得很好但是在进入编辑,详情和删除时我很困惑如何执行,所以有人可以帮助我。

这是我的代码

创建新员工

public ActionResult Index(Test test)
    {
        try
        {
            if (ModelState.IsValid)
            {
                test.insert(test);
                test.Name = "";
                test.LastName = "";
            }
        }
        catch (Exception)
        {

        }
        return View(test);
    }

显示所有结果

 public ActionResult display()
    {
        Test tst = new Test();
        return View(tst.getList());
    }

这是我在类文件中的代码

public class Test
{
    public int EmpID { get; set; }
    [Required]
    [DisplayName("Name")]
    public string Name { get; set; }

    [Required]
    [DisplayName("LastName")]
    public string LastName { get; set; }

    string strConnection = ConfigurationManager.ConnectionStrings["SomeDataBase"].ConnectionString.ToString();

    public void insert(Test test)
    {


        using (SqlConnection con = new SqlConnection(strConnection))
        {
            SqlCommand cmd = new SqlCommand("insert into Employee values('" + test.Name + "','" + test.LastName + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();
        }
    }

    public List<Test> getList()
    {
        List<Test> _lstPoducts = new List<Test>();
        SqlConnection con = new SqlConnection(strConnection);
        SqlCommand cmd = new SqlCommand("select * from Employee", con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Test _Products = new Test();
            _Products.EmpID = Convert.ToInt16(dr["EmpID"].ToString());
            _Products.Name = dr["FirstName"].ToString();
            _Products.LastName = dr["LastName"].ToString();

            _lstPoducts.Add(_Products);
        }

        return _lstPoducts;
    }

    public List<Test> edit(int id)
    {
        List<Test> _lstPoducts = new List<Test>();
        SqlConnection con = new SqlConnection(strConnection);
        SqlCommand cmd = new SqlCommand("select * from Employee where EmpID='" + id + "'", con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Test _Products = new Test();
            _Products.EmpID = Convert.ToInt16(dr["EmpID"].ToString());
            _Products.Name = dr["FirstName"].ToString();
            _Products.LastName = dr["LastName"].ToString();

            _lstPoducts.Add(_Products);
        }
        return _lstPoducts;
    }

}

有人可以帮助我完成剩余的操作,例如Details, Edit update and Delete

1 个答案:

答案 0 :(得分:2)

您可以使用

的详细信息
public Test details(int id)
{
    Test  _products = new Test();
    SqlConnection con = new SqlConnection(strConnection);
    SqlCommand cmd = new SqlCommand("select * from Employee where EmpID='" + id + "'", con);
    con.Open();

    try{
        SqlDataReader dr = cmd.ExecuteReader();
        _products.EmpID = Convert.ToInt16(dr["EmpID"].ToString());
        _products.Name = dr["FirstName"].ToString();
        _products.LastName = dr["LastName"].ToString();

    }
    catch(exception ex)
    {
         /* Do Some Stuff */
    }
    return _products;
}

从你的控制器

public ActionResult Details(int id)
{
    Test tst = new Test();
    return tst.Details(id);
}

对于编辑,您可以使用

public Bool Edit(test tst)
{
    SqlConnection con = new SqlConnection(strConnection);
    SqlCommand cmd = new SqlCommand("UPDATE TABLE Employee SET FirstName='"+tst.Name+"',Lastname='"+tst.LastName+"' where EmpID='" + tst.EmpId + "'", con);
    con.Open();
    try{
         SqlDataReader dr = cmd.ExecuteReader();
         return true;
    }  
    catch(exception ex)
    {
              /* Do Some Stuff */
    }

}

从你的控制器

public ActionResult Edit(test tsts)
{
    Test tst = new Test();
    return tst.Edit(tsts);
}

并对其余案例进行类似处理