当我尝试调用将日期作为输入的方法时,我有一个例外。当我将同一应用程序部署到另一台计算机上时,页面加载良好。我需要了解这是否与代码或Sql Server配置有关。如果它们是更改,我应该在Controller中对日期格式进行更改。我尝试将所有SQL Server登录名设置为使用英语作为默认语言,但无济于事。其他指南指向在相关SP中强制转换DateTime数据类型。我正在使用的代码使用Raw SQL。我的想法是,可以重构 PayPeriod 的SQL语句中的转换,但不能确定要转换为哪个数据类型。
在我调用的控制器方法中,我有:
....
if (Convert.ToString(formcollection["PayDayId"]) != null)
{
payrun.PayDayId = Convert.ToInt32(formcollection["PayDayId"]);
Session["PayDayId"] = payrun.PayDayId;
DateTime Today = DateTime.Today;
var startdate = new DateTime(Today.Year, Today.Month, 1);
var enddate = startdate.AddMonths(1).AddDays(-1);
using (SqlConnection conn1 = new SqlConnection(Helpers.DatabaseConnect))
{
SqlCommand cmd1 = new SqlCommand("SELECT PayDayDate FROM PayDayCode WHERE PayDayId= " + Convert.ToInt32(Session["PayDayId"]) + "", conn1);
conn1.Open();
SqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
payrun.PayDayDate = Convert.ToInt32(dr1["PayDayDate"]);
}
conn1.Close();
}
int day = (int)System.DateTime.Now.Day;
using (SqlConnection conn1 = new SqlConnection(Helpers.DatabaseConnect))
{
SqlCommand cmd1 = new SqlCommand("SELECT * FROM PayRun WHERE PayPeriod= '"+Convert.ToDateTime(enddate)+"'", conn1);
conn1.Open();
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
ViewBag.ErrorMessage = Helpers.Messages.AlreadyProcessSalaryForThisMonth;
return View(payrun);
}
conn1.Close();
}
return RedirectToAction("PayRun");
}
else
{
ViewBag.ErrorMessage = Helpers.Messages.NoPayCodeSelected;
return View(payrun);
}
}