2010-03-27 103 views
3

我需要建議。我有asp.net web服務和winforms客戶端應用程序。 客戶端調用此Web方法並獲取數據集。使用數據集更新數據源

1. [WebMethod] 
    2. public DataSet GetSecureDataSet(string id) 
    3. { 
    4. 
    5. 
    6.  SqlConnection conn = null; 
    7.  SqlDataAdapter da = null; 
    8.  DataSet ds; 
    9.  try 
    10.  { 
    11. 
    12.   string sql = "SELECT * FROM Tab1"; 
    13. 
    14.   string connStr = WebConfigurationManager.ConnectionStrings["Employees"].ConnectionString; 
    15. 
    16.   conn = new SqlConnection(connStr); 
    17.   conn.Open(); 
    18. 
    19.   da = new SqlDataAdapter(sql, conn); 
    20. 
    21.   ds = new DataSet(); 
    22.   da.Fill(ds, "Tab1"); 
    23. 
    24.   return ds; 
    25.  } 
    26.  catch (Exception ex) 
    27.  { 
    28.   throw ex; 
    29.  } 
    30.  finally 
    31.  { 
    32.   if (conn != null) 
    33.    conn.Close(); 
    34.   if (da != null) 
    35.    da.Dispose(); 
    36.  } 
    37. } 

在他完成工作後,他將此更新方法稱爲Web方法。他可以在數據集的表中添加,刪除和編輯行。在客戶端

[WebMethod] 
    public bool SecureUpdateDataSet(DataSet ds) 
    { 

     SqlConnection conn = null; 
     SqlDataAdapter da = null; 
     SqlCommand cmd = null; 
     try 
     { 

      DataTable delRows = ds.Tables[0].GetChanges(DataRowState.Deleted); 

      DataTable addRows = ds.Tables[0].GetChanges(DataRowState.Added); 

      DataTable editRows = ds.Tables[0].GetChanges(DataRowState.Modified); 

      string sql = "UPDATE * FROM Tab1"; 

      string connStr = WebConfigurationManager.ConnectionStrings["Employees"].ConnectionString; 

      conn = new SqlConnection(connStr); 
      conn.Open(); 

      cmd = new SqlCommand(sql, conn); 
      da = new SqlDataAdapter(sql, conn); 

      if (addRows != null) 
      { 
       da.Update(addRows); 
      } 

      if (delRows != null) 
      { 
       da.Update(delRows); 
      } 

      if (editRows != null) 
      { 
       da.Update(editRows); 
      } 


      return true; 

     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      if (conn != null) 
       conn.Close(); 
      if (da != null) 
       da.Dispose(); 
     } 
    } 

代碼

1. //on client side is dataset bind to datagridview 
    2. Dataset ds = proxy.GetSecureDataSet(""); 
    3. ds.AcceptChanges(); 
    4. 
    5. //edit dataset 
    6. 
    7. 
    8. //get changes 
    9. DataSet editDataset = ds.GetChanges(); 
    10. 
    11. //call update webmethod 
    12. proxy.SecureUpdateDataSet(editDataSet) 

但與此錯誤完成:

System.Web.Services.Protocols.SoapException:服務器無法處理請求。 ---> System.InvalidOperationException:在使用修改的行傳遞DataRow集合時,Update需要有效的UpdateCommand。 在WebService.Service.SecureUpdateDataSet(數據集DS)在d:\ Diploma.Work \ WebService的\ Service1.asmx.cs:線489

問題是與SQL Commad,客戶端可以添加,刪除和插入行,如何可以編寫一個corect SQL命令....有什麼建議嗎?謝謝

+1

使用到位的 「使用」 語句的try-catch-finally塊的SqlConnection的。方便,並使代碼塊看起來更短。 :-) – 2010-03-28 05:16:55

回答

0

我認爲SqlCommandBuilder是通過數據集添加,更新和刪除數據庫中數據的更好,更方便的方法。

1

試試這個:

[WebMethod] 
public bool SecureUpdateDataSet(DataSet delta) 
{ 

    string connStr = WebConfigurationManager.ConnectionStrings["Employees"].ConnectionString; 

    using(var conn = new SqlConnection(connStr)) 
    { 
     conn.Open(); 

     string sql = "select * from tab1 where 1 = 0"; 

     using(var da = new SqlDataAdapter(sql, conn)) 
     { 

      var builder = new SqlCommandBuilder(ad); 

      da.InsertCommand = builder.GetInsertCommand(); 
      da.UpdateCommand = builder.GetUpdateCommand(); 
      da.DeleteCommand = builder.GetDeleteCommand(); 

      da.Update(delta); 

      return true; 
     } 
    } 
    return false; 
} 
+0

我認爲你應該獲得主要/唯一的關鍵信息,以便在填充數據集之前進行如下操作,否則更新可能會失敗: - da.MissingSchemaAction = MissingSchemaAction.AddWithKey; – 2010-03-28 07:36:09

+0

-1:你也應該把'SqlDataAdapter'放到'using'塊中。 – 2010-03-28 09:31:44

+0

@John Saunders:你爲什麼在這一點上停下來解決問題?你是否誠實地在你的每個對象分配上實踐它?我想你的大部分代碼是非常嵌套的。我把連接放在使用子句的原因是爲了確保在執行查詢後立即釋放數據庫連接。你知道某些數據庫是基於連接許可的嗎?我相信.NET的垃圾收集或任何託管語言的事情,所以我沒有看到任何要求使用子句 – 2010-03-28 10:42:28