我有一个Web API调用,在这里我将对象列表发送到存储的proc。我正在创建要发送到存储过程的数据表,但是当我尝试将其添加到数据表时,它立即返回500状态。
private static DataTable CreateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Project_Name", typeof(int));
dt.Columns.Add("Project_Staffing_Period", typeof(DateTime));
dt.Columns.Add("Employment_Start_Date", typeof(DateTime));
dt.Columns.Add("Employment_Rate_Type_Code", typeof(string));
dt.Columns.Add("Employment_Rate_Effective_Date", typeof(DateTime));
dt.Columns.Add("Company_ID", typeof(int));
dt.Columns.Add("Person_ID", typeof(int));
dt.Columns.Add("Project_Role_Type_Code", typeof(string));
dt.Columns.Add("Staffed_Hours", typeof(decimal));
dt.Columns.Add("CreateID", typeof(string));
dt.Columns.Add("CreateTS", typeof(DateTime));
dt.Columns.Add("UpdateID", typeof(string));
dt.Columns.Add("UpdateTS", typeof(DateTime));
return dt;
}
这是我的数据表格式。
public class ProjectStaffing
{
public string ProjectName { get; set; }
public DateTime ProjectStaffingPeriod { get; set; }
public DateTime EmploymentStartDate { get; set; }
public string EmploymentRateTypeCode { get; set; }
public DateTime EmploymentRateEffectiveDate { get; set; }
public int CompanyID { get; set; }
public int PersonID { get; set; }
public string ProjectRoleTypeCode { get; set; }
public decimal StaffedHours { get;set; }
public string CreateID { get; set; }
public string UpdateID { get; set; }
public DateTime CreateTS { get; set; }
public DateTime UpdateTS { get; set; }
这是我的课程格式
public async Task<string> CreateProjectStaffingByDateRange(List<ProjectStaffing> projectstaffings)
{
string statusMessage;
DataTable dt = CreateTable();
for(int i = 0; i < projectstaffings.Count; i++)
{
dt.Rows.Add(projectstaffings[i].ProjectName, projectstaffings[i].ProjectStaffingPeriod,
projectstaffings[i].EmploymentStartDate, projectstaffings[i].EmploymentRateTypeCode,
projectstaffings[i].EmploymentRateEffectiveDate, projectstaffings[i].CompanyID,
projectstaffings[i].PersonID, projectstaffings[i].ProjectRoleTypeCode, projectstaffings[i].StaffedHours,
projectstaffings[i].CreateID, projectstaffings[i].CreateTS, projectstaffings[i].UpdateID, projectstaffings[i].UpdateTS);
}
using (SqlConnection conn = new SqlConnection(Connection))
{
conn.Open();
SqlParameter returnValue; //Holds the bit that determines if insert was successful or not
SqlCommand command;
returnValue = new SqlParameter();
command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "USP_Add_Project_Staffing_By_Date_Range";
command.Parameters.AddWithValue("@TempTable", dt);
command.Parameters.Add(new SqlParameter("@statusMessage", SqlDbType.NVarChar, -1)).Direction = ParameterDirection.Output;
returnValue.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnValue);
await command.ExecuteNonQueryAsync().ConfigureAwait(false);
statusMessage = command.Parameters["@statusMessage"].Value.ToString().Trim();
}
return statusMessage;
}
这是我给服务器的电话。而我将行添加到数据表的地方是它失败并返回500状态。
我是否将数据表错误地插入?我匹配了数据类型。我已经检查了要插入的顺序。所有值都不为空。
一些样本值可能是
ProjectName: "Project 1"
ProjectStaffingPeriod: "Oct 4, 2019"
EmploymentStartDate: "Oct 4, 2019"
EmploymentRateTypeCode: "NA Rate"
EmploymentRateEffectiveDate: "Oct 4, 2019"
CompanyID: 1
PersonID: 1
ProjectRoleTypeCode: "BA"
StaffedHours: 8.0
CreateID: "person1"
UpdateID: "person1"
CreateTS: "Oct 4, 2019 11:00 AM"
UpdateTS: "Oct 4, 2019 11:00 AM"
我不知道其余的设置,但是您似乎将列项目名称作为int添加到数据表中
dt.Columns.Add("Project_Name", typeof(int));
但这是你班上的一个字符串
public string ProjectName { get; set; }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句