【发布时间】:2014-11-29 19:53:58
【问题描述】:
我想从数据库表中返回最近插入记录的最后一列。我不断收到此错误:
在 SchoolManagement.exe 中发生了“System.InvalidCastException”类型的第一次机会异常
附加信息:指定的演员表无效。
如果有这个异常的处理程序,程序可以安全地继续
代码:
public int A()
{
string _connection = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
string stmt = "SELECT TOP 1 RegistrationNumber FROM tblStudentBiodata ORDER BY RegistrationNumber DESC";
int count = 0;
using (SqlConnection thisConnection = new SqlConnection(_connection))
{
using(SqlCommand cmdCount = new SqlCommand(stmt, thisConnection))
{
thisConnection.Open();
count = (int)cmdCount.ExecuteScalar();
}
}
return count;
}
【问题讨论】: