【问题标题】:Update table 1 data to table 2将表 1 数据更新为表 2
【发布时间】:2014-07-07 08:37:12
【问题描述】:

我使用SqlDataSource 和此代码来更新数据

UPDATE [centreadmin]
SET [centreadmin].centre = [centre].centre
FROM [centre], [centreadmin]
WHERE [centreadmin].centre = [centre].oldcentre

但现在我不想使用SqlDataSource

我在 .vb 中尝试过,但没有成功。

Protected Sub BtnUpdateAdmin_Click(sender As Object, e As EventArgs) Handles BtnUpdateAdmin.Click
        Dim connection As New SqlConnection()
        connection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim comd = connection.CreateCommand()

        connection.Open()
        comd.CommandText = "UPDATE [centreadmin] SET [centreadmin].centre = [centre].centre FROM [centre], [centreadmin] WHERE [centreadmin].centre = [centre].oldcentre"
        connection.Close()
    End Sub
End Class

我做错了什么,我该如何做到这一点?

【问题讨论】:

    标签: asp.net sql sql-server database vb.net


    【解决方案1】:

    您永远不会执行 SQL。在其中包含ExecuteNonQuery (MSDN)

    ...
    connection.Open()
    comd.CommandText = "UPDATE [centreadmin] SET [centreadmin].centre = [centre].centre FROM [centre], [centreadmin] WHERE [centreadmin].centre = [centre].oldcentre"
    comd.ExecuteNonQuery()
    connection.Close()
    ...
    

    【讨论】:

    • 还要注意 MSDN 页面上使用Using 语句进行连接以确保正确关闭所有连接。
    【解决方案2】:

    是的,您缺少 executeNonQuery 所以在connection.close()之前试试下面

    comd.ExecuteNonQuery
    

    这里是很好的link 关于如何使用命令对象插入和更新记录

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多