我收到了这个例外:
chart1.DataBindTable(myReader, "Name");
这里;完整的代码:
// Access database
//System.Windows.Forms.DataVisualization.Charting.Utilities.SampleMain.MainForm mainForm = (System.Windows.Forms.DataVisualization.Charting.Utilities.SampleMain.MainForm)this.ParentForm;
//string fileNameString = "\\data\\chartdata.mdb";
// Initialize a connection string
string myConnectionString = "Provider=SQLOLEDB;Data Source=hermes;Initial Catalog=qcvaluestest;Integrated Security=SSPI;";
// Define the database query
string mySelectQuery = "SELECT name, finalconc from qvalues where rowid in (20365,20366,20367);";
// 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();
我在做错了什么?我知道应该连接。也许sql语句没有返回任何东西?
答案 0 :(得分:2)
我不确定,但请尝试在myReader.Read()
之前使用DataBindTable
或尝试此操作:
OleDbDataAdapter da = new OleDbDataAdapter(myCommand);
DataTable data = new DataTable();
da.Fill(data);
chart1.DataBindTable(data.AsDataView(), "name");