0

如果我在表本身中将其设置为 AutoID,是否需要在 vb 中生成一个新 ID?我目前有

Protected Sub deleteButton_Click(sender As Object, e As System.EventArgs) Handles deleteButton.Click
    Dim deleteSQL As String
    deleteSQL = "DELETE FROM Authors WHERE au_id=@au_id"
    Dim myConnection As New SqlConnection(connectionString)
    Dim myCommand As New SqlCommand(deleteSQL, myConnection)
    myCommand.Parameters.AddWithValue("@au_id", authorDropDownList.SelectedItem.Value)
    Dim successBoolean As Boolean = True
    Dim index As Integer = authorDropDownList.SelectedIndex
    Try
        myConnection.Open()
        successBoolean = myCommand.ExecuteNonQuery
        'authorLabel.Text = "Record Deleted"
        'authorLabel.Visible = True
    Catch ex As Exception
        successBoolean = False
        authorLabel.Text = "Error deleting author. " & ex.Message
        authorLabel.Visible = True
    Finally
        myConnection.Close()
    End Try
    If successBoolean Then
        FillAutherList(index)
        authorDropDownList_SelectedIndexChanged(sender, e)
        authorLabel.Text = "Record Deleted"
        authorLabel.Visible = True
    End If
End Sub


Dim insertSQL As New StringBuilder
    Dim currentDate As String
    currentDate = DateTime.Now.ToString
    insertSQL.Append("INSERT INTO Story_Table (Author,Content,Submission_Date)") 'Inserts new story
    insertSQL.Append(" VALUES (@Author,@Content,@Submission_Date)")             'Sets the story values
    Dim myConnection As New SqlConnection(connectionString)
    Dim myCommand As New SqlCommand(insertSQL.ToString, myConnection)
    With myCommand.Parameters                        'Do this next
        .AddWithValue("@Author", authorTextBox.Text)
        .AddWithValue("@Content", storyTextBox.Text)
        .AddWithValue("@Submission_Date", currentDate)
    End With
    Dim successBoolean As Boolean = True
    Try
        myConnection.Open()
        successBoolean = myCommand.ExecuteNonQuery
        resultLabel.Text = "Thanks for the Story! Look for it on the homepage."
        resultLabel.Visible = True
    Catch ex As Exception
        successBoolean = False
        resultLabel.Text = "Error inserting story. " & ex.Message
        resultLabel.Visible = True
        storyLabel.Text = storyTextBox.Text
        storyLabel.Visible = True
    Finally
        myConnection.Close()
    End Try`
4

0 回答 0