简体   繁体   English

将具有搜索功能的数据网格视图中的数据保存到数据库中

[英]Saving data from a data grid view with search functionality to a database

Currently I'm using WPF and WCF. 目前,我正在使用WPF和WCF。 WCF handles most of the database end and is the server, while WPF is the client I'm using. WCF处理大部分数据库端,并且是服务器,而WPF是我正在使用的客户端。 On the WCF end I have: 在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);
        }
    }

On the WPF end I have: 在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);
    }

The data will update successfully if I don't search for anything specific. 如果我不搜索任何特定内容,数据将成功更新。 For example, if I load the program and click search it will generate the all the rows for that table, if I update one of the rows and save, then no crash and the changes will be pushed to the database. 例如,如果我加载程序并单击“搜索”,它将为该表生成所有行,如果我更新其中一行并保存,则不会崩溃,并且更改将被推送到数据库。

If I search for a specific row, then change that row and save it. 如果我搜索特定行,请更改该行并保存。 I will crash on the WCF side when it reach the "adap.Update(ds, "FixedCost")", I get a System.ServiceModel.FaultException. 当它到达“ adap.Update(ds,“ FixedCost”)“时,我将在WCF端崩溃,我得到了System.ServiceModel.FaultException。 The crash only occurs if I make a change to that row, or rows of specific data. 仅当我对该行或特定数据行进行更改时,才会发生崩溃。

In your update method on the wcf side, put this code: 在wcf端的更新方法中,输入以下代码:

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

In your getAllFixedCostsName method you are telling it to fill the data into a table named "FixedCost". 在您的getAllFixedCostsName方法中,您要告诉它将数据填充到名为“ FixedCost”的表中。 In the update method you create a new SqlDataAdapter but that adapter does not know anything about "FixedCost". 在更新方法中,您创建了一个新的SqlDataAdapter但是该适配器对“ FixedCost”一无所知。 So it is looking to update a table named "Table" in your database. 因此,它正在寻找更新数据库中名为“ Table”的表的方法。 It cannot find it, so it is complaining. 它找不到它,所以它在抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM