在ASP.NET MVC中使用简单查询 [英] using simple queries in ASP.NET MVC
本文介绍了在ASP.NET MVC中使用简单查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我检查了google,但发现没有任何好处.我在MVC
中而不是在Entity Framework等中搜索usinf传统SQL
查询.因此,如果你们提供一些示例,那就太好了.
I checked google but found nothing good. I am searching for usinf Traditional SQL
queries in MVC
instead of Entity framework etc. So it would be good if you guys provide me some examples.
我开始学习MVC
,但是很多示例都使用linq
至SQL
和EF
等,这些我根本不想使用,我想在Model中使用简单的旧SQL
查询层.
I started to learn MVC
but lot of examples uses linq
to SQL
and EF
etc which I don't want to use at all, I want to use simple old SQL
queries in Model layer.
推荐答案
最简单的示例:
//Domain Class
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace BanjoOnMyKnee.Models
{
public class DomainModel
{
public string connectionString = ".\\SQLEXPRESS; Initial-Catalog=YourDBName; Integrated-Security=true";
public void CreateSomething(ViewModel model)
{
using(SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("",connection))
{
command.CommandText = "insert into Names values(@Name)";
command.Parameters.AddWithValue("@Name", model.Name);
command.ExecuteNonQuery();
}
}
public ViewModel FindSomething(int id)
{
var model = new ViewModel();
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("", connection))
{
command.CommandText = "select * from Names where Id=@Id";
command.Parameters.AddWithValue("@Id",id);
SqlDataReader reader = command.ExecuteReader();
model.Id = id;
model.Name = reader["Name"].ToString();
}
return model;
}
public void DeleteSomething(ViewModel model)
{
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("", connection))
{
command.CommandText = "delete from Names where Id=@Id";
command.Parameters.AddWithValue("@Id", model.Id);
command.ExecuteNonQuery();
}
}
public void EditSomething(ViewModel model)
{
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("", connection))
{
command.CommandText = "Update Names set Name=@Name where Id=@Id";
command.Parameters.AddWithValue("@Name", model.Name);
command.Parameters.AddWithValue("@Id", model.Id);
command.ExecuteNonQuery();
}
}
}
}
这是我的控制器类
//My Controller class
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
//
// GET: /Home/Create
public ActionResult Create()
{
return View(new ViewModel());
}
//
// POST: /Home/Create
[HttpPost]
public ActionResult Create(ViewModel vm)
{
try
{
var domainModel = new DomainModel();
domainModel.CreateSomething(vm);
return RedirectToAction("Index");
}
catch
{
return View(new ViewModel());
}
}
//
// GET: /Home/Edit/5
public ActionResult Edit(int id)
{
ViewModel model = new DomainModel().FindSomething(id);
return View(model);
}
[HttpPost]
public ActionResult Edit(ViewModel editModel)
{
try
{
var dm = new DomainModel();
dm.EditSomething(editModel);
return RedirectToAction("Index");
}
catch
{
return View(new ViewModel());
}
}
}
我的ViewModel类
My ViewModel class
//My ViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BanjoOnMyKnee.Models
{
public class ViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
}
我的创建"视图
//My view
@model BanjoOnMyKnee.Models.ViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using(Html.BeginForm()){
@Html.HiddenFor(m => m.Id);
<p> Name :
Html.EditorFor(m=>m.Name);</p>
<input type="submit" value="Create" />
}
这篇关于在ASP.NET MVC中使用简单查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文