原创|使用教程|编辑:王香|2018-08-07 16:15:05.000|阅读 553 次
概述:本文详细介绍了如何在TeeChart中使用ADO.NET数据库访问
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
相关链接:
【下载TeeChart.Net最新版本】
将TeeChart连接到ADO.NET数据库可以在设计时使用TeeChart编辑器完成,并在运行时使用几行代码完成,任何Series都可以使用TeeChart Editor连接到ADO.NET表或查询。每个系列都可以使用相同或不同的ADO.NET数据库连接到相同或不同的表或查询,此设计为用户提供了如何将图表连接到数据库的完全灵活性,因为用户不仅限于一个数据库,也不仅限于一个表或查询。在设计时也会检索数据,因此用户可以在开发期间查看实际数据。
在设计时,将Series连接到ADO.NET数据源所需的步骤是:
在设计时,将Series连接到ADO.NET数据源所需的步骤如下:
单记录图表允许在TeeChart上显示来自单个数据库记录的所有或一些记录。它对数据库视图或表有用,例如,每年的销售数据存储为单独的月份列,“JAN”,“FEB”,“MAR”等。并逐步显示记录显示连续几年的月销售额,单记录图表支持数据集,表格和数据视图,使用图表编辑器选择单个记录作为图表系列的数据源类型。
连接单记录数据所需的大多数步骤都是通过图表编辑器实现的,要导航数据源并相应地更新图表,需要使用Windows.Form的CurrencyManager编写几行代码。
上述设计时步骤可以用几行代码以编程方式重现。这使用户可以更自由地控制数据库操作。每个Series都有“DataSource”属性,它确定数据库值的来源(表或查询),以及“YValues.DataMember”和“LabelMember”属性,以指定我们要绘制的字段。将新的TeeChart for .Net对象拖到新项目和新表单上。
[C#.Net]
using System.Data; using System.Data.OleDb; using System.Security; using System.Security.Permissions; private void Form1_Load(object sender, System.EventArgs e) { DataSet masterDataSet = new DataSet(); Bar bar1 = new Bar(tChart1.Chart); try { OpenConnWithJet40(ref masterDataSet); DataTable employeeTable = masterDataSet.Tables["EMPLOYEE"]; bar1.YValues.DataMember = employeeTable.Columns["SALARY"].ToString(); bar1.LabelMember = employeeTable.Columns["LASTNAME"].ToString(); bar1.DataSource = employeeTable; } catch (SecurityException) { MessageBox.Show("The calling method has not been granted sufficient permission to access the Microsoft Jet 4.0 OLE DB Provider."); } } [OleDbPermission(SecurityAction.Demand, Provider = "Microsoft.Jet.OLEDB.4.0")] private void OpenConnWithJet40(ref DataSet masterDataSet) { OleDbConnection testConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=C:\Program Files\Steema Software\TeeChart for .NET v3\Sample data\TeeChart.mdb;"); testConn.Open(); string strCom = "SELECT * FROM Employee"; OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom,testConn); myCommand.Fill(masterDataSet, "Employee"); testConn.Close(); }
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim MasterDataSet As New DataSet() Dim Bar1 As New Steema.TeeChart.Styles.Bar(TChart1.Chart) Try OpenConnWithJet40(MasterDataSet) Dim employeeTable As DataTable = MasterDataSet.Tables("Employee") Bar1.YValues.DataMember = employeeTable.Columns("SALARY").ToString() Bar1.LabelMember = employeeTable.Columns("LASTNAME").ToString() Bar1.DataSource = employeeTable Catch ex As Exception MessageBox.Show("Microsoft Jet 4.0 OLE DB Provider problem: " & ex.Message) End Try End Sub Private Sub OpenConnWithJet40(ByRef MasterDataSet As DataSet) Dim TestConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Program Files\Steema Software\TeeChart for .NET v3\Sample data\TeeChart.mdb;") TestConn.Open() Dim StrCom As String = "SELECT * FROM Employee" Dim MyCommand As New OleDbDataAdapter(StrCom, TestConn) MyCommand.Fill(MasterDataSet, "Employee") TestConn.Close() End Sub End Class
[C#.Net]
using System.Data; using System.Data.OleDb; using System.Security; using System.Security.Permissions; private void Form1_Load(object sender, EventArgs e) { //string ConnString = SqlDataSource1.ConnectionString; string sQuery = "select * from Employee"; //SqlConnection myConnection = new SqlConnection(ConnString); string myConnString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Program Files\\Steema Software\\TeeChart for .NET v3\\Sample data\\TeeChart.mdb;Uid=Admin;Pwd=;"; System.Data.Odbc.OdbcConnection myConnection = new System.Data.Odbc.OdbcConnection(myConnString); //SqlCommand myCommand = new SqlCommand(sQuery, myConnection); System.Data.Odbc.OdbcCommand myCommand = new System.Data.Odbc.OdbcCommand(sQuery, myConnection); myConnection.Open(); //SqlDataReader myReader = myCommand.ExecuteReader(); System.Data.Odbc.OdbcDataReader myReader = myCommand.ExecuteReader(); int XColumn = myReader.GetOrdinal("ID"); int YColumn = myReader.GetOrdinal("SALARY"); int LabelColumn = myReader.GetOrdinal("LASTNAME"); Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart); while (myReader.Read()) { bar1.Add(Convert.ToInt32(myReader[XColumn]), Convert.ToDouble(myReader[YColumn]), Convert.ToString(myReader[LabelColumn])); } myReader.Close(); myConnection.Close(); }
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim MasterDataSet As New DataSet() Dim Bar1 As New Steema.TeeChart.Styles.Bar(TChart1.Chart) Try OpenConnWithJet40(MasterDataSet) Dim employeeTable As DataTable = MasterDataSet.Tables("EMPLOYEE") Bar1.YValues.DataMember = employeeTable.Columns("SALARY").ToString() Bar1.LabelMember = employeeTable.Columns("LASTNAME").ToString() Bar1.DataSource = employeeTable Catch ex As Exception MessageBox.Show("Microsoft Jet 4.0 OLE DB Provider problem: " & ex.Message) End Try End Sub Private Sub OpenConnWithJet40(ByRef MasterDataSet As DataSet) Dim TestConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Program Files\Steema Software\TeeChart for .NET v3\Sample data\TeeChart.mdb;") TestConn.Open() Dim StrCom As String = "SELECT * FROM Employee" Dim MyCommand As New OleDb.OleDbDataAdapter(StrCom, TestConn) MyCommand.Fill(MasterDataSet, "Employee") TestConn.Close() End Sub End Class
如果数据库值已更改,并且想要“refresh”连接并再次检索值,则应执行以下操作:
TChart1.Series(0).CheckDataSource
“CheckDataSource”方法将强制Series再次检索所有记录,所有系列都有“XValues.ValueSource”属性,此属性仅用于XY图表(为每个点设置X坐标时)。
连接使用图表编辑器将在表格的初始化中创建以下代码,可以在运行时使用类似的代码来创建和连接新的SingleRecord数据源。
(假设一个名为sourceTableSet的DataSet,带有table:sourceTable)
this.singleRecordSource1 = new Steema.TeeChart.Data.SingleRecordSource(); this.sourceTableSet.DataSetName = "NewDataSet"; this.sourceTableSet.Tables.AddRange(new System.Data.DataTable[] {this.sourceTable}); this.singleRecordSource1.DataSource = this.sourceTableSet; this.singleRecordSource1.ValueMembers = new string[] { "JAN", "FEB", "MAR"}; this.bar1 = new Steema.TeeChart.Styles.Bar(); this.bar1.DataSource = this.singleRecordSource1;
表格导航 TeeChart的SingleRecord使用CurrencyManager组件导航数据库表并更新图表。更改表记录后,使用CheckDatasource更新图表。例:
private CurrencyManager myCurrencyManager; public void ConnectChartToTable() { myCurrencyManager = (CurrencyManager)this.BindingContext[sourceTable]; singleRecordSource1.RecordCurrency=myCurrencyManager; tChart1[0].CheckDataSource(); }
可以使用CurrencyManager导航表(图表)
private void MovePrevious() { if (myCurrencyManager.Position>0) { myCurrencyManager.Position=myCurrencyManager.Position+1; tChart1[0].CheckDataSource(); } } private void MoveNext() { if (myCurrencyManager.Position < myCurrencyManager.Count) { myCurrencyManager.Position=myCurrencyManager.Position+1; tChart1[0].CheckDataSource(); } }
用户可以使用ASP.NET WebForm上的TeeChart WebChart通过ASP.NET连接数据源,其方式几乎与数据源可以连接到WinForm上的TeeChart组件的方式相同。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@capbkgr.cn