我突然开始在我的 PC 上的多个 C# 项目中出现以下错误:
System.Data.dll 中发生“System.OverflowException”类型的未处理异常附加信息:算术运算导致溢出。
我尝试使用 .Net 框架 4、3.5、3 和 4.5 运行我的项目,但总是出现相同的错误。在处理我的 SQL Server 数据库时,在一个项目中使用 dataadapter 填充数据表时总是出现该错误,而在另一个项目中尝试打开连接时总是会出现该错误。这是一个示例代码:
string strConn = "Data Source=Home\\SqlExpress;Initial Catalog=PatientDBNew;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
string sql = string.Format("Select 'file:///' + FolderPath + PhotoName As PhotoPath From Patient Inner Join Photo On Patient.FileNumber = Photo.FileNumber Where Patient.FileNumber = '{0}' And BeforeOrAfter = {1} Order by DateTimeAdded Asc", Patient.FileNumber, beforeOrAfter);
SqlDataAdapter da = new SqlDataAdapter(sql, conn); ;
DataTable dt = null;
try
{
dt = new DataTable();
da.Fill(dt); //**here I get the exception**
}
catch (Exception ex)
{
ErrorLog("MethodName", ex);
}
这是来自另一个项目:
string strConn = "Data Source=Home\\SqlExpress;Initial Catalog=PatientDB;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
string sql = string.Format("Select Replace([Filenumber], 'A', '20'), [Bphoto_issuing_date], [Aphoto_issuing_date] From [Patient_Master] Where FileNumber like 'A%' Order by FileNumber");
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt); //**Here I don't get an exception!**
strConn = "Data Source=MSD-Home;Initial Catalog=PatientDBNew;Integrated Security=True";
conn = new SqlConnection(strConn);
foreach (DataRow item in dt.Rows)
{
string fileNb = item[0].ToString();
string startOfTreatment = item[1].ToString();
string endOfTreatment = item[2].ToString();
sql = "Update Patient Set BPhotoDate = '" + startOfTreatment + "', APhotoDate = '" + endOfTreatment + "' Where FileNumber = '" + fileNb + "'";
using (SqlCommand cmd = new SqlCommand(sql))
{
cmd.Connection = conn;
conn.Open(); //**Here I get the exception**
cmd.ExecuteNonQuery();
conn.Close();
}
}
很明显,在处理 PatientDB 时进展顺利,而在处理 PatientDBNew 时则不然。我试图找出问题所在,但无法。这是我在 PatientDBNew 数据库中的“患者”表结构:
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Patient](
[FileNumber] [varchar](25) NOT NULL,
[PhysicianId] [int] NOT NULL,
[FolderPath] [varchar](MAX) NULL CONSTRAINT [DF_Patient_FolderPath] DEFAULT (''),
[BPhotoDate] [datetime] NULL,
[APhotoDate] [datetime] NULL,
CONSTRAINT [PK_Patient] PRIMARY KEY CLUSTERED
(
[FileNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Patient] WITH CHECK ADD CONSTRAINT [FK_Patient_Physician] FOREIGN KEY([PhysicianId])
REFERENCES [dbo].[Physician] ([PhysicianId])
GO
ALTER TABLE [dbo].[Patient] CHECK CONSTRAINT [FK_Patient_Physician]
GO
FilenNumber 不超过 25 个字符,PhysicianId 始终为 1 或 2,正如我之前所说,直到 2 天前我开始出现此错误时,它一直在运行,没有任何异常。