c#:使用数据绑定控件与硬编码有什么好处?

时间:2010-11-01 21:10:28

标签: c# .net sql

我正在使用ms图表控件在winform上制作有趣的图表。使用数据绑定的优点是什么,而不是像这样说:

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();
... 

1 个答案:

答案 0 :(得分:1)

与MS聊天控制对象进行数据绑定而不是手动编程是为了保持一致性和性能。

http://msdn.microsoft.com/en-us/library/bb613546.aspx

如果您的应用程序需要高性能,我建议尽可能将组件与数据源绑定。