将接收到的信息写入数据库的两个表pacs_images和dengji表中,逻辑是:当pacs_images表满足"select count() from pacs_images where StudyUID='"+studyUid+"' and DicomName='"+DicomName2+"'"为0个,且dengji表满足"select count() from dengji where 检查UID='"+studyUid+"'"为0个时,执行两个表的添加操作。当dengji表的上述条件结果不为0,但pacs_images表的上述条件结果为0时,执行pacs_images表的添加操作,dengji表的更新操作。其它情况,做dengji表的更新操作。按以下程序执行下来,好像只能执行到第一个条件(if后面的),elseif和else后面的代码不能执行,而且在执行if块的两个表的添加操作时报错数据库错误:引发的异常:“MySql.Data.MySqlClient.MySqlException”(位于 mscorlib.dll 中)
数据库异常: Duplicate entry '1.2.156.112605.13' for key '检查UID'
注:'检查UID'字段是dengji表中唯一值,我将唯一值取消后,dengji表中出现很多相同条目,肯定不行。
查找原因一周了,无解。
string commandText = "select count(*) from dengji where 检查UID='"+studyUid+"'";
MySqlCommand mYSqlCommand = new MySqlCommand(commandText,conn);
//返回object类型第一行第一列
object obj=mYSqlCommand.ExecuteScalar();
//查询本读数据库pacs_images表
string commandText2 = "select count(*) from pacs_images where StudyUID='"+studyUid+"' and DicomName='"+DicomName2+"'";
//查无此数据
MySqlCommand mYSqlCommand2 = new MySqlCommand(commandText2, conn);
object obj2 = mYSqlCommand2.ExecuteScalar();
//表pacs_study和pacs_images都无数据
try
{
int value1 = int.Parse(obj.ToString());
int value2 = int.Parse(obj2.ToString());
// 如果 value1 等于 0 并且 value2 等于 0
if (value1 == 0 && value2 == 0)
{
// 插入到 pacs_images 表
string commandText3 = "INSERT INTO pacs_images(StudyUID, DicomName) VALUES(@studyUid, @DicomName2)";
using (MySqlCommand mYSqlCommand3 = new MySqlCommand(commandText3, conn))
{
mYSqlCommand3.Parameters.AddWithValue("@studyUid", studyUid);
mYSqlCommand3.Parameters.AddWithValue("@DicomName2", DicomName2);
mYSqlCommand3.ExecuteScalar(); // 使用 ExecuteNonQuery 而不是 ExecuteScalar
}
// 插入到 dengji 表
string commandText4 = "INSERT INTO dengji(检查UID,部位,诊断意见2,照片号,姓名,性别,年龄,检查时间,检查日期,shebei,DicomCount, 图像路径, Status, 接收时间) VALUES(@studyUid, @StudyDescription, @InstitutionName,@patID,@patName,@patSex,@patAge,@studyDateTime,@StudyDate,@dicomModality,'1', @dicomLoad1, '1', @currentTime)";
using (MySqlCommand mYSqlCommand4 = new MySqlCommand(commandText4, conn))
{
mYSqlCommand4.Parameters.AddWithValue("@studyUid", studyUid);
mYSqlCommand4.Parameters.AddWithValue("@StudyDescription", patientInfo.StudyDescription);
mYSqlCommand4.Parameters.AddWithValue("@InstitutionName", patientInfo.InstitutionName);
mYSqlCommand4.Parameters.AddWithValue("@patID", patientInfo.patID);
mYSqlCommand4.Parameters.AddWithValue("@patName", patientInfo.patName);
mYSqlCommand4.Parameters.AddWithValue("@patSex", patientInfo.patSex);
mYSqlCommand4.Parameters.AddWithValue("@patAge", patientInfo.patAge);
mYSqlCommand4.Parameters.AddWithValue("@studyDateTime", patientInfo.studyDateTime);
mYSqlCommand4.Parameters.AddWithValue("@StudyDate", patientInfo.StudyDate);
mYSqlCommand4.Parameters.AddWithValue("@dicomModality", dicomModality);
mYSqlCommand4.Parameters.AddWithValue("@dicomLoad1", dicomLoad1);
mYSqlCommand4.Parameters.AddWithValue("@currentTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
mYSqlCommand4.ExecuteNonQuery();
}
// 等待(如果需要)
System.Threading.Thread.Sleep(10);
}
// 如果 value1 不等于 0,但是 value2 等于 0
else if (value2 == 0)
{
// 插入到 pacs_images 表
string commandText3 = "INSERT INTO pacs_images(StudyUID, DicomName) VALUES(@studyUid, @DicomName2)";
using (MySqlCommand mYSqlCommand3 = new MySqlCommand(commandText3, conn))
{
mYSqlCommand3.Parameters.AddWithValue("@studyUid", studyUid);
mYSqlCommand3.Parameters.AddWithValue("@DicomName2", DicomName2);
mYSqlCommand3.ExecuteScalar(); // 使用 ExecuteNonQuery
}
// 更新 dengji 表
string commandText4 = "UPDATE dengji SET DicomCount = DicomCount + 1, 图像路径 = @dicomLoad1, 接收时间 = @currentTime, Status = '2' WHERE 检查UID = @studyUid";
using (MySqlCommand mYSqlCommand4 = new MySqlCommand(commandText4, conn))
{
mYSqlCommand4.Parameters.AddWithValue("@studyUid", studyUid);
mYSqlCommand4.Parameters.AddWithValue("@dicomLoad1", dicomLoad1);
mYSqlCommand4.Parameters.AddWithValue("@currentTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
mYSqlCommand4.ExecuteNonQuery();
}
}
// 如果两个条件都不满足(即 value1 != 0 && value2 != 0)
else
{
// 更新 dengji 表
string commandText4 = "UPDATE dengji SET 图像路径 = @dicomLoad1, 接收时间 = @currentTime, Status = '' WHERE 检查UID = @studyUid";
using (MySqlCommand mYSqlCommand4 = new MySqlCommand(commandText4, conn))
{
mYSqlCommand4.Parameters.AddWithValue("@studyUid", studyUid);
mYSqlCommand4.Parameters.AddWithValue("@dicomLoad1", dicomLoad1);
mYSqlCommand4.Parameters.AddWithValue("@currentTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
mYSqlCommand4.ExecuteNonQuery();
}
}
}