VB.NET数据集更新
为什么我的代码集没有在数据集中更新?然后是错误。请任何人检查此代码并指出我丢失的地方。提前谢谢VB.NET数据集更新,vb.net,dataset,Vb.net,Dataset,为什么我的代码集没有在数据集中更新?然后是错误。请任何人检查此代码并指出我丢失的地方。提前谢谢 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
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
您的dataAdapter是否具有update命令 (看起来好像没有-因此它不知道如何处理更新…) 下面是一个Update命令示例:(对于包含3列的employee表-如下所示:
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)
您可以看到它是一个通用更新,可以处理任何字段中的更改。循环中的条件是否得到执行(设置断点!)?引发的错误在哪里?什么错误 另外,它为什么要使用
ToString
?这似乎是多余的
If CInt(dRow.Item("StockID")) = 2 Then
应该足够了
最后,您将执行冗余清理:
If conxMain IsNot Nothing Then
conxMain.Close()
conxMain.Dispose()
End If
Dispose
意味着关闭
–无需执行两个操作:
Close
和Dispose
在功能上是等效的
[]我是从科纳德·鲁道夫对我的程序进行的错误更正中得到的
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社区和回答我问题的人
以下是参考:
--菜单--
--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
Dim登录为新登录类
login.ShowDialog()
--连接--
专用conec作为SqlConnection
Dim stringCon As String=“数据源=;初始目录=;持久安全信息=真;用户ID=;密码=”
公共只读属性prConec()作为对象
得到
返回conec
结束
端属性
公共子连接()
尝试
conec=新的SqlConnection(stringCon)
如果conec.State ConnectionState.Open,则
conec.Open()
如果结束
特例
MessageBox.Show(例如Message)
结束尝试
端接头
--客车--
functiones.Conectar()
Dim coman作为新的SqlCommand(“sp_cliente”,functiones.prConec)
Dim dt作为新数据表
coman.CommandType=CommandType.StoredProcess
compan.Parameters.Add(“@i_operacion”,SqlDbType.Char,1,ParameterDirection.Input).Value=“B”
dt.Load(coman.ExecuteReader())
grdclients.DataSource=dt
--插入器--
functiones.Conectar()
Dim coman作为新的SqlCommand(“sp_articulo”,functiones.prConec)
coman.CommandType=CommandType.StoredProcess
compan.Parameters.Add(“@i_operacion”,SqlDbType.Char,1,ParameterDirection.Input).Value=“i”
coman.ExecuteNonQuery()
客车()
林皮亚()
--组合--
Dim dt作为新数据表
dt.列添加(“Codigo”)
dt.列添加(“说明”)
尺寸dr1为DataRow=dt.NewRow
dr1.项目(“Codigo”)=“A”
dr1.项目(“描述”)=“活动”
dt.Rows.Add(dr1)
尺寸dr2为DataRow=dt.NewRow
dr2.项目(“Codigo”)=“I”
dr2.项目(“描述”)=“不活动”
dt.Rows.Add(dr2)
cmbEstado.DataSource=dt
cmbEstado.ValueMember=“Codigo”
cmbEstado.DisplayMember=“description”
--网格视图--
--1--
作为DataGridViewRow=grdClient.CurrentRow的Dim grdFila
txtCedula.Text=grdFila.Cells(0).Value
--2--
如果DataGridProductos.CurrentCell.ColumnIndex=0,则
将模糊FLstArticulos作为新FLstArticulos
FLstArticulos.ShowDialog()
DataGridProductos.CurrentRow.Cells(0).Value=FLstArticulos.PrIdArticulo
如果结束
--GRIDVIEW.CELLENDEDIT--
如果DataGridProductos.CurrentCell.ColumnIndex=3,则
Dim precio作为新双人
黯淡的康蒂达像新的双人舞
精度=CDbl(grdRow.单元格(2.值)
cantidad=CDbl(grdRow.单元格(3.值)
DataGridProductos.CurrentRow.Cells(4).Value=PLTotalFilaItem(cantidad,precio)
PLCargaTotales()
如果结束
副驾驶室()
双倍小计
双腔
对于DataGridProductos.Rows中作为DataGridViewRow的每个grd
如果不是String.IsNullOrEmpty(grd.Cells(4.Value)),则
小计=小计+CDbl(合计单元格(4)值)
如果结束
下一个grd
txtSubtotal.Text=subTotal.ToString
iva=十进制.四舍五入(小计`在此处输入代码`*0.12)
txtIva.Text=iva.ToString
txtTotalPagar.Text=(小计+iva).ToString
端接头
您能发布抛出的异常消息吗?它是从哪一行代码抛出的?您好,这里是我收到的异常消息------------------InvalidPearationException被捕获UpdateCommand的动态SQL生成不支持对不返回任何键列信息的SelectCommand------------@RedsDevils:你的“股票”是什么表中有主键列?没有!我使用StockID作为主键,但没有在表结构中定义。@RedsDevils:建议您将该列设置为主键,但这不是重点。我只是猜测查询构造函数无法为没有标识符的表生成update语句。这部分如何?对于dsPurchaseInfo.Tables(“Stock”)行中的每个dRow,如果CInt(dRow.Item(“StockID”).ToString())=2,那么dRow.Item(“StockCode”)=“Re Fashion[G]”结束,如果下一步它说要更新dataAdapter,不是吗?您给了适配器一个select命令(检查代码)您需要给它一个update命令。您的代码会更改数据,但适配器需要知道运行什么命令来更新它。您可以使用visual studio向导生成更新命令,或将其写入您的sefl.Dim cmdUpdateCommand作为SqlCommand=New SqlCommand(“更新库存集StockCode='Re-Fashion(H)'WHERE StockID=2”,conxMain)cmdUpdateCommand.CommandTimeout=30 dadPurchaseInfo.UpdateCommand=cmdUpdateCommand Dim cbUpdate作为SqlCommandBuilder=New SqlCommandBuilder(dadPurchaseInfo)dadPurchaseInfo.Update(dsPurchaseInfo,“库存”)------我像上面那样编写并运行,但它不起作用。您能给我看一下在我在Visual Studio中创建更新查询后调用的代码段吗?谢谢Konard Rudolph!我是根据您对我的程序的错误更正得到的!:)非常感谢。我花了一整天的时间来解决这个问题!非常感谢!我不确定那些--XYZ--
标题应该是注释、#region
s还是ju