标量变量问题存在问题。输出说代码需要声明fname标量变量。代码如下所示。任何帮助将不胜感激。
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim fname As String = firstname.Text
Dim strSurname As String = surname.Text
Dim strAddress As String = address.Text
Dim strGraveType As String
Dim GraveNo1 As String
Dim GraveNo2 As String
Dim GraveNo3 As String
Dim filename As String
Dim photoFilePath As String
If Not radSingle.Checked Or radDouble.Checked Or radTreble.Checked Then
MsgBox("Please Select a Grave Type")
ElseIf radSingle.Checked Then
strGraveType = "Single"
ElseIf radDouble.Checked Then
strGraveType = "Double"
ElseIf radTreble.Checked Then
strGraveType = "Treble"
End If
GraveNo1 = txtGraveNo1.Text
GraveNo2 = txtGraveNo2.Text
GraveNo3 = txtGraveNo3.Text
If GravePhotoUpload.HasFile Then
If GravePhotoUpload.PostedFile.ContentType = "image/jpeg" Then
Try
filename = Path.GetFileName(GravePhotoUpload.FileName)
GravePhotoUpload.SaveAs(Server.MapPath("../Images/") + filename)
photoFilePath = "Images/" + filename
Catch ex As Exception
MsgBox("ERROR - The file could not be uploaded. The following error occured: " + ex.Message)
End Try
Else
MsgBox("Please upload a JPG File Format")
End If
End If
Dim cmdstring As String = "INSERT INTO DeathRecords(FirstName, Surname, Address, GraveType, GraveNo1, GraveNo2, GraveNo3, GravePhoto) Values (@Fname, @strSurname, @strAddress, @strGraveType, @GraveNo1, @GraveNo1, @GraveNo3, @photoFilePath)"
conn = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DrumcreeCemetery.mdf;Integrated Security=True")
cmd = New SqlCommand(cmdstring, conn)
cmd.Parameters.AddWithValue("@FirstName", fname)
cmd.Parameters.AddWithValue("@Surname", strSurname)
cmd.Parameters.AddWithValue("@Address", strAddress)
cmd.Parameters.AddWithValue("@GraveType", strGraveType)
cmd.Parameters.AddWithValue("@GraveNo1", GraveNo1)
cmd.Parameters.AddWithValue("@GraveNo2", GraveNo2)
cmd.Parameters.AddWithValue("@GraveNo3", GraveNo3)
cmd.Parameters.AddWithValue("@GravePhoto", photoFilePath)
Try
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
答案 0 :(得分:0)
查询中定义的参数:
Dim cmdstring As String = "INSERT INTO DeathRecords(FirstName, Surname, Address, GraveType, GraveNo1, GraveNo2, GraveNo3, GravePhoto) Values (@Fname, @strSurname, @strAddress, @strGraveType, @GraveNo1, @GraveNo1, @GraveNo3, @photoFilePath)"
与您在命令中设置的参数不匹配:
cmd.Parameters.AddWithValue("@FirstName", fname)
cmd.Parameters.AddWithValue("@Surname", strSurname)
cmd.Parameters.AddWithValue("@Address", strAddress)
cmd.Parameters.AddWithValue("@GraveType", strGraveType)
cmd.Parameters.AddWithValue("@GraveNo1", GraveNo1)
cmd.Parameters.AddWithValue("@GraveNo2", GraveNo2)
cmd.Parameters.AddWithValue("@GraveNo3", GraveNo3)
cmd.Parameters.AddWithValue("@GravePhoto", photoFilePath)
例如,您的查询对于FirstName有@Fname
,但您要为FirstName参数指定@FirstName
。大多数其他变量也是如此。如果Command中给定的参数名称与查询中的参数名称不匹配,则Command无法将您指定的参数链接到查询所期望的内容,从而导致您获得的错误。