【TeeChart .NET教程】(八)ADO.NET數(shù)據(jù)庫訪問
【下載TeeChart.Net最新版本】
(一)簡介
將TeeChart連接到ADO.NET數(shù)據(jù)庫可以在設(shè)計(jì)時(shí)使用TeeChart編輯器完成,并在運(yùn)行時(shí)使用幾行代碼完成,任何Series都可以使用TeeChart Editor連接到ADO.NET表或查詢。每個(gè)系列都可以使用相同或不同的ADO.NET數(shù)據(jù)庫連接到相同或不同的表或查詢,此設(shè)計(jì)為用戶提供了如何將圖表連接到數(shù)據(jù)庫的完全靈活性,因?yàn)橛脩舨粌H限于一個(gè)數(shù)據(jù)庫,也不僅限于一個(gè)表或查詢。在設(shè)計(jì)時(shí)也會(huì)檢索數(shù)據(jù),因此用戶可以在開發(fā)期間查看實(shí)際數(shù)據(jù)。
(二)在設(shè)計(jì)時(shí)連接到ADO.NET
2.1 DataAdapter [VS2003]
在設(shè)計(jì)時(shí),將Series連接到ADO.NET數(shù)據(jù)源所需的步驟是:
- 從VS.NET ToolBox的Data選項(xiàng)卡中,將OleDbDataAdapter拖到WinForm上,并在其上添加TeeChart for .Net對象。
- 單擊“Data Adapter Configuration Wizard(數(shù)據(jù)適配器配置向?qū)В?rdquo;上的“Next”,然后在下一頁上單擊“New Connection...(新建連接...)”按鈕。
- 現(xiàn)在單擊新窗口的“Provider”選項(xiàng)卡,然后選擇“Microsoft Jet OLE DB Provider”,如下圖所示:
- 現(xiàn)在單擊同一窗口的“Connection”選項(xiàng)卡,然后從TeeChart for .Net根目錄下的Sample Data文件夾中選擇TeeChart.mdb文件:
- 單擊“Test Connection”按鈕以測試連接,然后單擊同一窗口的“OK”按鈕。
- 單擊Data Adapter Configuration Wizard窗口的“Next”按鈕,從出現(xiàn)的窗口中選擇“Use SQL Statements”,然后再次單擊“Next”按鈕。
- 單擊“Query Builder”按鈕并添加“Employee”表。關(guān)閉“Add Table”窗口并選擇“*(All Columns)”復(fù)選框:
- 確定此窗口,單擊“Next”,然后單擊“Finish”。
- 打開TeeChart Editor,添加BarSeries并導(dǎo)航到Series選項(xiàng)卡的DataSource選項(xiàng)卡。
- 從ComboBox中選擇“Database”,從DataSet ComboBox中選擇oleDbDataAdapter1 [System.Data.OleDb.OleDbDataAdapter]。
- 將標(biāo)簽設(shè)置為“LASTNAME”,將Y設(shè)置為“SALARY”,如下圖所示:
- 單擊Apply按鈕并運(yùn)行表單,應(yīng)該有以下內(nèi)容:
2.2 BindingSource [VS2005,VS2008,VS2010,VS2012,VS2013]
在設(shè)計(jì)時(shí),將Series連接到ADO.NET數(shù)據(jù)源所需的步驟如下:
- 從VS.NET ToolBox的Data選項(xiàng)卡中,將BindingSource拖到一個(gè)帶有TeeChart for .Net對象的WinForm上。
- 轉(zhuǎn)到Data選項(xiàng)卡,然后選擇“Show Data Source”
- 右鍵單擊“Data Source”工具箱,然后選擇“Add New Data Source”。
- 選擇Database,選擇/創(chuàng)建連接字符串,使用Microsoft Access DataBase File(OLE DB),單擊continue: 注意:單擊“Test Connection”按鈕以檢查數(shù)據(jù)源的連接。
- 最后在Browse中選擇要連接的表:
- 從“data source”工具箱中選擇要用作數(shù)據(jù)源的表,然后將其拖到表單中。
- 打開TeeChart Editor,添加BarSeries并導(dǎo)航到Series選項(xiàng)卡的DataSource選項(xiàng)卡。
- 從ComboBox中選擇“Database”,從DataSet ComboBox中選擇bindingSource1。
- 將標(biāo)簽設(shè)置為“LASTNAME”,將Y設(shè)置為“SALARY”,如下圖所示:
- 單擊Apply按鈕并運(yùn)行表單,有以下內(nèi)容:
2.3 單記錄
單記錄圖表允許在TeeChart上顯示來自單個(gè)數(shù)據(jù)庫記錄的所有或一些記錄。它對數(shù)據(jù)庫視圖或表有用,例如,每年的銷售數(shù)據(jù)存儲(chǔ)為單獨(dú)的月份列,“JAN”,“FEB”,“MAR”等。并逐步顯示記錄顯示連續(xù)幾年的月銷售額,單記錄圖表支持?jǐn)?shù)據(jù)集,表格和數(shù)據(jù)視圖,使用圖表編輯器選擇單個(gè)記錄作為圖表系列的數(shù)據(jù)源類型。
連接單記錄數(shù)據(jù)所需的大多數(shù)步驟都是通過圖表編輯器實(shí)現(xiàn)的,要導(dǎo)航數(shù)據(jù)源并相應(yīng)地更新圖表,需要使用Windows.Form的CurrencyManager編寫幾行代碼。
(三)在運(yùn)行時(shí)連接到ADO.NET
3.1 [VERSION .NET 2003] DataAdapter
上述設(shè)計(jì)時(shí)步驟可以用幾行代碼以編程方式重現(xiàn)。這使用戶可以更自由地控制數(shù)據(jù)庫操作。每個(gè)Series都有“DataSource”屬性,它確定數(shù)據(jù)庫值的來源(表或查詢),以及“YValues.DataMember”和“LabelMember”屬性,以指定我們要繪制的字段。將新的TeeChart for .Net對象拖到新項(xiàng)目和新表單上。
[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
3.2 [VERSION .NET 2005] DataAdapter
[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
如果數(shù)據(jù)庫值已更改,并且想要“refresh”連接并再次檢索值,則應(yīng)執(zhí)行以下操作:
TChart1.Series(0).CheckDataSource
“CheckDataSource”方法將強(qiáng)制Series再次檢索所有記錄,所有系列都有“XValues.ValueSource”屬性,此屬性僅用于XY圖表(為每個(gè)點(diǎn)設(shè)置X坐標(biāo)時(shí))。
3.3 單記錄
連接使用圖表編輯器將在表格的初始化中創(chuàng)建以下代碼,可以在運(yùn)行時(shí)使用類似的代碼來創(chuàng)建和連接新的SingleRecord數(shù)據(jù)源。
(假設(shè)一個(gè)名為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;
表格導(dǎo)航 TeeChart的SingleRecord使用CurrencyManager組件導(dǎo)航數(shù)據(jù)庫表并更新圖表。更改表記錄后,使用CheckDatasource更新圖表。例:
private CurrencyManager myCurrencyManager; public void ConnectChartToTable() { myCurrencyManager = (CurrencyManager)this.BindingContext[sourceTable]; singleRecordSource1.RecordCurrency=myCurrencyManager; tChart1[0].CheckDataSource(); }
可以使用CurrencyManager導(dǎo)航表(圖表)
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中使用ADO.NET
用戶可以使用ASP.NET WebForm上的TeeChart WebChart通過ASP.NET連接數(shù)據(jù)源,其方式幾乎與數(shù)據(jù)源可以連接到WinForm上的TeeChart組件的方式相同。