13  DATABASE FUNCTION
13.3  CPU Module Database Access (from External Device) Function
257
13
■Source code of the sample program
The following describes the source code of the sample program.
 • Development environment: Visual Studio 2015
 • Programming language: C#
namespace iQ_R_DB_Access
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            // Range setting of X-/Y-axis on a graph
            chart1.ChartAreas[0].AxisX.Minimum = 400;
            chart1.ChartAreas[0].AxisX.Maximum = 600;
            chart1.ChartAreas[0].AxisY.Minimum = 400;
            chart1.ChartAreas[0].AxisY.Maximum = 600;
            // Initial value (all) set to the judge value of search target
            cmbJudge.SelectedIndex = 2;
        }
        /// <summary>
        /// Operation when the Search button is clicked
        /// Records that match the judge value specified by the combo box are acquired and displayed in a list and a scatter plot of X and Y field values.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            // The number of fields (the number of fields on the CheckData table)
            int len = 6;
            // Acquiring the data source name
            string strConnect = txtDataSourceName.Text.ToString();
            // ODBC connection information management class
            OdbcConnectionStringBuilder o = new OdbcConnectionStringBuilder();
            o.Dsn = strConnect;
            // Creating the connection object
            OdbcConnection cn = new OdbcConnection(o.ConnectionString);
            // SQL statement
            string strQuery = GetSQL();
            // Creating the command object
            OdbcCommand cmd = new OdbcCommand(strQuery, cn);
            // Initializing the list
            dtRecord.Rows.Clear();
            // Initializing the graph
            chart1.Series[0].Points.Clear();
            try
            {
   // Connecting to DB
   cn.Open();
   // Creating the data reader object
   OdbcDataReader reader = cmd.ExecuteReader();