我正在尝试在数据库中插入数据。不幸的是,现在我无法开始使用存储库模式和统一框架的数据了。
这是我的代码:
控制器:
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
return RedirectToAction("Index");
}
catch
{
return View();
}
}
存储库:
public class UserMasterRepository : IUserMasterRepository
{
private List<UserMaster> users = new List<UserMaster>();
string connectionstring = @"Data Source="";Initial
Catalog=DI;User ID=ma;Password=***********";
public UserMaster Add(UserMaster item)
{
using (SqlConnection sqlCon = new SqlConnection(connectionstring))
{
sqlCon.Open();
string query = "INSERT INTO Product (@ID,@Name,@City,@Address)
Values(ID,Name,City,Address)";
for (int i = 0; i <= 100; i++)
{
SqlCommand sqlcmd = new SqlCommand(query, sqlCon);
sqlcmd.Parameters.Add("@ID", SqlDbType.Int).Value = i;
sqlcmd.Parameters.Add("@Name", SqlDbType.VarChar,
100).Value = "newride";
sqlcmd.Parameters.Add("@City", SqlDbType.VarChar,
100).Value = "newride";
sqlcmd.Parameters.Add("@Address", SqlDbType.VarChar,
100).Value = "USA";
sqlcmd.ExecuteNonQuery();
}
return item;
}
RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "Index", id =
UrlParameter.Optional }
);
}
以下代码我正在使用依赖注入统一框架 UnityConfig:
public static class UnityConfig
{
public static Type IUserRepository { get; private set; }
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<IUserMasterRepository,
UserMasterRepository>
();
DependencyResolver.SetResolver(new
UnityDependencyResolver(container));
}
}
IUserMasterRepository:
public interface IUserMasterRepository
{
IEnumerable<UserMaster> GetAll();
UserMaster Get(int id);
UserMaster Add(UserMaster item);
bool Update(UserMaster item);
bool Delete(int id);
}
不抛出任何错误
如何解决此问题。