2010-11-01 56 views
0

我使用ms图表控件在winform上制作有趣的图表。什么是使用数据绑定,而不是说像这样的优势:c#:使用数据绑定控制vs硬编码有什么优势?

using System.Windows.Forms.DataVisualization.Charting; 
using System.Data; 
using System.Data.OleDb; 
... 

// Access database 
System.Windows.Forms.DataVisualization.Charting.Utilities.SampleMain.MainForm mainForm = (System.Windows.Forms.DataVisualization.Charting.Utilities.SampleMain.MainForm)this.ParentForm; 
string fileNameString = mainForm.applicationPath + "\\data\\chartdata.mdb"; 

// Initialize a connection string  
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString; 

// Define the database query  
string mySelectQuery="SELECT Name, Sales FROM REPS;"; 

// Create a database connection object using the connection string  
OleDbConnection myConnection = new OleDbConnection(myConnectionString); 

// Create a database command on the connection using query  
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection); 

// Open the connection  
myCommand.Connection.Open(); 

// Create a database reader  
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); 

// Since the reader implements and IEnumerable, pass the reader directly into 
// the DataBindTable method with the name of the Column to be used as the XValue 
Chart1.DataBindTable(myReader, "Name"); 

// Close the reader and the connection 
myReader.Close(); 
myConnection.Close(); 
... 
+0

真的是你的问题? – yonan2236 2010-11-01 23:50:16

+0

@yanon shalom,问题是我想从数据库传递数据到ms聊天控件 – 2010-11-02 01:23:02

+0

仍然问题不清楚。您可能想尝试提供更多详情 – PradeepGB 2010-11-02 13:38:26

回答