2011-10-15 55 views
-1

我有一個gridview加載了來自我的數據庫的數據和一個gridview交換函數,在列之間交換數據後調用「優先級」,我想保存這些更改到我的數據庫。將數據集表格數據的特定列更新到SQL數據庫

只有「優先級」列值會發生變化,我如何只更新我的數據集表的列到我的SQL數據庫?請指教,謝謝!

代碼:

Protected Sub Gridviewselectbus_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) 

     If e.CommandName = "Up" Then 

      Dim index As Int16 = Convert.ToInt16(e.CommandArgument) 
      If index = 0 Or index = 1 Then 
       Exit Sub 
      End If 

      Dim objCampaignManagementTable As New CampaignManagementBLL 
      Dim ds As DataSet = objCampaignManagementTable.SelectCampaignManagementTableListing() 

      Dim dtr As DataRow = ds.Tables(0).Rows(index - 1) 

      Dim dtrSwap As DataRow = ds.Tables(0).Rows(index - 2) 
      Dim dc As DataColumn = ds.Tables(0).Columns(6) 

      'Dim value As Int16 = Convert.ToInt16(dt.Rows(index)("Priority")) 
      Dim value As Int16 = CType((dtr)(dc), Short) 
      Dim temp1 As Int16 = value 

      'Increases the selected row's priority 
      dtr(dc) = value - 1 

      'Decreases the priority of the row that is on top of the selected row by assigning 
      'the original selected row value to it. 
      dtrSwap(dc) = value 

      ds.Tables(0).DefaultView.Sort = "Priority" 
      ds.Tables(0).AcceptChanges() 
      dtNew = ds.Tables(0).Copy() 
      uigvList.DataSource = ds.Tables(0) 
      uigvList.DataBind() 
      ds.Tables(0).AcceptChanges() 

      For i As Integer = 0 To uigvList.Rows.Count - 1 
       dtNew.Rows(i)("Code") = uigvList.Rows(i).Cells(1).Text 
       dtNew.Rows(i)("Name") = uigvList.Rows(i).Cells(2).Text 
       dtNew.Rows(i)("Type") = uigvList.Rows(i).Cells(3).Text 
       dtNew.Rows(i)("ActiveDateFrom") = uigvList.Rows(i).Cells(4).Text 
       dtNew.Rows(i)("ActiveDateTo") = uigvList.Rows(i).Cells(5).Text 
       dtNew.Rows(i)("Priority") = uigvList.Rows(i).Cells(6).Text 
      Next 

      ' Update database 
End if 

    If e.CommandName = "down" Then 

' Down code here 

End Sub 

回答

1

看看這個代碼這隻更新特定列,然後同時更新SQL Server數據庫..

我希望它會幫助你..

注意:catDA意思是數據適配器....並且這僅僅是示例...如何更新.....

catDA.UpdateCommand = new OdbcCommand("UPDATE Categories SET CategoryName = ? " + 
             "WHERE CategoryID = ?" , nwindConn); 

catDA.UpdateCommand.Parameters.Add("@CategoryName", OdbcType.VarChar, 15, "CategoryName"); 

OdbcParameter workParm = catDA.UpdateCommand.Parameters.Add("@CategoryID", OdbcType.Int); 
workParm.SourceColumn = "CategoryID"; 
workParm.SourceVersion = DataRowVersion.Original; 

DataSet catDS = new DataSet(); 
catDA.Fill(catDS, "Categories");  

DataRow cRow = catDS.Tables["Categories"].Rows[0]; 

cRow["CategoryName"] = "New Category"; 

DataRow[] modRows = catDS.Tables["Categories"].Select(null, null, DataViewRowState.ModifiedCurrent); 
catDA.Update(modRows); 
+0

出於好奇,爲什麼ODBC在這裏? –

+0

@MarcGravell這只是一個例子,我們可以用sql數據庫替換... –

相關問題