【发布时间】:2017-08-25 22:43:16
【问题描述】:
为什么这么说
mscorlib.dll 中发生了“System.FormatException”类型的未处理异常
附加信息:输入字符串的格式不正确。
这是我的代码
public int transCode = 0;
con.Open();
cmd = new SqlCommand("SELECT MAX(TransactionCode) FROM TRANSACTIONS",con);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
transCode = Convert.ToInt32(rdr[0].ToString()); // Here is the line where the problem go
}
con.Close();
【问题讨论】:
-
你检查过
rdr[0]里面有什么吗? -
TransactionCode在数据库中是什么类型?你为什么要把它转换成字符串呢?如果它已经是int,请使用rdr.GetInt32(0)。如果不是,您必须问自己为什么,因为您想将其转换为一个。 -
最好的调试方法是使用 SQL Server Management Studio (SSMS) 并将查询输入到新的查询视图中。 SSMS 中的调试消息比 c# 好得多。检查 SSMS 中的结果以确保 TransactionCode 值都是整数。
-
您可能没有转换数字。试试Int32.tryParse方法,不直接解析,用dabugger fot show rdr[0]里面是什么。