Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查护照号码是否存在?_C# - Fatal编程技术网

C# 如何检查护照号码是否存在?

C# 如何检查护照号码是否存在?,c#,C#,我想检查护照号码是否存在 在我使用此代码检查整数是否存在之前 但是MSSQL类型varchar(50)中的passport number列 我试过的 1-创建存储过程以读取ID号: create proc [dbo].[VALIDATE_PATIENT_IDNO] @patient_id varchar(50) as select Patient_id from Patients where Patient_id = @patient_id 2-我在C#中创建了此代码,以验证id是否不存在

我想检查护照号码是否存在

在我使用此代码检查整数是否存在之前

但是MSSQL类型varchar(50)中的passport number列

我试过的

1-创建存储过程以读取ID号:

create proc [dbo].[VALIDATE_PATIENT_IDNO]

@patient_id varchar(50)
as 
select Patient_id from Patients
where Patient_id = @patient_id
2-我在C#中创建了此代码,以验证id是否不存在:

public int? VALIDATE_PATIENT_IDNO(string patient_id)
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DataTable dt = new DataTable();
            SqlParameter[] Param = new SqlParameter[1];

            Param[0] = new SqlParameter("@patient_id", SqlDbType.VarChar,50);
            Param[0].Value = patient_id;

            dt = DAL.SelectData("VALIDATE_PATIENT_IDNO", Param);
            DAL.close();

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                int? patientNumber = row.Field<int>("patient_id");
                return patientNumber;
            }

            // return null otherwise
            return null;
        }
4-我在步骤2中的代码中出现错误:

Additional information: Specified cast is not valid.
int? patientNumber = row.Field<int>("patient_id");
其他信息:指定的强制转换无效。
智力?patientNumber=行字段(“患者id”);

如何更改步骤2中的代码并解决此错误并检查字符串值not int

抛开命名混乱(passportnumber vs patientid),您可能不想返回找到的patientid(因为您已经知道它们,因为它们是您选择条件的一部分),而是要返回计数

此外,您的patientid似乎是一个字符串,但在结果中您尝试将其转换为整数。这是不可能的,因此出现了错误

您可以尝试以下操作:

create proc [dbo].[VALIDATE_PATIENT_IDNO]

@patient_id varchar(50)
as 
select count(Patient_id) as patientcount from Patients
where Patient_id = @patient_id
假设
patient\u id
是表的主键,如果存在具有给定id的行,则返回
1
,否则返回
0

那你就可以了

int? patientNumber = row.Field<int>("patientcount");

尝试
row.Field
,然后将其转换为
int
?不要在
varchar
字段中存储数字。将它们存储在
int
至少在我居住的地方,护照“编号”由字母(字母顺序)和数字组成。护照号码可以被视为
int
值,这可能不是一个好的假设。对于int,您还可以丢弃前导零。也许所有这些都解释了为什么它被存储为varchar。@Charlieface我倾向于同意,但有时前导零很重要。同意,但这里为什么OP转换为
int
,显然它是
int
?诚然,这是护照号码,所以你是对的。谢谢你这么多天才和解决方案,如果我能给你1000票,我会做的,:):)
int? patientNumber = row.Field<int>("patientcount");
bool patientExists = patientNumber > 0;