处理FileNotFound异常

时间:2016-05-07 19:31:57

标签: vb.net sql-server-2008

cn.Open()
    Dim query As String
    query = "INSERT INTO Documents(AdmissionNumber,FullName,LeavingCertificate,KCPEResultSlip,BirthCertificate,MedicalCertificate,ParentOrGuardianPhoto,ParentGuardianIDFront,ParentGuardianIDBack,AnyOtherDocument) VALUES('" & Tbx1.Text & "','" & Tbx2.Text & "', @LeavingCertificate,@KCPEResultSlip,@BirthCertificate,@MedicalCertificate,@ParentOrGuardianPhoto,@ParentGuardianIDFront,@ParentGuardianIDBack,@AnyOtherDocument)"
    cmd = New SqlCommand(query, cn)
    cmd.Parameters.Add(New SqlClient.SqlParameter("@LeavingCertificate", SqlDbType.Image)).Value = IO.File.ReadAllBytes(OpenFileDialog1.FileName)
    cmd.Parameters.Add(New SqlClient.SqlParameter("@KCPEResultSlip", SqlDbType.Image)).Value = IO.File.ReadAllBytes(OpenFileDialog2.FileName)
    cmd.Parameters.Add(New SqlClient.SqlParameter("@BirthCertificate", SqlDbType.Image)).Value = IO.File.ReadAllBytes(OpenFileDialog3.FileName)
    cmd.Parameters.Add(New SqlClient.SqlParameter("@MedicalCertificate", SqlDbType.Image)).Value = IO.File.ReadAllBytes(OpenFileDialog4.FileName)
    cmd.Parameters.Add(New SqlClient.SqlParameter("@ParentOrGuardianPhoto", SqlDbType.Image)).Value = IO.File.ReadAllBytes(OpenFileDialog5.FileName)
    cmd.Parameters.Add(New SqlClient.SqlParameter("@ParentGuardianIDFront", SqlDbType.Image)).Value = IO.File.ReadAllBytes(OpenFileDialog6.FileName)
    cmd.Parameters.Add(New SqlClient.SqlParameter("@ParentGuardianIDBack", SqlDbType.Image)).Value = IO.File.ReadAllBytes(OpenFileDialog7.FileName)
    cmd.Parameters.Add(New SqlClient.SqlParameter("@AnyOtherDocument", SqlDbType.Image)).Value = IO.File.ReadAllBytes(OpenFileDialog8.FileName)

    Reader = cmd.ExecuteReader
    MsgBox("Students' Documents Added Successfully to The Regista.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
    Btn9_Click(sender, e)


End Sub 

'嗨同志们。你在上面看到这个代码?这个名为(AnyOtherDocument)的项是可选的。用户可以提供或不提供。但是如果没有提供,我会收到异常错误。我该如何处理?

1 个答案:

答案 0 :(得分:0)

只需查看File.Exists即可。如果没有文件,则将参数设置为DbNull.Value,否则读取文件

Dim exists = File.Exists(OpenFileDialog8.FileName)

.....
cmd.Parameters.Add(New SqlClient.SqlParameter( _ 
     "@AnyOtherDocument", SqlDbType.Image)).Value _
     = If(exists, IO.File.ReadAllBytes(OpenFileDialog8.FileName), _
                  DBNull.Value)

顺便说一句,您使用的是所有图像值的参数,为什么不将它们用于两个字符串值?参数的有用性与要传递的值的DataType无关。您可以将它们用于每个值以避免Sql Injection和解析问题

query = "INSERT INTO Documents " & _
"(AdmissionNumber,FullName,LeavingCertificate,KCPEResultSlip," & _ 
"BirthCertificate,MedicalCertificate,ParentOrGuardianPhoto," & _
"ParentGuardianIDFront,ParentGuardianIDBack,AnyOtherDocument) " & _
"VALUES(@admnum, @fullname,  @LeavingCertificate,@KCPEResultSlip, " & _
"@BirthCertificate,@MedicalCertificate,@ParentOrGuardianPhoto," & _
"@ParentGuardianIDFront,@ParentGuardianIDBack,@AnyOtherDocument)"

cmd = New SqlCommand(query, cn)
cmd.Parameters.Add("@admnum", SqlDbType.NVarChar).Value = Tbx1.Text 
cmd.Parameters.Add("@fullname", SqlDbType.NVarChar).Value = Tbx2.Text 
cmd.Parameters.Add("@LeavingCertificate", SqlDbType.Image).Value = IO.File.ReadAllBytes(OpenFileDialog1.FileName)
.... and so on with all the other parameters ....