1.参数@s_id没有默认值2.无效的SQL语句

时间:2014-05-21 16:47:40

标签: sql vb.net visual-studio parameters

因此,当我将datagridview的值加载到文本框并点击删除时,我的第一个问题就出现了,出现错误:

the parameter @id has no default value

其次,当我再次从我的datagridview加载我的值到我的文本框并单击更新时,会出现错误:

Invalid SQL statement

对不起。关于visual basic,我只是个新手。我正在使用access作为我的数据库。有人可以解释并提供解决方案吗?对不起。

Imports System.Data.OleDb
Imports System.IO
Public Class Form1

Public sEditType As String = String.Empty


Private Sub Save_Record()
    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim sSQL As String = String.Empty

    Dim inputOk As Boolean = False

    Do Until inputOk = True

        Try
            'get connection string declared in the Module1.vb and assing it to conn variable
            conn = New OleDbConnection(Get_Constring)
            conn.Open()
            cmd.Connection = conn
            cmd.CommandType = CommandType.Text

            'I just use the textbox tag property to idetify if the data is new or existing.
            If Me.tbFirstName.Tag = 0 Then
                sSQL = "INSERT INTO tbl_test ( first_namedb, last_namedb)"
                sSQL = sSQL & "  VALUES(@first_namedb, @last_namedb)"
                cmd.CommandText = sSQL


            End If
            cmd.Parameters.Add("@first_namedb", OleDbType.VarChar).Value = IIf(Len(Trim(Me.tbFirstName.Text)) > 0, Me.tbFirstName.Text, DBNull.Value)
            cmd.Parameters.Add("@last_namedb", OleDbType.VarChar).Value = IIf(Len(Trim(Me.tbLastName.Text)) > 0, Me.tbLastName.Text, DBNull.Value)
            cmd.Parameters.Add("@s_id", OleDbType.Numeric).Value = Me.tbFirstName.Tag
            cmd.ExecuteNonQuery()

            If Me.tbFirstName.Tag = 0 Then
                cmd.CommandText = "Select @@Identity"
                'Set textbox tag property with the ID of new record
                Me.tbFirstName.Tag = cmd.ExecuteScalar()
            End If

            MsgBox("Data has been save.")
            inputOk = True
        Catch ex As Exception
            MsgBox(ErrorToString)

        End Try
    Loop

End Sub

Private Sub Delete_Record()
    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim sSQL As String = String.Empty


    Try
        'get connection string declared in the Module1.vb and assing it to conn variable
        conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text

        cmd.CommandText = "delete from tbl_test where student_ID = @s_id"

        cmd.Parameters.Add("@s_id", OleDbType.Numeric).Value = Me.tbFirstName.Tag
        cmd.ExecuteNonQuery()

        MsgBox("Data has been deleted.")

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try
End Sub

Private Sub Update_Record()
    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim sSQL As String = String.Empty
    Try
        'get connection string declared in the Module1.vb and assing it to conn variable
        conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text

        'I just use the textbox tag property to idetify if the data is new or existing.
        If Me.tbFirstName.Tag = 0 Then
            sSQL = "UDPATE tbl_test set first_namedb = @first_namedb, last_namedb = @last_namedb where student_ID = @s_id"
            cmd.CommandText = sSQL
        End If
        cmd.Parameters.Add("@first_namedb", OleDbType.VarChar).Value = IIf(Len(Trim(Me.tbFirstName.Text)) > 0, Me.tbFirstName.Text, DBNull.Value)
        cmd.Parameters.Add("@last_namedb", OleDbType.VarChar).Value = IIf(Len(Trim(Me.tbLastName.Text)) > 0, Me.tbLastName.Text, DBNull.Value)
        cmd.Parameters.Add("@s_id", OleDbType.Numeric).Value = Me.tbFirstName.Tag
        cmd.ExecuteNonQuery()
        MsgBox("Data has been updated.")
    Catch ex As Exception
        MsgBox(ErrorToString)

    End Try
End Sub

Private Sub Set_Button(ByVal iMode As Short)
    Select Case iMode
        Case 1

        Case 2

        Case 3

    End Select

End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim res As New DialogResult

    res = MsgBox("Do you want to save data?", MsgBoxStyle.YesNo, "Sample")
    If res = DialogResult.Yes Then
        Save_Record()
        'Set_Button(2)
    End If
End Sub

Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
    Dim conn As New OleDbConnection
    'Dim cmd As New OleDbCommand
    Dim da As New OleDbDataAdapter
    Dim dt As New DataTable
    Dim sSQL As String = String.Empty
    'get connection string declared in the Module1.vb and assing it to conn variable

    conn = New OleDbConnection(Get_Constring)
    conn.Open()
    'cmd.Connection = conn
    'cmd.CommandType = CommandType.Text

    sSQL = "Select * from tbl_test"
    'cmd.CommandText = sSQL
    'da.SelectCommand = cmd
    da = New OleDb.OleDbDataAdapter(sSQL, conn)
    da.Fill(dt)

    Me.DataGridView1.DataSource = dt


End Sub

Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

    tbFirstName.Text = DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value
    tbLastName.Text = DataGridView1.Item(2, DataGridView1.CurrentRow.Index).Value

End Sub




Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    Update_Record()
End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    Delete_Record()
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您的删除程序缺少一步:

cmd.CommandText = "delete from tbl_test where student_ID = @s_id"
cmd.Parameters.Add("@s_id", OleDbType.Numeric).Value = Me.tbFirstName.Tag 

如果Me.tbFirstName.Tag为0,Null或Nothing,您将收到no default value错误,因为您未指定有效ID且相关列没有默认值。

不确定您的更新问题是否有问题,因为相关代码似乎不存在,并且每个帖子只有一个问题。