我正在使用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"
然后将其放入......
Dim setCmd As New SqlCommand(saveCommand, sqlConn)
然后将参数值放入其中。这很奇怪,因为“saveCommand”是根据项目是否已经存在(更新)还是不存在来确定的(切换到“INSERT INTO”命令可以工作)
更多代码
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
这是由
召集的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
答案 0 :(得分:1)
我明白了。它以某种方式在save()函数之前调用read()函数RIGHT,因此它替换了所有值。谢谢,无论如何