VB.NET 更新 SQL 数据库命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24432558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET updating SQL Database Command
提问by nwestfall
I am building an asp.net web application using vb and .net 2.0. I have a class that handles an object and all its attributes. This class correctly handles reading, creating, and deleting from the database. However, it will not update. I took the update script directly from the Microsoft SQL database, but I cannot figure it out. Here is my SQL command.
我正在使用 vb 和 .net 2.0 构建一个 asp.net web 应用程序。我有一个处理对象及其所有属性的类。此类正确处理从数据库中读取、创建和删除的操作。但是,它不会更新。我直接从 Microsoft SQL 数据库中获取了更新脚本,但我无法弄清楚。这是我的 SQL 命令。
saveCommand = "UPDATE [Database].[Table] SET [name] = @name,[content] = @content,[date_updated] = @dateupdated WHERE ref = @ref"
This is then being put into...
然后将其放入...
Dim setCmd As New SqlCommand(saveCommand, sqlConn)
And then parameter values are put into it. It is weird because the "saveCommand" is determined on whether the item already exists (updates) or doesn't (switches to "INSERT INTO" command WHICH WORKS)
然后将参数值放入其中。这很奇怪,因为“saveCommand”取决于项目是否已经存在(更新)或不存在(切换到“INSERT INTO”命令WHICH WORKS)
More Code
更多代码
Public Class MyClass()
#Region Properties
Public name As String = ""
Public content As String = ""
Public date_updated As DateTime = DateTime.Now
#End Region
Public Sub Save()
Dim sqlConn As New SqlConnection(sqlConnString)
sqlConn.Open()
Dim saveCommand As String
If Me.Exists Then
saveCommand = "UPDATE [Database].[Table] SET [name] = @name,[content] = @content,[date_updated] = @date_updated WHERE ref = @ref"
Else
saveCommand = "INSERT INTO [Database].[Table] ([name],[content],[date_updated],[ref]) VALUES(@name,@content,@date_updated,@ref)"
End If
Dim setCmd As New SqlCommand(saveCommand, sqlConn)
setCmd.Parameters.Add("@name", Data.SqlDbType.VarChar).Value = Me.name
setCmd.Parameters.Add("@content", Data.SqlDbType.VarChar).Value = Me.content
setCmd.Parameters.Add("@date_updated", Data.SqlDbType.DateTime).Value = Me.date_updated
If Me.Exists Then
setCmd.Parameters.Add("@ref", Data.SqlDbType.Int).Value = Me.ID
Else
Dim countCmd As New SqlCommand("SELECT COUNT(*) FROM [DWM-DataSQL].[dbo].[biglots]", sqlConn)
Me.ID = countCmd.ExecuteScalar() + 1
setCmd.Parameters.Add("@ref", Data.SqlDbType.Int).Value = Me.ID
Me.Exists = True
End If
setCmd.ExecuteNonQuery()
sqlConn.Close()
sqlConn.Dispose()
End Sub
Public Sub New()
End Sub
Public Sub New(ByVal num As Integer)
Me.ID = num
End Sub
End Class
This is being called upon by
这是由
myObj = New BLL.MyClass(ref)
'When form is submitted
Public Sub submitForm(ByVal sender As Object, ByVal e As System.EventArgs)
myObj.name = tbName.Text
myObj.content = tbcontent.Text
myObj.Save()
End Sub
回答by nwestfall
I figured it out. It was somehow calling upon the read() function RIGHT before the save() function so it replaced all the values. Thanks Anyway
我想到了。它以某种方式在 save() 函数之前调用 read() 函数,因此它替换了所有值。不管怎么说,还是要谢谢你

