尝试使用数据表中的数据更新数据库,但它不断添加新行 [英] trying to update database with data from a datatable, but it keeps adding new rows

查看:84
本文介绍了尝试使用数据表中的数据更新数据库,但它不断添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我有一个asp.net webservice需要使用数据表中的新数据更新我的sql数据库。我的主程序填充数据表并将其传递给网络服务器。

我的问题是改为或更新它继续添加具有相同名称列的新行。这里是用于存储数据表的webserver函数的代码。

Hello
I have a asp.net webservice that needs to update my sql database with new data from a datatable. My main program fills the datatable and passes it to the webserver.
My problem is instead or updating the row it keeps adding new rows with the same 'Name' column. Here it the code for the webserver function to store the datatable.

Public Function UpdateDataBase(ByVal dTable As DataTable) As String
       If Not sqlConnection.State = ConnectionState.Open Then
           sqlConnection.Open()
       End If

       If dTable.Rows.Count < 1 Then
           Return False
       End If

       Dim tblName As String = Trim(dTable.Rows(0)("Name"))
       Dim selectStatement As String = "SELECT * FROM Refriguration Where Name='" & tblName & "'"
       Dim sqlDa As SqlDataAdapter = New SqlDataAdapter()
       sqlDa.SelectCommand = New SqlCommand(selectStatement, sqlConnection)
       Dim cb As SqlCommandBuilder = New SqlCommandBuilder(sqlDa)
       Try
           sqlDa.UpdateCommand = cb.GetUpdateCommand()
           sqlDa.Update(dTable)
           Return "True " & selectStatement
       Catch ex As ArgumentException
           Return "False " & selectStatement
       End Try

   End Function





任何帮助将不胜感激。



Any help will be appreciated.

推荐答案

这篇关于尝试使用数据表中的数据更新数据库,但它不断添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆