如何将导入的数据用作数据库?

莫格

我目前有一个程序,可以将Excel数据以表格格式导入到datgrid中。

代码在这里:

 private void browse_Click(object sender, EventArgs e)
    {
        OpenFileDialog opfd = new OpenFileDialog();
        if (opfd.ShowDialog() == DialogResult.OK)
            textselect.Text = opfd.FileName;
    }

    private void showdata_Click(object sender, EventArgs e)
    {
        try {
            System.Data.OleDb.OleDbConnection MyConnection;
            System.Data.DataSet DtSet;
            System.Data.OleDb.OleDbDataAdapter MyCommand;
            MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textselect.Text + "; Extended Properties = Excel 8.0");
            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + textchoice.Text + "$]", MyConnection);
            MyCommand.TableMappings.Add("Table", "TestTable");
            DtSet = new System.Data.DataSet();
            MyCommand.Fill(DtSet);
            dataGridView.DataSource = DtSet.Tables[0];
            MyConnection.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

浏览和显示数据是一个按钮,而文本选择和文本选择是文本框。

这样就成功创建了一个我想在图表上绘制的表格。

我当前的图表程序使用的数据库必须手动将数据插入其中,但我想使用该程序为我导入的数据。

当前图表的代码(从简单数据库中提取):

namespace StockCharts
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

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

            }

            private void btnSave_Click(object sender, EventArgs e)
            {
                try
                {
                    stocksBindingSource.EndEdit();
                    stocksTableAdapter.Update(database.Stocks);
                    Refresh();
                    MessageBox.Show("Your data has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            private void btnLoad_Click(object sender, EventArgs e)
            {
                //Clear Grid
                chart.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 0;
                chart.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 0;
                //
                chart.Series["Daily"].XValueMember = "Day";
                chart.Series["Daily"].YValueMembers = "High,Low,Open,Close";
                chart.Series["Daily"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
                chart.Series["Daily"].CustomProperties = "PriceDownColor=Red,PriceUpColor=Green";
                //chart.Series["Daily"]["OpenCloseStyle"] = "Triangle";
                chart.Series["Daily"]["ShowOpenClose"] = "Both";
                chart.DataManipulator.IsStartFromFirst = true;
                chart.DataSource = database.Stocks;
                chart.DataBind();
            }

是否可以将导入的数据分配为数据集供我的图表使用?

我能否从发布的第一组代码中加载新数据?

理想情况下,我想利用已设置的“加载”按钮,而无视或重新利用已创建的数据库。

提前致谢!

佩尤什·辛格

你的评论让我有些困惑 How could I link showdata function to a newly made datatable?

这就是我所假设的:btnLoad_Click是您现有的功能,它与Form1_Load结合使用可以帮助您根据插入数据库中的数据来绘制图表。现在,这些数据来自正在加载的excel文件showdata_Click从excel加载数据后,我假设您将其保存在数据库中,然后在图表中进行绘制。

您要删除将数据保存到数据库中并直接将excel中加载的数据绑定到图表数据源的步骤。

您可以通过更改图表的数据源chart.DataSource = database.Stocks;来执行此操作,如果您将其绑定到自己的数据源,则可以这样做datatable chart.DataSource = DtSet.Tables[0];(只要表和stocks表的结构相同)就可以了。

发布代码后,所做的更改使数据集公开,并以表单加载而不是按钮单击事件加载数据。

public partial class Form1 : Form
        {
        System.Data.DataSet DtSet = new System.Data.DataSet();

            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'database.Stocks' table. You can move, or remove it, as needed.

                // this.stocksTableAdapter.Fill(this.database.Stocks);
                   LoadData();

            }

    private void LoadData()
    {
        try {
                System.Data.OleDb.OleDbConnection MyConnection;
                // System.Data.DataSet DtSet;
                System.Data.OleDb.OleDbDataAdapter MyCommand;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textselect.Text + "; Extended Properties = Excel 8.0");
                MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + textchoice.Text + "$]", MyConnection);
                MyCommand.TableMappings.Add("Table", "TestTable");
                // DtSet = new System.Data.DataSet();
                MyCommand.Fill(DtSet);
                dataGridView.DataSource = DtSet.Tables[0];
                MyConnection.Close();

            }
            catch (Exception ex)
            {
             MessageBox.Show(ex.ToString());
            }
    }       

            private void btnLoad_Click(object sender, EventArgs e)
            {
                // rest of the function stays same, only change the line shown below
                // chart.DataSource = database.Stocks;
        chart.DataSource = DtSet.Tables[0];

            }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章