基本信息
源码名称:读取ACCESS数据库的数据,并显示到chart控件和DataGridView中
源码大小:3.00M
文件格式:.rar
开发语言:C#
更新时间:2020-02-12
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
读取Access数据库表格的数据,将数据用datagridview和chart控件显示出来




ACCESS数据库操作代码

public class AccessOperator
    {
        private string conn_str = null;
        private OleDbConnection ole_connection = null;
        private OleDbCommand ole_command = null;
        private OleDbDataReader ole_reader = null;
        private DataTable dt = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        public AccessOperator()
        {
            //conn_str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" Environment.CurrentDirectory "\\Data\\test_data.mdb'";//office 2003
            conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" Environment.CurrentDirectory "\\Data\\test_data.mdb'";//office 2007
            InitDB();
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db_path">数据库路径</param>
        public AccessOperator(string db_path)
        {
            //conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" db_path "'";
            conn_str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" db_path "'";
            InitDB();
        }
        private void InitDB()
        {
            ole_connection = new OleDbConnection(conn_str);//创建实例
            ole_command = new OleDbCommand();
        }

        /// <summary>
        /// 转换数据格式
        /// </summary>
        /// <param name="reader">数据源</param>
        /// <returns>数据列表</returns>
        private DataTable ConvertOleDbReaderToDataTable(ref OleDbDataReader reader)
        {
            DataTable dt_tmp = null;
            DataRow dr = null;
            int data_column_count = 0;
            int i = 0;

            data_column_count = reader.FieldCount;
            dt_tmp = BuildAndInitDataTable(data_column_count, ref reader);

            if (dt_tmp == null)
            {
                return null;
            }

            while (reader.Read())
            {
                dr = dt_tmp.NewRow();

                for (i = 0; i < data_column_count; i)
                {
                    dr[i] = reader[i];
                }

                dt_tmp.Rows.Add(dr);
            }

            return dt_tmp;
        }
        /// <summary>
        /// 创建并初始化数据列表
        /// </summary>
        /// <param name="Field_Count">列的个数</param>
        /// <returns>数据列表</returns>
        /// 按序号创建列的名称
        private DataTable BuildAndInitDataTable(int Field_Count)
        {
            DataTable dt_tmp = null;
            DataColumn dc = null;
            int i = 0;

            if (Field_Count <= 0)
            {
                return null;
            }

            dt_tmp = new DataTable();

            for (i = 0; i < Field_Count; i)
            {
                dc = new DataColumn(i.ToString());
                dt_tmp.Columns.Add(dc);
            }

            return dt_tmp;
        }
        /// <summary>
        /// 创建并初始化数据列表
        /// </summary>
        /// <param name="Field_Count">列的个数</param>
        /// <returns>数据列表</returns>
        /// 根据读到的表字段名称,创建列名
        private DataTable BuildAndInitDataTable(int Field_Count, ref OleDbDataReader reader)
        {
            DataTable dt_tmp = null;
            DataColumn dc = null;
            int i = 0;

            if (Field_Count <= 0)
            {
                return null;
            }

            dt_tmp = new DataTable();

            for (i = 0; i < Field_Count; i)
            {
                dc = new DataColumn(reader.GetName(i));
                dt_tmp.Columns.Add(dc);
            }

            return dt_tmp;
        }
        /// <summary>
        /// 从数据库里面获取数据
        /// </summary>
        /// <param name="strSql">查询语句</param>
        /// <returns>数据列表</returns>
        public DataTable GetDataTableFromDB(string strSql)
        {
            if (conn_str == null)
            {
                return null;
            }
            try
            {
                ole_connection.Open();//打开连接

                if (ole_connection.State == ConnectionState.Closed)
                {
                    return null;
                }

                ole_command.CommandText = strSql;
                ole_command.Connection = ole_connection;

                ole_reader = ole_command.ExecuteReader(CommandBehavior.Default);

                dt = ConvertOleDbReaderToDataTable(ref ole_reader);

                ole_reader.Close();
                ole_reader.Dispose();
            }
            catch (System.Exception e)
            {
                //Console.WriteLine(e.ToString());
                MessageBox.Show("A1:" e.Message);
            }
            finally
            {
                if (ole_connection.State != ConnectionState.Closed)
                {
                    ole_connection.Close();
                }
            }

            return dt;
        }
        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <returns>返回结果</returns>
        public int ExcuteSql(string strSql)
        {
            int nResult = 0;

            try
            {
                ole_connection.Open();//打开数据库连接
                if (ole_connection.State == ConnectionState.Closed)
                {
                    return nResult;
                }

                ole_command.Connection = ole_connection;
                ole_command.CommandText = strSql;

                nResult = ole_command.ExecuteNonQuery();
            }
            catch (System.Exception e)
            {
                //Console.WriteLine(e.ToString());
                MessageBox.Show("A2:" e.Message);
                return nResult;
            }
            finally
            {
                if (ole_connection.State != ConnectionState.Closed)
                {
                    ole_connection.Close();
                }
            }

            return nResult;
        }
        /// <summary>
        /// 从数据库里面获取数据
        /// </summary>
        /// <param name="strSql">查询语句</param>
        /// <returns>数据列表</returns>
        public object ScalarTableFromDB(string strSql)
        {
            object nResult = 0;

            try
            {
                ole_connection.Open();//打开数据库连接
                if (ole_connection.State == ConnectionState.Closed)
                {
                    return nResult;
                }

                ole_command.Connection = ole_connection;
                ole_command.CommandText = strSql;

                nResult = ole_command.ExecuteScalar();
            }
            catch (System.Exception e)
            {
                //Console.WriteLine(e.ToString());
                MessageBox.Show("A3:" e.Message);
                return nResult;
            }
            finally
            {
                if (ole_connection.State != ConnectionState.Closed)
                {
                    ole_connection.Close();
                }
            }

            return nResult;
        }
        


      
        /// <summary>
        /// 查询数据库是否存在某条记录
        /// </summary>
        /// P_str_Process进程名称

        public Boolean InquiryRecordFromAccess(string tablename, string fieldname, string inquiryvaluename)//关闭进程
        {
            string sql = "Select * from " tablename " where " fieldname " ='" inquiryvaluename "'";
            object ret = ScalarTableFromDB(sql);
            if (object.Equals(ret, null))
            {
                return false;

            }
            else
            {
                return true;
            }
        }





        /// <summary>
        /// 关闭指定的进程
        /// </summary>
        /// P_str_Process进程名称

        private void CloseProcess(string P_str_Process)//关闭进程
        {
            System.Diagnostics.Process[] excelProcess = System.Diagnostics.Process.GetProcessesByName(P_str_Process);//实例化进程对象
            foreach (System.Diagnostics.Process p in excelProcess)
                p.Kill();//关闭进程
            System.Threading.Thread.Sleep(10);//使线程休眠10毫秒
        }


    }

chart设置代码

private void chartdisplay(DataTable data)
        {
            chart1.DataSource = data;


            //设置图表背景
            this.chart1.BorderSkin.SkinStyle = BorderSkinStyle.FrameThin1;//图标边框外观样式
            this.chart1.BorderSkin.BackColor = Color.Blue;//图标背景颜色
            this.chart1.Width = 1200;//图表的宽度
            this.chart1.Height = 520;//图表的高度
            //设置图表区
            this.chart1.ChartAreas[0].BackGradientStyle = GradientStyle.None;//背景渐变样式设置
            this.chart1.ChartAreas[0].ShadowColor = Color.Gainsboro;//背景渐变色设置
            this.chart1.ChartAreas[0].ShadowOffset = 1;//设置阴影的偏移量
            this.chart1.ChartAreas[0].BorderDashStyle = ChartDashStyle.Solid;//边框的样式设置
            this.chart1.ChartAreas[0].BorderColor = Color.Black;//边框的颜色设置
            this.chart1.ChartAreas[0].BackColor = Color.Black;//数据区域背景颜色设置
            //X轴网格线设置
            this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.White;//X轴网格线颜色
            this.chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;//X轴网格线类型
            this.chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 1;//X轴网格线宽度
            this.chart1.ChartAreas[0].AxisX.MajorGrid.IntervalOffset = 1;   //网格线距X轴的偏移量
            this.chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 1;    //网格X间距
            this.chart1.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.Lines; //X轴箭头样式;           
            this.chart1.ChartAreas[0].AxisX.Interval = 1;
            this.chart1.ChartAreas[0].AxisX.IntervalOffset = 0;
            this.chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -30;  //设置X轴标签角度
            //X轴标题设置
            this.chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Far;
            this.chart1.ChartAreas[0].AxisX.Title = "测试时间";//X轴标题
            //Y轴网格线设置
            this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.White;//Y轴网格线颜色
            this.chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;//Y轴网格线类型
            this.chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 1;//Y轴网格线宽度
            this.chart1.ChartAreas[0].AxisY.MajorGrid.IntervalOffset = 20;   //网格线距Y轴的偏移量                
            this.chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 20;   //网格Y间距
            this.chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.Lines; //Y轴箭头样式;  
            this.chart1.ChartAreas[0].AxisY.Minimum = 0;//Y轴最小显示值
            this.chart1.ChartAreas[0].AxisY.Maximum = 100;//Y值最显示值
            this.chart1.ChartAreas[0].AxisY.Interval = 20;  //Y轴的间距
            this.chart1.ChartAreas[0].AxisY.IntervalOffset = 0; //Y轴的坐标偏移量 
            //Y轴标题设置
            this.chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Far;
            this.chart1.ChartAreas[0].AxisY.Title = "压力值";//X轴标题






            //图表3D设置
            //chart1.ChartAreas[0].Area3DStyle.Enable3D = false;
            //chart1.ChartAreas[0].Area3DStyle.LightStyle = LightStyle.Realistic;
            //滚动条设置
            this.chart1.ChartAreas[0].CursorX.AutoScroll = true;
            this.chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
            this.chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            this.chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
            this.chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            this.chart1.ChartAreas[0].AxisX.ScaleView.Position = 1;
            this.chart1.ChartAreas[0].AxisX.ScaleView.Size = 30;  //设置滚动条包含的数据数量,此参数默认不设置,不设置滚动不显示
            this.chart1.ChartAreas[0].AxisX.ScrollBar.ButtonColor = System.Drawing.Color.Silver;
            this.chart1.ChartAreas[0].AxisX.ScrollBar.LineColor = System.Drawing.Color.White;



            //////////////////////Series属性设置///////////////////////////
            this.chart1.Series[0].XValueType = ChartValueType.DateTime;//设置X轴数据类型为时间
            this.chart1.Series[0].YValueType = ChartValueType.Double;//设置Y轴数据类型为双精度浮点数
            this.chart1.Series[0].ChartType = SeriesChartType.Line;  //设置图表类型为折线            
            this.chart1.Series[0].IsValueShownAsLabel = true;  //是否在数据点上显示值
            this.chart1.Series[0].MarkerStyle = MarkerStyle.Circle;  // 标记数据点类型
            this.chart1.Series[0].MarkerSize = 8;  //标记数据点大小
            this.chart1.Series[0].MarkerColor = Color.White;//标记数据点的颜色
            this.chart1.Series[0].MarkerBorderColor = Color.Green;//标记数据点边框的颜色
            this.chart1.Series[0].MarkerBorderWidth = 2;//标记数据点边框的宽度
            this.chart1.Series[0].BorderDashStyle = ChartDashStyle.Solid;
            //this.chart1.Series[0].BorderColor = Color.Yellow ;//直方图时才有
            this.chart1.Series[0].LabelAngle = 45;
            this.chart1.Series[0].LabelBackColor = Color.Blue;
            this.chart1.Series[0].LabelBorderColor = Color.Black;
            this.chart1.Series[0].LabelForeColor = Color.White;//标记数据点字体的颜色           
            this.chart1.Series[0].Color = Color.Red;//曲线颜色
            this.chart1.Series[0].BorderWidth = 2;//曲线宽度
            this.chart1.Series[0].Name = "压力采集";//曲线名称

            chart1.Series[0].YValueMembers = "压力检测值";
            chart1.Series[0].XValueMember = "测试时间";
            chart1.Series[0].Points.Clear()