Asp.net 通过vb.net向Access数据库添加新记录
如果我在表中将其设置为AutoID,是否需要在vb中生成新ID? 我现在有Asp.net 通过vb.net向Access数据库添加新记录,asp.net,sql,vb.net,Asp.net,Sql,Vb.net,如果我在表中将其设置为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 Ne
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`
你在没有解释的情况下发布了大量代码,这到底是什么问题?我正在尝试将一条新记录插入Access表,该表将显示在另一页上。我很难弄清楚autoID在vb中是如何工作的。我需要生成一个新的ID吗?或者它会为我生成一个新ID吗?我想我的问题很清楚,但我会再试一次。您在问题中发布了大量代码。你有什么问题吗?(是否正常工作?是否有错误?运行时会发生什么?)您发布了一个通用的“我是否需要生成一个新的ID…?”问题,其中包含几十行代码,但没有说明包含代码的目的-我试图找出为什么您觉得有必要在没有解释的情况下发布所有代码。您的问题只是关于插入数据,因此唯一相关的部分似乎是
insertSQL
部分,但这还不清楚。对不起,我是新手。这给了我一个错误,即“尝试为文件C:\Users\dclifford14\Documents\Visual Studio 2010\WebSites\FinalProject\App\u Data\Final\u Project\u database.accdb附加自动命名数据库失败。存在同名数据库,或无法打开指定文件,或位于UNC共享上。”如何创建表的新条目?请阅读错误消息。它与向表中添加条目无关。错误明确表示“存在同名数据库,或无法打开指定的文件,或该文件位于UNC共享上”。这不是插入数据的问题,而是打开数据库的问题。非常重要的一点是,你要实际阅读错误消息中包含的单词,而不是仅仅假设你知道是什么导致了问题。(关于获取错误的信息和错误消息应该添加到问题中,而不是放在评论中,以便人们可以看到。)