使用mvc体系结构在.net中插入记录的示例程序 [英] Sample program for insert the record in .net using mvc architecture

查看:61
本文介绍了使用mvc体系结构在.net中插入记录的示例程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This is model class file:-

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

namespace MVC.Models
{
    public class Idetails
    {
        private int Id;
        private string Name;
        private string City;


        public int id
        {
            set
            {
                Id = value;
            }
            get
            {
                return Id;
            }
        }

        public string name
        {
            set
            {
                Name = value;
            }
            get
            {
                return Name;
            }
        }

        public string city
        {
            set
            {
                City = value;
            }
            get
            {
                return City;
            }
        }
    }
}



This is the controller file:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC.Models;
using System.Data.SqlClient;
using System.Data;

namespace MVC.Controllers
{
    public class DetailsController : Controller
    {
        //
        // GET: /Details/

        public ActionResult det()
        {
            Idetails i = new Idetails();
            SqlConnection con = new SqlConnection("Data Source=PRABAKARAN\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into ct values("+i.id+",'"+i.name+"','"+i.city+"')", con);
            cmd.ExecuteNonQuery();
            return View(i);
        }

    }
}

In that views i dont know how to write the query in design page because the code page is not open..

please help me to solve this..

推荐答案

public ActionResult det()

{

Connection();

Idetails i = new Idetails();

con_Open();

SqlCommand cmd = new SqlCommand();

cmd = new SqlCommand(MVC_Insert ,con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue(@ Id,1);

cmd.Parameters.AddWithValue(@ Name,Himanshu);

cmd.Parameters.AddWithValue( @City,勒克瑙);

int ins = cmd.ExecuteNonQuery();

con_Close();

返回视图(i );

}



public void连接()

{

con = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnString] .ConnectionString);

}



public static void con_Open()

{

试试

{

if(con.State == ConnectionState.Open)

{

con.Close();

}

con.Open();

}

catch(例外情况)

{



}

}



public void con_Close()

{

试试

{

if(con.State == ConnectionState.Open)

{

con.Close();
}

con.Close();

}

catch(exception ex)

{

}

}
public ActionResult det()
{
Connection();
Idetails i = new Idetails();
con_Open();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand("MVC_Insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", 1);
cmd.Parameters.AddWithValue("@Name", "Himanshu");
cmd.Parameters.AddWithValue("@City", "Lucknow");
int ins = cmd.ExecuteNonQuery();
con_Close();
return View(i);
}

public void Connection()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
}

public static void con_Open()
{
try
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
}
catch (Exception ex)
{

}
}

public void con_Close()
{
try
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Close();
}
catch (Exception ex)
{
}
}


您的视图应指向您创建的模型。在您的控制器中,您可以访问您的模型。这就是它们的联系方式。在您使用 模型点 查看时,您可以访问所有方法。

请参阅此以获取更多帮助

http:// www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3 [ ^ ]
Your view should point to the model you have created. and in your controller you have access to your model. This is how they are linked. In you view using Model dot you can access all the methods.
Refer this for more help
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3[^]


这篇关于使用mvc体系结构在.net中插入记录的示例程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆