2015-08-21 200 views
0

我一直在为此排查一个星期。我尝试将一个值插入到SQL Server数据库中。它不显示任何错误,但是当我检查数据库时,没有插入任何数据。我可能在这里做错了什么,但我找不到它。感谢您的帮助。ASP.NET,VB.NET无法将数据插入到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 
+0

不知道为什么你不会看到一个异常;该查询字符串看起来不正确。 –

+0

任何想法如何写一个正确的查询? –

+0

我编辑来自Microsoft页面的示例以符合我的代码。 –

回答

1

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

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

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

更奇怪的问题是为什么不出现错误。这是因为insert语句永远不会被执行。 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() 
+0

Plus:**在** values的关键字之后,会有一个额外的错误逗号,并且'values'关键字后面的后括号缺失.... –

+0

与我所做的类似。仍然没有运气。一切编译并运行良好。但是在数据库中没有更新。 –

0

您需要设置的CommandText财产的SqlCommand创建插入命令字符串之后。 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 
+0

我插入命令文本。但仍然不工作。 –

+0

@IsmaelBinAzman使用这个完整的代码,在我的最后工作正常。我在你的查询字符串中发现了一些问题。 –

相关问题