【问题标题】:ASP.NET, VB.NET can't insert data into a SQL Server databaseASP.NET、VB.NET 无法将数据插入 SQL Server 数据库
【发布时间】:2015-08-21 03:51:27
【问题描述】:

我已经解决了一周的问题。我尝试在 SQL Server 数据库中插入一个值。它没有显示任何错误,但是当我检查数据库时,没有插入数据。我可能会在这里做一些错误的事情,但我找不到。感谢您的帮助。

Dim connect As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlServer").ToString())

Using coa As New SqlCommand()
    With coa
        .Connection = connect
        .CommandType = CommandType.Text
    End With

    Try
       connect.Open()

       Dim insertcmd As String

       insertcmd = "insert into tblUserSection (@newValue, @SectionName, @newSectionID) values ," _
       & "@newValue, @SectionName, @newSectionID);"

       coa.Parameters.Add(New SqlParameter("@newValue", SqlDbType.BigInt))
       coa.Parameters("@newValue").Value = newValue

       coa.Parameters.Add(New SqlParameter("@SectionName", SqlDbType.NVarChar))
       coa.Parameters("@SectionName").Value = SectionName.ToString

       coa.Parameters.Add(New SqlParameter("@newSectionID", SqlDbType.BigInt))
       coa.Parameters("@newSectionID").Value = newSectionID

       coa.ExecuteNonQuery()
       connect.Close()

       MsgBox("success insert")

    Catch ex As Exception
       MsgBox("Fail to Save to database")
    End Try
End Using

【问题讨论】:

  • 不知道为什么看不到异常;该查询字符串看起来完全不正确。
  • 知道如何编写正确的查询吗?
  • 我从 Microsoft 页面编辑示例以适合我的代码。
  • 您可能将数据插入到一个数据库中,然后在另一个数据库中查找。
  • 我已经从其他表中获取数据。我发出警报以打印出该值,它确实显示了该值。问题是,数据库中没有更新。

标签: sql sql-server database vb.net


【解决方案1】:

插入命令不正确。它具有列名和值的参数;参数名称只能用于值。

假设列名与参数名匹配,这里是命令的更新版本。

insertcmd = "insert into tblUserSection (newValue, SectionName, newSectionID) values ," _
        & "@newValue, @SectionName, @newSectionID);"

更奇怪的问题是为什么没有出现错误。那是因为插入语句永远不会被执行。 ExecuteNonQuery 命令针对连接运行,但 insertcmd 永远不会以任何方式与执行相关联。

我建议创建一个 SQLCommand 并使用它来执行查询。这是一个示例(我的代码可能有错误,我的 vb.net 很生锈):

Dim sqlcommand as New SqlCommand(coa)
sqlcommand.text = insertcmd
sqlcommand.type = Text
sqlcommand.Parameters.Add(New SqlParameter("@newValue", SqlDbType.BigInt))
sqlcommand.Parameters("@newValue").Value = newValue
sqlcommand.Parameters.Add(New SqlParameter("@SectionName", SqlDbType.NVarChar))
sqlcommand.Parameters("@SectionName").Value = SectionName.ToString
sqlcommand.Parameters.Add(New SqlParameter("@newSectionID", SqlDbType.BigInt))
sqlcommand.Parameters("@newSectionID").Value = newSectionID
sqlcommand.ExecuteNonQuery()

【讨论】:

  • 加:在 values 关键字之后,多了一个错误的逗号,values 关键字之后的左括号缺失....
  • 它与我所做的相似。仍然没有运气。一切都编译并运行良好。但数据库中没有更新。
【解决方案2】:

您需要在创建插入命令字符串后设置SqlCommand 的CommandText 属性。 喜欢:

Dim connect As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlServer").ToString())
    Using coa As New SqlCommand()
        With coa
            .Connection = connect
            .CommandType = CommandType.Text
        End With
        Try
            connect.Open()
            Dim insertcmd As String
            insertcmd = "insert into [TableName] (newValue, SectionName, newSectionID) values " _
            & "(@newValue, @SectionName, @newSectionID);"
            coa.CommandText = insertcmd
            coa.Parameters.Add(New SqlParameter("@newValue", SqlDbType.BigInt))
            coa.Parameters("@newValue").Value = newValue
            coa.Parameters.Add(New SqlParameter("@SectionName", SqlDbType.NVarChar))
            coa.Parameters("@SectionName").Value = SectionName.ToString()
            coa.Parameters.Add(New SqlParameter("@newSectionID", SqlDbType.BigInt))
            coa.Parameters("@newSectionID").Value = newSectionID
            coa.ExecuteNonQuery()
            connect.Close()
            MsgBox("success insert")
        Catch ex As Exception
            MsgBox("Fail to Save to database")
        End Try
    End Using

【讨论】:

  • @IsmaelBinAzman 使用整个代码,这在我的最后工作正常。我在您的查询字符串中发现了一些问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2012-01-31
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-10
相关资源
最近更新 更多