2010-02-18 56 views
0

我正在使用UltraGrid在表中插入數據。問題是我需要插入,更新,刪除Web服務。我從Web服務獲取dataSet並將其顯示到網格中,但是當我在網格上進行一些更改(例如插入或更新數據)時,還需要將新的dataSet發送到Web服務,以便Web服務執行更新數據。Web服務中的數據集

這是表 ID(PK,INT,不爲空) 名稱(nvarchar的100,NULL)

這是爲客戶端的代碼:

Public Sub Refresh() 
    Dim p As localhost.Service1 = New localhost.Service1 
    'bind datatable to UltraGrid 
    UltraGrid1.DataSource = p.GetData() 
    End Sub 

這是代碼從表中獲取數據的Web服務:

<WebMethod()> Public Function GetData() As DataSet 
    Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT ID,Name FROM Btable", nwindConn) 
    Dim custDS As DataSet = New DataSet() 
    custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey 
    custDA.Fill(custDS, "Btable") 
    GetData= custDS 
End Function 

這一切都工作正常,但現在我要插入新行線併發送至Web服務的nd從那裏插入它。我該怎麼做?謝謝!

回答

1

您應該處理UltraTable的事件「AfterRowInsert」,並從新行中檢索值。然後,您可以在您的Web服務上調用一個將該行插入數據庫的新Web方法。

Web方法可以是這個樣子....

<WebMethod()> Public sub AddRecord(id as integer, name as string) 

dim sSql as string = "INSERT INTO Btable (ID, Name) VALUES (@ID, @Name)"  
Dim oCmd as SqlCommand = nwindConn.CreateCommand() 
oCmd.CommandText = sSql 
oCmd.Parameters.Add("@ID", SqlDbType.Int).Value = id 
oCmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = name 
oCmd.ExecuteNonQuery() 

End Function 

如果你的ID列是自動編號它會略有不同。在這種情況下,您的web方法只會接受一個名稱參數,並會返回新記錄的ID。在MS SQL Server中,您可以通過在INSERT查詢後調用SCOPE_IDENTITY()來獲得此信息。

+0

-1用於建議使用@@ Identity而不是Scope_Identity()。在你解決問題後,我會給你回覆。 – ErikE 2010-02-19 02:33:22

+1

謝謝。如果您在開始時使用@Emtucifor發表評論,這會有所幫助,因此我會收到通知(以防忘記回來查看)。 – ErikE 2010-02-22 17:58:24