0

嗨,我是初学者MVC,我正在尝试使用out或class来实现CRUD操作。我对所选表的操作做得很好,但是在进行编辑、详细信息和删除时,我对如何执行感到困惑,所以有人可以帮助我。MVCSQLEntity FrameworkLINQ-SQLinsertdetails

这是我的代码

创建新员工

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

4

1 回答 1

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);
}

其余案件也同样处理

于 2013-01-07T13:38:08.410 回答