简体   繁体   English

ASP.NET,VB.NET无法将数据插入SQL Server数据库

[英]ASP.NET, VB.NET can't insert data into a SQL Server database

I have been troubleshooting this for a week. 我已经对此进行了一周的故障排除。 I try to insert a value into a SQL Server database. 我尝试将值插入SQL Server数据库。 It doesn't show any error but when I check the database, there's no data inserted. 它没有显示任何错误,但是当我检查数据库时,没有插入任何数据。 I might doing something that is wrong here but I can't find it. 我可能在这里做错了什么,但找不到。 Thanks for helping. 感谢您的帮助。

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

The insert command is incorrect. 插入命令不正确。 It has parameters for both the column names and the value; 它具有用于列名和值的参数; the parameter names should only be used for the values. 参数名称应仅用于值。

Assuming the column names match the parameter names, here's an updated version of the command. 假设列名与参数名匹配,这是命令的更新版本。

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

The more curious question is why isn't an error showing up. 更奇怪的问题是为什么没有出现错误。 That's because the insert statement is never getting executed. 这是因为insert语句永远不会执行。 The ExecuteNonQuery command is run against the connection but insertcmd is never associated with the execution in any way. ExecuteNonQuery命令针对该连接运行,但是insertcmd决不会以任何方式与执行关联。

I'd recommend creating a SQLCommand and using that to execute the query. 我建议创建一个SQLCommand并使用该命令执行查询。 Here's a sample (and my code might have mistakes, my vb.net is pretty rusty): 这是一个示例(我的代码可能有错误,我的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()

You need to set CommandText property of SqlCommand after creating the insert command string. 创建插入命令字符串后,需要设置SqlCommand的 CommandText属性。 like: 喜欢:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM