使用vb.net插入访问时,数据似乎为“0”

时间:2015-09-17 03:40:49

标签: vb.net

我遇到的问题是,当我将数据插入数据库时​​,值都是0。

我的列是StudentID,IntakeCode,FirstName,LastName,Contactnumber,emailaddress和unitID。

这是第一种形式

    If txtunitid.Text <> "" And txttp.Text <> "" Then

        cmdInsert.CommandText = "INSERT INTO StudentAcc (StudentID, UnitID) VALUES ('" & txttp.Text & "', '" & txtunitid.Text & "');"
        cmdInsert.CommandType = CommandType.Text
        cmdInsert.Connection = cnnOLEDB
        cmdInsert.ExecuteNonQuery()

        cmdUpdate.CommandText = "UPDATE Accommodation SET Status = True WHERE Unit_ID = " & txtunitid.Text & ";"
        cmdUpdate.CommandType = CommandType.Text
        cmdUpdate.Connection = cnnOLEDB
        cmdUpdate.ExecuteNonQuery()
        obj.Show()
        MsgBox(txtunitid.Text & " " & "Record updated.")

        Me.Hide()
    Else
        MsgBox("Hello")
    End If
    cmdUpdate.Dispose()

这是第二种形式

    If intakecodetxt.Text <> "" And firstnametxt.Text <> "" And lastnametxt.Text <> "" And MaskedTextBox1.Text <> "" And emailtxt.Text <> "" And IsNumeric(firstnametxt.Text) = False And IsNumeric(lastnametxt.Text) = False Then
        cmdUpdate.CommandText = "UPDATE Students SET IntakeCode = '" & intakecodetxt.Text & "', FirstName = '" & firstnametxt.Text & "', LastName = '" & lastnametxt.Text & "', ContactNumber = '" & MaskedTextBox1.Text & "', Email_Address = '" & emailtxt.Text & "' WHERE StudentID = '" & studentidtxt.Text & "';"

        cmdUpdate.CommandType = CommandType.Text
        cmdUpdate.Connection = cnnOLEDB
        cmdUpdate.ExecuteNonQuery()
        MsgBox(studentidtxt.Text & " " & "Record updated.")
    Else
        MsgBox("Please Enter Value")
    End If
    cmdInsert.Dispose()

1 个答案:

答案 0 :(得分:0)

在第一个语句中,您似乎试图将StudentId和UnitId字段包装在引号中,表示它们是字符值,而在第二个语句中,您似乎将UnitId引用为数值。我怀疑它是一个数字字段,你传递它的字符串被转换为零。