【问题标题】:Saving data from a data grid view with search functionality to a database将具有搜索功能的数据网格视图中的数据保存到数据库
【发布时间】:2016-12-23 20:26:08
【问题描述】:

目前我正在使用 WPF 和 WCF。 WCF 处理大部分数据库端并且是服务器,而 WPF 是我正在使用的客户端。在 WCF 端,我有:

    public DataSet getAllFixedCostsName(string name)
    {
        SqlCommand cmd;

        if (name == null || name == string.Empty)
        {
            cmd = new SqlCommand("Select * From FixedCost", con);
        }
        else
        {
            cmd = new SqlCommand("Select * From FixedCost Where FixedCostName = " + name, con);
        }

        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds, "FixedCost");

        return ds;
    }

    public void UpdateFixedCosts(DataSet ds)
    {
        try
        {
            SqlCommand cmd = new SqlCommand("Select * From FixedCost", con);
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            SqlCommandBuilder cmdbl = new SqlCommandBuilder(adap);

            adap.Update(ds);
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: '{0}'", e);
        }
    }

在 WPF 端我有:

public void SearchFixedCost_OnClick(object sender, RoutedEventArgs e)
    {
        TextBox search = _contentGrid.Children.OfType<TextBox>().First();
        DataGrid dataGrid = _contentGrid.Children.OfType<DataGrid>().First();
        ds = new DataSet();

        if (_contentGrid.Children.OfType<RadioButton>().First().IsChecked == true)
        {
            ds = cln.getAllFixedCostsName(search.Text);
        }
        else if (search.Text.All(char.IsDigit))
        {
            ds = cln.getAllFixedCostsExpenses(search.Text);
        }
        else
        {
            ds = null;
        }

        if (ds != null)
        {
            dataGrid.ItemsSource = ds.Tables["FixedCost"].DefaultView;
            dataGrid.Columns[0].Visibility = Visibility.Hidden;
        }
    }

    public void UpdateFixedCost_OnClick(object sender, RoutedEventArgs e)
    {
        cln.UpdateFixedCosts(ds);
    }

如果我不搜索任何特定内容,数据将成功更新。例如,如果我加载程序并单击搜索,它将生成该表的所有行,如果我更新其中一行并保存,则不会崩溃并且更改将被推送到数据库。

如果我搜索特定行,则更改该行并保存。当 WCF 到达“adap.Update(ds,“FixedCost”)”时,我将在 WCF 端崩溃,我得到一个 System.ServiceModel.FaultException。仅当我对该行或特定数据行进行更改时,才会发生崩溃。

【问题讨论】:

  • wcf 端有什么异常?将更新代码放在 try catch 之间,看看当时的确切异常是什么。
  • 更改自:sda.Fill(ds, "FixedCost");至:sda.Fill(ds);您创建了一个没有任何表的新数据集,因此您无法更新不存在的表。
  • 我很想知道SqlCommandBuilder 在提供执行存储过程的查询时会做什么。当数据集有待处理的更改时,它可能会出错。您希望您的“获取”过程如何更新数据库?
  • @RieKumar 不将其添加到 wcf 端而不是 wpf 端
  • 我添加了 try-catch 并得到了同样的异常,ServiceModel.FaultException。我将 sda.Fill(ds, "FixedCost") 更改为 sda.Fill(ds) 并且它在同一点崩溃。至于存储过程,我将它们更改为普通的 sql 语句,仍然会崩溃。

标签: c# wpf entity-framework wcf


【解决方案1】:

在 wcf 端的更新方法中,输入以下代码:

adap.TableMappings.Add("Table", "FixedCost");

在您的getAllFixedCostsName 方法中,您告诉它将数据填充到名为“FixedCost”的表中。在更新方法中,您创建了一个新的SqlDataAdapter,但该适配器对“FixedCost”一无所知。因此,它正在寻找更新数据库中名为“Table”的表。它找不到它,所以它在抱怨。

【讨论】:

  • 修复了它。谢谢:)
猜你喜欢
  • 2011-12-04
  • 2012-03-09
  • 2020-02-15
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多