Public Shared Function Save(ByVal TableName As String, ByVal SaveTable As DataTable, ByVal Cnstr As String) As Boolean Try Using da As New SqlDataAdapter("select * from " & TableName & " where 1 =2 ", Cnstr) Dim scb As SqlCommandBuilder = New SqlCommandBuilder(da) da.Update(SaveTable) SaveTable.AcceptChanges() Return True End Using Catch ex As Exception MsgBox(ex.Message.ToString) Return False End Try End Function
我是用DATASET 绑定到DGV的,请问如何更新到源数据库?没有用DATATABLE对象哦 [解决办法] datset 是 Datatable的集合。 你的打他table就是dataset.table(0) [解决办法] UPDATA 表要有主键就好弄 不然无法使用UPDATA [解决办法] Sub bluksqlcopy() Dim yeah As String Dim ah As String Dim av As Object If Tab1.SelectedTab.Name = "T1" Then yeah = "ZL" ah = "種類" av = DG3 ElseIf Tab1.SelectedTab.Name = "T3" Then yeah = "BBS" ah = "歸類" av = DG1 ElseIf Tab1.SelectedTab.Name = "T2" Then yeah = "ZHG" ah = "綜合櫃" av = DG2 End If Dim i = MsgBox("是否更新數據庫" & ah & "表 ", MsgBoxStyle.Question + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, "確認操作點是(Y)") If i = vbYes Then Dim connectionString As String = GetConnectionString() Using destinationConnection As SqlConnection = New SqlConnection(connectionString) destinationConnection.Open() Dim strsql1 = "delete from " & yeah & "" bmd1.CommandText = strsql1 bmd1.Connection = destinationConnection bmd1.ExecuteNonQuery() Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(destinationConnection) bulkCopy.DestinationTableName = "" & yeah & "" Dim reader As New DataTable reader = av.DataSource Try bulkCopy.WriteToServer(reader) Catch ex As Exception Console.WriteLine(ex.Message) Finally destinationConnection.Close() MsgBox("更新成功", MsgBoxStyle.OkOnly, "系統提示") End Try End Using End Using Else MsgBox("操作取消") End If End Sub 表结构不是数据表 只是关联表的话而且数据量不是很大(而且没有主键)就用上面代码