在 C# 中设置显示数据集记录出现次数的图表

Setting up a chart that displays the number of times a dataset record appears in C#

我正在尝试创建一个图表,当按下按钮时会显示一个图表,向用户显示一条记录在它所链接的 dataset/table 中出现的次数。请记住,我在 Visual Studios/C#.

中使用图表的经验很少

目前我收到此错误:Error

这是我目前所有的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace RRAS
{
    public partial class formRRAS : Form
    {
        public OleDbConnection DataConnection = new OleDbConnection();

        public formRRAS()
        {
            InitializeComponent();
        }

        private void formRRAS_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'database1DataSet.tblReject_test' table. You can move, or remove it, as needed.
            this.tblReject_testTableAdapter.Fill(this.database1DataSet.tblReject_test);

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            //This creates the String Publisher which grabs the information from the combo box on the form.
            //Select and Dataconnection are also defined here.
            string Select = "SELECT * FROM tblReject_test";
                string DataConnection;
                string Department = txtDepartment.Text;
                string Start_Date = txtStart.Text;
                string End_Date = txtEnd.Text;
                string Anatomy = txtAnatomy.Text;
                string RFR = cmbRFR.Text;
                string Comment = txtComment.Text;

            //Select defines what should be loaded on to the dataset.
            if (Department != "")
            {
                Select = Select + " WHERE department_id =" + "'" + Department + "'";

                if (Anatomy != "")
                {
                    Select = Select + "AND body_part_examined =" + "'" + Anatomy + "'";

                    if (Start_Date != "")
                    {
                        Select = Select + " AND study_date =" + "'" + Start_Date + "'";

                        if (End_Date != "")
                        {
                            Select = Select + " AND study_date =" + "'" + End_Date + "'";

                            if (RFR != "")
                            {
                                Select = Select + " AND reject_category =" + "'" + RFR + "'";

                                if(Comment != "")
                                {
                                    Select = Select + " AND reject_comment =" + "'" + Comment + "'";
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Select = "SELECT * FROM tblReject_test";
            }
            //DataConnection connects to the database.
            string connectiontring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database1.mdb";
            DataConnection = new OleDbConnection(connectiontring);

            //The DataAdapter is the code that ensures both the data in the Select and DataConnection strings match.
            OleDbDataAdapter rdDataAdapter = new OleDbDataAdapter(Select, DataConnection);

            try
            {
                //It then clears the datagridview and loads the data that has been selected from the DataAdapter.
                database1DataSet.tblReject_test.Clear();
                rdDataAdapter.Fill(this.database1DataSet.tblReject_test);
            }
            catch (OleDbException exc)
            {
                System.Windows.Forms.MessageBox.Show(exc.Message);
            }
        }

        private void btnLoadChart_Click(object sender, EventArgs e)
        {

            try
            {
                int count = database1DataSet.Tables["tblReject_test"].Rows.Count;

                DataConnection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = DataConnection;
                string query = "SELECT * FROM tblReject_test";
                command.CommandText = query;

                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    charRejections.Series["RFR"].Points.AddXY(reader["reject_category"].ToString(), reader[count].ToString());
                }

                DataConnection.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }
    }
}

您的代码无法编译,因为您正在将字符串分配给 DataConnection(OleDbConnection 的实例)。

正确的用法应该如下。

string connectiontring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database1.mdb";

DataConnection = new OleDbConnection(connectiontring));

此外,您的代码不会在出现异常时关闭数据库连接。 建议使用如下所示的代码。这是取自 MSDN

using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        try
        {
            connection.Open();
            Console.WriteLine("DataSource: {0} \nDatabase: {1}",
                connection.DataSource, connection.Database);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }