C# 从数据库获取数据并分配它
我已从数据库中分配检索到的数据(C# 从数据库获取数据并分配它,c#,sql,asp.net,asp.net-mvc,database,C#,Sql,Asp.net,Asp.net Mvc,Database,我已从数据库中分配检索到的数据(ChancesLeft),并使用创建的变量分配它。但是当我检查控制器中创建的变量时(已经分配给检索到的数据(ChancesLeft)从数据库中,创建变量的值始终为0,即使我已经调用了该检索函数。但是,当我签入检索函数所在的类时,创建变量的值不是0,而是在检索数据的值之后(ChancesLeft) 我的问题是,如何将值从类传递给控制器?以便使用它 代码(系统管理器): 控制器: SystemManager _sm = new SystemManager();
ChancesLeft
),并使用创建的变量分配它。但是当我检查控制器中创建的变量时(已经分配给检索到的数据(ChancesLeft
)从数据库中,创建变量的值始终为0,即使我已经调用了该检索函数。但是,当我签入检索函数所在的类时,创建变量的值不是0,而是在检索数据的值之后(ChancesLeft
)
我的问题是,如何将值从类传递给控制器?以便使用它
代码(系统管理器):
控制器:
SystemManager _sm = new SystemManager();
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyModel context, string Name)
{
_sm.RetrieveChancesLeft(Name);
if (context.Chances > 0)
{
return RedirectToAction("About", "Home");
}
else
{
return RedirectToAction("GetStarted", "Home");
}
}
public MyModel RetrieveChancesLeft(string Name)
{
MyModel context = new MyModel();
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = "SELECT [Name], [ChancesLeft] FROM [Credentials] WHERE [Name] = @Name";
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
cmd.Parameters["@Name"].Value = Name;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
context.Chances = Convert.ToInt32(reader["ChancesLeft"]);
}
}
}
conn.Close();
}
return context;
}
SystemManager _sm = new SystemManager();
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyModel context, string Name)
{
context = _sm.RetrieveChancesLeft(Name);
if (context.Chances > 0)
{
return RedirectToAction("About", "Home");
}
else
{
return RedirectToAction("GetStarted", "Home");
}
}
问题是,每当我运行程序并输入
名称时,它总是重定向到GetStarted
视图,这意味着上下文。尽管数据库中的ChancesLeft
不是0,但
的几率始终为0。RetrieveChancesLeft函数应该将本地上下文对象存储在用于检索的ystemManager对象或将上下文返回给调用方。您可以更改RetrieveChancesLeft函数的返回类型,以将对象传递给控制器:
SystemManager _sm = new SystemManager();
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyModel context, string Name)
{
_sm.RetrieveChancesLeft(Name);
if (context.Chances > 0)
{
return RedirectToAction("About", "Home");
}
else
{
return RedirectToAction("GetStarted", "Home");
}
}
public MyModel RetrieveChancesLeft(string Name)
{
MyModel context = new MyModel();
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = "SELECT [Name], [ChancesLeft] FROM [Credentials] WHERE [Name] = @Name";
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
cmd.Parameters["@Name"].Value = Name;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
context.Chances = Convert.ToInt32(reader["ChancesLeft"]);
}
}
}
conn.Close();
}
return context;
}
SystemManager _sm = new SystemManager();
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyModel context, string Name)
{
context = _sm.RetrieveChancesLeft(Name);
if (context.Chances > 0)
{
return RedirectToAction("About", "Home");
}
else
{
return RedirectToAction("GetStarted", "Home");
}
}
和您的控制器:
SystemManager _sm = new SystemManager();
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyModel context, string Name)
{
_sm.RetrieveChancesLeft(Name);
if (context.Chances > 0)
{
return RedirectToAction("About", "Home");
}
else
{
return RedirectToAction("GetStarted", "Home");
}
}
public MyModel RetrieveChancesLeft(string Name)
{
MyModel context = new MyModel();
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = "SELECT [Name], [ChancesLeft] FROM [Credentials] WHERE [Name] = @Name";
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
cmd.Parameters["@Name"].Value = Name;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
context.Chances = Convert.ToInt32(reader["ChancesLeft"]);
}
}
}
conn.Close();
}
return context;
}
SystemManager _sm = new SystemManager();
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyModel context, string Name)
{
context = _sm.RetrieveChancesLeft(Name);
if (context.Chances > 0)
{
return RedirectToAction("About", "Home");
}
else
{
return RedirectToAction("GetStarted", "Home");
}
}