使用数据集更新数据源

发布于 2024-08-26 14:16:14 字数 3238 浏览 3 评论 0原文

我需要建议。我有 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 集合时,更新需要有效的 UpdateCommand。 在 D:\Diploma.Work\WebService\Service1.asmx.cs:line 489 中的 WebService.Service.SecureUpdateDataSet(DataSet ds) 处,

问题在于 SQL Command,客户端可以添加、删除和插入行,如何编写一个正确的 SQL 命令......请问有什么建议吗?谢谢

I need advice. I have asp.net web service and winforms client app.
Client call this web method and get dataset.

   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.  }  

After he finish work he call this update web method. He can add, delete and edit rows in table in dataset.

  [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();
        }
    }

Code on client side

   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)  

But it finish with this error :

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
at WebService.Service.SecureUpdateDataSet(DataSet ds) in D:\Diploma.Work\WebService\Service1.asmx.cs:line 489

Problem is with SQL Commad, client can add, delete and insert row, how can write a corect SQL command.... any advice please? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-09-02 14:16:14

试试这个:

[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;
}

try this:

[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;
}
︶ ̄淡然 2024-09-02 14:16:14

我认为 SqlCommandBuilder 是添加、更新和更新的更好、更方便的方法通过dataset删除数据库中的数据。

I think SqlCommandBuilder is the better and convenient way for adding, updating and deleting data in the database through dataset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文