SQL
中搜索传统的MVC
查询,而不是实体框架等。所以如果你们给我一些例子会很好。
我开始学习MVC
,但很多示例使用linq
到SQL
和EF
等我根本不想使用,我想使用模型层中的简单旧SQL
查询。
答案 0 :(得分:11)
最简单的例子:
//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
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" />
}
答案 1 :(得分:0)
所以只需添加一个Helper class
并编写您需要的任何内容(Sql连接,命令,查询......),然后从您的控制器调用这些辅助方法。它与旧样式没有区别
静态助手类:
public static class HelperFunctions
{
private static string connString = "your connection string";
public static IEnumerable<User> GetAllUsers()
{
using (var conn = new SqlConnection(connString))
using (var cmd = new SqlCommand(connection:conn))
{
// set your command text, execute your command
// get results and return
}
}
在您的控制器中:
public ActionResult Users()
{
// Get user list
IEnumerable<User> users = HelperFunctions.GetAllUsers();
// Return your view and pass it to your list
return View(users);
}
在您的视图中设置您的视图模型:
@model IEnumerable<User>
然后,您可以从View中访问您的用户列表,例如:
foreach(var user in Model)
{
<p> @user.Name </p>
}
Model
代表您的实际视图模型。如果您想从View中访问多个列表,可以使用ViewBag或ViewData。有关详细信息,请查看此文章:http://www.codeproject.com/Articles/476967/WhatplusisplusViewData-2cplusViewBagplusandplusTem
答案 2 :(得分:0)
如何编写存储过程,只需将它们作为方法调用,只需在项目中添加LinQ to Sql Class
,这样就可以更轻松地处理数据库了
在完成所需的存储过程之后:
- 右键单击项目,然后添加新项目,然后选择LinQ to SQL Class并将其命名为
- 拖放以前制作的存储过程
- 制作
的实例linq to sql class
然后你可以调用你的存储过程和这个类的成员
http://msdn.microsoft.com/en-us/library/bb386946(v=vs.110).aspx
此链接也可以帮助您
答案 3 :(得分:0)
使用实体框架可以完成
Entitiesdb db = new Entitiesdb();
string query="delete from tableA where id=@id"
db.Database.ExecuteSqlCommand(query, @id);