即时通讯,新的ASP MVC和我不知道如何创建基于存储过程从我的数据库模型。我已经有数据库可以与另一个应用程序一起工作,并且我的网页必须使用提到的db。ASP .NET MVC 3模型+存储过程
如果有人能告诉我一些代码描述正确的方法如何做到这一点, (如果我不是清楚:我需要创建使用存储过程从我的数据库,仅此而已ASP .NET模式)提前
TXH
即时通讯,新的ASP MVC和我不知道如何创建基于存储过程从我的数据库模型。我已经有数据库可以与另一个应用程序一起工作,并且我的网页必须使用提到的db。ASP .NET MVC 3模型+存储过程
如果有人能告诉我一些代码描述正确的方法如何做到这一点, (如果我不是清楚:我需要创建使用存储过程从我的数据库,仅此而已ASP .NET模式)提前
TXH
@fgeorgiew你只需要知道如何从存储过程中填充模型(类)?你可以使用像NHibernate或Entity Framework这样的ORM来为你处理管道,或者使用原始的ADO.NET代码,就像下面的例子。请注意,这只是粗略的代码,但您明白了。
public class MyModel
{
public int ModelId { get; set; }
public string FirstName { get; set; }
}
public class SqlMyModelRespoitory : IMyModelRepository // optional for DI/IoC, assume interface with GetSingleModel method
{
public MyModel GetSingleModel()
{
MyModel model;
string connString = "server=10.1.1.1;database=MyDb;uid=me;pwd=hidden";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "p_GetMyModelFromDb";
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
model = new MyModel
{
ModelId = Convert.ToInt32(reader[0]),
FirstName = reader[1].ToString()
};
}
}
}
}
return model;
}
}
你可以先从一些抽象的,表明你的意图:
public interface IMyRepository
{
SomeModel Get(int id);
}
,那么你可以写一个将使用您的存储过程的实现:
public class MyRepositorySql: IMyRepository
{
public SomeModel Get(int id)
{
... call your stored procedure
}
}
然后设计自己的控制器,它需要这种抽象作为参数:
public class MyController: Controller
{
private readonly IMyRepository _repository;
public MyController(IMyRepository repository)
{
_repository = repository;
}
public ActionResult Index(int id)
{
var model = _repository.Get(id);
return View(model);
}
}
现在,所有剩下的就是配置您的DI框架,通过正确实施到控制器的构造函数。正如您现在所看到的,控制器与数据获取的方式完全分离。在你的数据访问层中使用StoredProcs,一些ORM或其他任何东西并不重要。
存储过程你离开我身后1秒。 –
thx :)我想我明白了。现在是时候实现这一点,看看结果 – fgeorgiew
好吧,让我们说你有一个SP名为Customers这是选择像一些列:
ID
名称
地址
现在,您将创建内部模型类的模型文件夹名为“Customer.cs”并定义属性如下:
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
这是你的MODEL类。
thx 4的回复,但我认为你没有正确理解我:)我知道什么模型是从RoR或Php等,而问题不是关于映射C#模型类到Db表,但问题是关于如何使用我的数据库和ADO .NET中的存储过程来完成的。 (关于最后一个我不太确定):) – fgeorgiew
明白了。我误解了你的问题...... –
如何调用Asp.Net MVC
using System;
using System.Collections.Generic;
using System.IO;
using System.Data;
using System.Data.Entity;
using System.Net;
using System.Data.SqlClient;
public partial class StudentRecord
{
public int Id { get; set; }
public string Name { get; set; }
public string Marks { get; set; }
public string Grade { get; set; }
public Nullable<System.DateTime> DOB { get; set; }
}
public class SqlMyModelRespoitory : First_Test_DBEntities
{
public StudentRecord GetSingleModel()
{
StudentRecord model;
string connString = @"Paste Your Local Connection String";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "InsertStudent";
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
model = new StudentRecord
{
Id = Convert.ToInt32(reader[0]),
Name = reader[1].ToString(),
Marks = reader[1].ToString(),
Grade = reader[1].ToString(),
DOB = Convert.ToDateTime(reader[1])
};
}
}
}
}
return model;
}
}
这正是我需要的,谢谢! :) Btw达林Dimitrov answear它很好,但我不熟悉DI(认为它是依赖注入)。 – fgeorgiew
如果我想要获取所有对象,该怎么办? –