2012年12月20日木曜日

C# でAccessデータベースをチャート化

C# でAccessデータベースの内容を取り出してチャートを表示するサンプルです。 予めAccessのmdbに、株価のデータを格納しておきます。(データベース名は、c:\mdb\stock_mdb.mdbです。テーブル名は、stock_mdb です。フィールド名は、meigara,hiduke,evalue,hvalue などです。) また予めフォームにチャートコントロールを張り付けておきます。引数として、code と miniYを与えます。 code は銘柄コードでテーブルのmeigara に相当します。miniYはチャートの表示の下限を表します。
private void display_chart(int code , int miniY)
{
chart1.ChartAreas[0].AxisY.Minimum = miniY;
String meigara = code.ToString();
// Resolve the address to the Access database
// string fileNameString = @"stock_mdb.mdb";
string fileNameString = @"c:\mdb\stock_mdb.mdb";
// Initialize a connection string
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;
// Define the database query
string mySelectQuery = "SELECT * FROM table_stock where meigara='" + meigara + "' order by hiduke;";
// 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);
myConnection.Open();
// set chart data source - the data source must implement IEnumerable
chart1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
// Set series members names for the X and Y values
chart1.Series[0].XValueMember = "hiduke";
chart1.Series[0].YValueMembers = "evalue";
chart1.Series[0].ChartType = SeriesChartType.Line;
chart1.Series[1].XValueMember = "hiduke";
chart1.Series[1].YValueMembers = "hvalue";
chart1.Series[1].ChartType = SeriesChartType.Line;
// Data bind to the selected data source
//chart1.DataBind();
}
view raw gistfile1.cs hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿