我似乎无法在像这样的其他问题中找到正确的答案。
我正在使用这段代码来分配应该保存在数据库中的日期。
request.BranchRequestDate = DateTime.Now;
当我尝试使用此函数从表中检索数据时。
public Request SelectByID()
{
try
{
SqlCommand command = this.GetParametizedCommand("SELECT * FROM Request WHERE RequestUID = '" + this.RequestUID.ToString() + "' ;");
command.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable tbl = new DataTable();
adapter.Fill(tbl); //This is where the exception is thrown
return ToEntity(tbl);
}
catch
{
return null;
}
}
...它抛出异常。
我已经尝试检查所有内容,但我无法弄清楚它为什么会抛出和异常,因为我看不到检索到的数据在 C# 中的样子,但我已经尝试了 SQL Server 本身中的语句和它返回正确的数据。
任何想法?
编辑:我还将它与同一程序中的类似功能进行了比较,它运行得很好。
编辑:GetParametizedCommand 方法的代码
public SqlCommand GetParametizedCommand(String commandText)
{
try
{
SqlCommand command = base.GetSqlCommand(commandText);
if(SavingStatus == Util.SavingStatus.INSERT)
command.Parameters.Add(new SqlParameter("@RequestUID", SqlDbType.Int,32, ParameterDirection.Output, false, 38, 38, "RequestUID", DataRowVersion.Default, this.RequestUID));
else if (SavingStatus == Util.SavingStatus.UPDATE || SavingStatus == Util.SavingStatus.DELETE)
command.Parameters.Add(new SqlParameter("@RequestUID", SqlDbType.Int,32, ParameterDirection.Input, false, 38, 38, "RequestUID", DataRowVersion.Default, this.RequestUID));
command.Parameters.AddWithValue("@DisplayID", this.DisplayID);
command.Parameters.AddWithValue("@BranchUID", this.BranchUID);
command.Parameters.AddWithValue("@EmployeeUID", this.EmployeeUID);
command.Parameters.AddWithValue("@BranchRequestDate", this.BranchRequestDate);
command.Parameters.AddWithValue("@IsDeleted", this.IsDeleted);
return command;
}
catch
{
return null;
}
}