2009-11-08 63 views
0

爲什麼我的代碼集在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 
+0

你可以發佈拋出的異常消息嗎?它從哪個代碼行拋出? – 2009-11-08 06:48:00

+0

嗨。 O操作。 ķ。 W, 這裏是異常消息我 -------------------- InvalidOpeartionException被抓獲 動態SQL生成的更新命令不抵抗的SelectCommand支持這不會返回任何關鍵列信息。 ------------ – RedsDevils 2009-11-08 06:51:34

+0

@RedsDevils:你的「股票」表是否有主鍵列? – 2009-11-08 06:59:03

回答

1

請問您在循環條件得到執行(!設置一個斷點)在哪裏錯誤拋出?什麼錯誤?

另外,它爲什麼使用ToString呢?這似乎是多餘的。

If CInt(dRow.Item("StockID")) = 2 Then 

應該足夠了。

最後,您要執行冗餘清理:

If conxMain IsNot Nothing Then 
    conxMain.Close() 
    conxMain.Dispose() 
End If 

Dispose意味着Close - 沒有必要進行這兩種操作:

CloseDispose在功能上等同。

[Source: MSDN]

+0

謝謝Konard Rudolph!我根據你的錯誤糾正得到了它的程序! :) 非常感謝。我需要整整一天才能解決!非常感謝你! – RedsDevils 2009-11-08 10:48:11

1

您的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) 

你可以看到它是一個可以處理任何領域變化的一般更新

+0

這部分怎麼樣? 對於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

+0

你給了適配器一個選擇命令(檢查你的代碼),你需要給它一個更新命令。 您的代碼會更改數據 - 但適配器需要知道運行哪個命令來更新它。 您可以使用visual studio wizard來生成更新命令,或將其寫入yoursefl。 – Dani 2009-11-08 07:24:53

+0

dadPurchaseInfo.UpdateCommand = ..... – Dani 2009-11-08 07:25:46

0

我是從由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社區和誰回答我的問題。

這裏是參照:

P.S:像o.k.w表示:表必須具有主鍵。感謝o.k.w!

-1
--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 
+0

我不確定那些' - XYZ - '頭文件是否應該是註釋,'#區域'或者只是普通的文本文本,但是因爲它們不會被編譯,所以你應該修復它。 – 2017-05-03 05:25:34

+0

歡迎來到StackOverflow。雖然這段代碼可能會回答這個問題,但爲什麼和/或代碼如何回答這個問題提供了額外的背景,這提高了它的長期價值。 – 2017-05-03 05:59:12