VB.NET数据集更新
为什么我的代码集在DataSet中没有更新?然后它转到错误。请任何人检查这个代码,并指出我失踪的地方。提前致谢!VB.NET数据集更新
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim conxMain As New SqlConnection("Data Source=SERVER;Initial Catalog=DBTest;Persist Security Info=True;User ID=username;Password=pwds")
Dim dadPurchaseInfo As New SqlDataAdapter
Dim dsPurchaseInfo As New DataSet1
Try
Dim dRow As DataRow
conxMain.Open()
Dim cmdSelectCommand As SqlCommand = New SqlCommand("SELECT * FROM Stock", conxMain)
cmdSelectCommand.CommandTimeout = 30
dadPurchaseInfo.SelectCommand = cmdSelectCommand
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(dadPurchaseInfo)
dadPurchaseInfo.Fill(dsPurchaseInfo, "Stock")
For Each dRow In dsPurchaseInfo.Tables("Stock").Rows
If CInt(dRow.Item("StockID").ToString()) = 2 Then
dRow.Item("StockCode") = "Re-Fashion[G]"
End If
Next
dadPurchaseInfo.Update(dsPurchaseInfo, "Stock")
Catch ex As Exception
MsgBox("Error : ")
Finally
If dadPurchaseInfo IsNot Nothing Then
dadPurchaseInfo.Dispose()
End If
If dsPurchaseInfo IsNot Nothing Then
dsPurchaseInfo.Dispose()
End If
If conxMain IsNot Nothing Then
conxMain.Close()
conxMain.Dispose()
End If
End Try
End Sub
请问您在循环条件得到执行(!设置一个断点)在哪里错误抛出?什么错误?
另外,它为什么使用ToString
呢?这似乎是多余的。
If CInt(dRow.Item("StockID")) = 2 Then
应该足够了。
最后,您要执行冗余清理:
If conxMain IsNot Nothing Then
conxMain.Close()
conxMain.Dispose()
End If
Dispose
意味着Close
- 没有必要进行这两种操作:
Close
和Dispose
在功能上等同。
谢谢Konard Rudolph!我根据你的错误纠正得到了它的程序! :) 非常感谢。我需要整整一天才能解决!非常感谢你! – RedsDevils 2009-11-08 10:48:11
您的dataAdapter是否有更新命令?
(它看起来像它不会 - 所以它不知道做什么用更新....)
下面是一个更新命令例如:(与3列的雇员表 - 为列举如下:?
UPDATE [Employee]
SET [name] = @name
, [manager] = @manager
WHERE (([id] = @Original_id) AND
((@IsNull_name = 1 AND [name] IS NULL) OR
([name] = @Original_name)) AND
((@IsNull_manager = 1 AND [manager] IS NULL) OR
([manager] = @Original_manager)));
SELECT id
, name
, manager
FROM Employee
WHERE (id = @id)
你可以看到它是一个可以处理任何领域变化的一般更新
这部分怎么样? 对于dsPurchaseInfo.Tables(“Stock”)中的每个dRow。行 如果CInt(dRow.Item(“StockID”).ToString())= 2则 dRow.Item(“StockCode”)=“Re-Fashion [G]“ End If Next 它说要更新dataAdapter,不是吗? – RedsDevils 2009-11-08 06:56:59
你给了适配器一个选择命令(检查你的代码),你需要给它一个更新命令。 您的代码会更改数据 - 但适配器需要知道运行哪个命令来更新它。 您可以使用visual studio wizard来生成更新命令,或将其写入yoursefl。 – Dani 2009-11-08 07:24:53
dadPurchaseInfo.UpdateCommand = ..... – Dani 2009-11-08 07:25:46
我是从由Konard鲁道夫我的程序的纠错!
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim conxMain As New SqlConnection("Data Source=SERVER;Initial Catalog=DBTest;Persist Security Info=True;User ID=username;Password=pwds")
Dim dadPurchaseInfo As New SqlDataAdapter
Dim dsPurchaseInfo As New DataSet1
Try
Dim dRow As DataRow
conxMain.Open()
dadPurchaseInfo.SelectCommand = New SqlCommand("SELECT * FROM Stock", conxMain)
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(dadPurchaseInfo)
dadPurchaseInfo.Fill(dsPurchaseInfo, "Stock")
For Each dRow In dsPurchaseInfo.Tables("Stock").Rows
If CInt(dRow.Item("StockID")) = 2 Then
dRow.Item("StockCode") = "Re-Fashion(H)"
End If
Next
dadPurchaseInfo.Update(dsPurchaseInfo, "Stock")
Catch ex As Exception
MsgBox("Error : " & vbCrLf & ex.Message)
Finally
If dadPurchaseInfo IsNot Nothing Then
dadPurchaseInfo.Dispose()
End If
If dsPurchaseInfo IsNot Nothing Then
dsPurchaseInfo.Dispose()
End If
If conxMain IsNot Nothing Then
conxMain.Dispose()
End If
End Try
End Sub
上述一组代码可以使用DataSet进行更新!感谢stackoverflow社区和谁回答我的问题。
这里是参照:
- How To Update a SQL Server Database by Using the SqlDataAdapter Object in Visual Basic .NET
- How to update a database from a DataSet object by using Visual Basic .NET
P.S:像o.k.w表示:表必须具有主键。感谢o.k.w!
--MENU--
Dim login As New LoginClass
login.ShowDialog()
--CONEXION--
Private conec As SqlConnection
Dim stringCon As String = "Data Source= ;Initial Catalog=;Persist Security Info=True;User ID=;Password="
Public ReadOnly Property prConec() As Object
Get
Return conec
End Get
End Property
Public Sub Conectar()
Try
conec = New SqlConnection(stringCon)
If conec.State <> ConnectionState.Open Then
conec.Open()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
--BUSCAR--
funciones.Conectar()
Dim coman As New SqlCommand("sp_cliente", funciones.prConec)
Dim dt As New DataTable
coman.CommandType = CommandType.StoredProcedure
coman.Parameters.Add("@i_operacion", SqlDbType.Char, 1, ParameterDirection.Input).Value = "B"
dt.Load(coman.ExecuteReader())
grdClientes.DataSource = dt
--INSERTAR--
funciones.Conectar()
Dim coman As New SqlCommand("sp_articulo", funciones.prConec)
coman.CommandType = CommandType.StoredProcedure
coman.Parameters.Add("@i_operacion", SqlDbType.Char, 1, ParameterDirection.Input).Value = "I"
coman.ExecuteNonQuery()
Buscar()
Limpiar()
--COMBO--
Dim dt As New DataTable
dt.Columns.Add("Codigo")
dt.Columns.Add("Descripcion")
Dim dr1 As DataRow = dt.NewRow
dr1.Item("Codigo") = "A"
dr1.Item("Descripcion") = "Activo"
dt.Rows.Add(dr1)
Dim dr2 As DataRow = dt.NewRow
dr2.Item("Codigo") = "I"
dr2.Item("Descripcion") = "Inactivo"
dt.Rows.Add(dr2)
cmbEstado.DataSource = dt
cmbEstado.ValueMember = "Codigo"
cmbEstado.DisplayMember = "Descripcion"
--GRIDVIEW--
--1--
Dim grdFila As DataGridViewRow = grdClientes.CurrentRow
txtCedula.Text = grdFila.Cells(0).Value
--2--
If DataGridProductos.CurrentCell.ColumnIndex = 0 Then
Dim FLstArticulos As New FLstArticulos
FLstArticulos.ShowDialog()
DataGridProductos.CurrentRow.Cells(0).Value = FLstArticulos.PrIdArticulo
End If
--GRIDVIEW.CELLENDEDIT--
If DataGridProductos.CurrentCell.ColumnIndex = 3 Then
Dim precio As New Double
Dim cantidad As New Double
precio = CDbl(grdRow.Cells(2).Value)
cantidad = CDbl(grdRow.Cells(3).Value)
DataGridProductos.CurrentRow.Cells(4).Value = PLTotalFilaItem(cantidad, precio)
PLCargaTotales()
End If
Sub PLCargaTotales()
Dim subTotal As Double
Dim iva As Double
For Each grd As DataGridViewRow In DataGridProductos.Rows
If Not String.IsNullOrEmpty(grd.Cells(4).Value) Then
subTotal = subTotal + CDbl(grd.Cells(4).Value)
End If
Next grd
txtSubtotal.Text = subTotal.ToString
iva = Decimal.Round(subTotal`enter code here` * 0.12)
txtIva.Text = iva.ToString
txtTotalPagar.Text = (subTotal + iva).ToString
End Sub
我不确定那些' - XYZ - '头文件是否应该是注释,'#区域'或者只是普通的文本文本,但是因为它们不会被编译,所以你应该修复它。 – 2017-05-03 05:25:34
欢迎来到StackOverflow。虽然这段代码可能会回答这个问题,但为什么和/或代码如何回答这个问题提供了额外的背景,这提高了它的长期价值。 – 2017-05-03 05:59:12
你可以发布抛出的异常消息吗?它从哪个代码行抛出? – 2009-11-08 06:48:00
嗨。 O操作。 ķ。 W, 这里是异常消息我 -------------------- InvalidOpeartionException被抓获 动态SQL生成的更新命令不抵抗的SelectCommand支持这不会返回任何关键列信息。 ------------ – RedsDevils 2009-11-08 06:51:34
@RedsDevils:你的“股票”表是否有主键列? – 2009-11-08 06:59:03