C# 数据网格未填充且无错误

C# datagrid not populating and no error

我的第一个问题是我有一个方法 private void FillGeneralLedger(),我把这个方法放在点击事件的按钮中以填充数据网格视图 dgvGeneralLedger 我的问题是当我 运行 我没有收到错误并且 dgv 仍然是空的。

我的第二个问题是我想使用相同的连接但有 5 个命令所有相同只是帐号不同 例如在下面的示例中下面的帐户是“8200030”我想对“8200031”、“8200032”做同样的事情

private void FillGeneralLedger()
    {
        SqlConnection conn = new SqlConnection(sConnectionString);
        try
        {
            DataSet dataset = new DataSet();
            SqlCommand command = new SqlCommand("Select Ddate as Date" +
                                                 ", etype" +
                                                 ", Refrence" +
                                                 ", linkacc as ContraAcc" +
                                                 ", Description" +
                                                 ", sum(case when amount > 0 then amount else 0 end) as Debits" +
                                                 ", sum(case when amount < 0 then amount else 0 end) as Credits" +
                                                 ", sum(amount) as Cumulative" +

                                                   " FROM  dbo.vw_LedgerTransactions " +
                                                   " WHERE accnumber = '8200030'" +
                                                   " AND DDate BETWEEN '2016-04-01 00:00:00' AND '2016-04-30 00:00:00'" +
                                                   " AND DataSource = 'PAS11CEDCRE17'" +
                                                   " group by Ddate, etype, Refrence, linkacc, Description, Amount", conn);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            conn.Open();
            command.ExecuteNonQuery();
            adapter.Fill(dataset);
            if (dataset.Tables[0].Rows.Count != 0)
            {
                lstGeneralLedger = new List<ReportDataClasses.GeneralLedger>();
                foreach (DataRow row in dataset.Tables[0].Rows)
                {
                    ReportDataClasses.GeneralLedger newGeneralLedger = new ReportDataClasses.GeneralLedger();
                    newGeneralLedger.Ddate = row[0].ToString();
                    newGeneralLedger.etype = row[1].ToString();
                    newGeneralLedger.refrence = row[2].ToString();
                    newGeneralLedger.linkacc = row[3].ToString();
                    newGeneralLedger.Description = row[4].ToString();
                    newGeneralLedger.debit = decimal.Parse(row[5].ToString());
                    newGeneralLedger.credit = decimal.Parse(row[6].ToString());
                    newGeneralLedger.cumulative = decimal.Parse(row[7].ToString());

                    lstGeneralLedger.Add(newGeneralLedger);
                }
                dgvGeneralLedger.DataSource = dataset;
                dgvGeneralLedger.Columns[0].Visible = false;
                pdfCreator.AddGeneralLedgerPage(lstGeneralLedger, 1);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Application Error. err:" + ex.ToString());
        }
        finally
        {
            conn.Close();
        }






    }

当你只需要一个 DataTable 时,为什么要使用 DataSet,这没有任何意义。只用一个数据表:

DataTable dataTableSource = new DataTable();
adapter.Fill(dataTableSource);

// .. cotinue your code


dgvGeneralLedger.DataSource = dataTableSource;

帐号问题:

您可以使用 SqlParameter 而不是对值进行硬编码。像这样:

command.Parameters.AddWithValue("@acc", "8200030");
dataTableSource.Load(command.ExecuteReader())
// .. store results ..

// second account
command.Parameters["@acc"].Value = "8200031";
dataTableSource.Load(command.ExecuteReader())

// continue with the rest

但是您需要将您的查询更改为:

"... WHERE accnumber = @acc ...."