1. 程式人生 > >C#2005 一個簡單的查詢介面程式碼:DataGridView使用、影象顯示、複合查詢樣例

C#2005 一個簡單的查詢介面程式碼:DataGridView使用、影象顯示、複合查詢樣例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Runtime.InteropServices;
using FileControl;

namespace xxxxx
{
    public partial class FrmFind : Form
    {
        private const int CLSLicenseMaxLength = 16;  // 車牌字串最大長度
        private const int CLSLaneMinNumber = 1;      // 車道最小值
        private const int CLSLaneMaxNumber = 255;    // 車道最大值
        private const int CLSSpeedMinNumber = 0;     // 車速最小值
        private const int CLSSpeedMaxNumber = 300;   // 車速最大值
        private const int CLSDefaultReturnRecord = 500; // 查詢預設返回記錄數目
        private const int CLSDefaultLoadRecord = 8;     // 啟動時預設返回記錄數目
        private const int CLSOverSpeedStateNormal = 1303;   // 一般超速狀態程式碼 小於等於50%
        private const int CLSOverSpeedStateSerious = 1603;  // 嚴重超速狀態程式碼 大於50%

        private DataSet clsDSVehicle;     // 記憶體中快取的資料庫表儲存在此物件中 
        private Image clsImageFull;       // 影象顯示物件-全景 
        private Image clsImageSpecial;    // 影象顯示物件-牌識用
        private int clsPreviousRowIndex;  // 前一次顯示記錄的行索引號(在DataSet中)

        public FrmFind()
        {
            InitializeComponent();

            clsPreviousRowIndex = -1; // 前一次行的索引號
        }

        private void FrmFind_Load(object sender, EventArgs e)
        {
            InitCompenent();

            GetVehicleFromDatabase(CLSDefaultLoadRecord, "");
        }

        private void FrmFind_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (clsImageFull != null)
            {
                clsImageFull.Dispose();
            }
            if (clsImageSpecial != null)
            {
                clsImageSpecial.Dispose();
            }

            if (clsDSVehicle != null)
            {
                clsDSVehicle.Dispose();
            }
        }

        /// <summary>
        /// 初始化介面上各個元件的狀態
        /// </summary>
        private void InitCompenent()
        {
            dtpTimeStart.Enabled = false;
            dtpTimeEnd.Enabled = false;
            txtLicenseFind.Enabled = false;
            cboLicenseColor.Enabled = false;
            cboLane.Enabled = false;
            cboSpeedCompareSymbol.Enabled = false;
            cboSpeed.Enabled = false;

            InitDataGridView();
        }

        /// <summary>
        /// 初始化DataGridView控制元件的狀態
        /// </summary>
        private void InitDataGridView()
        {
            dgvVehicle.AutoGenerateColumns = false;
            dgvVehicle.AllowUserToOrderColumns = true;
            dgvVehicle.AllowUserToAddRows = false;
            dgvVehicle.AllowUserToDeleteRows = false;
            // 設定奇數資料行的背景色
            dgvVehicle.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.InactiveCaptionText;
            dgvVehicle.MultiSelect = false;
            // 設定採用行選取模式
            dgvVehicle.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dgvVehicle.VirtualMode = true // 如果含未繫結資料則必須選設為true
            this.dgvVehicle.DataSource = this.bsVehicle;

            DataGridViewTextBoxColumn colautoId = new DataGridViewTextBoxColumn();
            colautoId.DataPropertyName = "autoId"; // 資料來源欄位
            colautoId.HeaderText = "序號";
            colautoId.Name = "自增長序號";
            colautoId.Width = 40;
            colautoId.SortMode = DataGridViewColumnSortMode.NotSortable; // 不允許按列排序
            dgvVehicle.Columns.Add(colautoId);

            DataGridViewTextBoxColumn colindex = new DataGridViewTextBoxColumn();
            colindex.DataPropertyName = "index";
            colindex.HeaderText = "索引";
            colindex.Name = "車輛索引號";
            colindex.Width = 40;
            colindex.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(colindex);

            DataGridViewTextBoxColumn collicense = new DataGridViewTextBoxColumn();
            collicense.DataPropertyName = "license";
            collicense.HeaderText = "車牌號碼";
            collicense.Name = "車牌號碼";
            collicense.Width =70;
            collicense.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(collicense);

            DataGridViewTextBoxColumn collicenseColor = new DataGridViewTextBoxColumn();
            collicenseColor.DataPropertyName = "licenseColor";
            collicenseColor.HeaderText = "顏色";
            collicenseColor.Name = "車牌顏色";
            collicenseColor.Width = 35;
            collicenseColor.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(collicenseColor);

            DataGridViewTextBoxColumn collicenseType = new DataGridViewTextBoxColumn();
            collicenseType.DataPropertyName = "licenseType";
            collicenseType.HeaderText = "型別";
            collicenseType.Name = "車牌型別";
            collicenseType.Width = 0;
            collicenseType.SortMode = DataGridViewColumnSortMode.NotSortable;
            collicenseType.Visible = false;
            dgvVehicle.Columns.Add(collicenseType);

            DataGridViewTextBoxColumn colpassTime = new DataGridViewTextBoxColumn();
            colpassTime.DataPropertyName = "passTime"; // 資料來源欄位
            colpassTime.DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            colpassTime.HeaderText = "通行時間";
            colpassTime.Name = "通行時間";
            colpassTime.Width = 125;
            colpassTime.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(colpassTime);

            DataGridViewTextBoxColumn collocation = new DataGridViewTextBoxColumn();
            collocation.DataPropertyName = "location"; // 資料來源欄位
            collocation.HeaderText = "地點";
            collocation.Name = "影象採集地點";
            collocation.Width = 120;
            collocation.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(collocation);

            DataGridViewTextBoxColumn collane = new DataGridViewTextBoxColumn();
            collane.DataPropertyName = "lane"; // 資料來源欄位
            collane.HeaderText = "車道";
            collane.Name = "車道";
            collane.Width = 35;
            collane.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(collane);

            DataGridViewTextBoxColumn coltype = new DataGridViewTextBoxColumn();
            coltype.DataPropertyName = "type"; // 資料來源欄位
            coltype.HeaderText = "車型";
            coltype.Name = "車型";
            coltype.Width = 50;
            coltype.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(coltype);

            DataGridViewTextBoxColumn coldirection = new DataGridViewTextBoxColumn();
            coldirection.DataPropertyName = "direction"; // 資料來源欄位
            coldirection.HeaderText = "方向";
            coldirection.Name = "方向";
            coldirection.Width = 35;
            coldirection.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(coldirection);

            DataGridViewTextBoxColumn colspeed = new DataGridViewTextBoxColumn();
            colspeed.DataPropertyName = "speed"; // 資料來源欄位
            colspeed.HeaderText = "車速";
            colspeed.Name = "車速";
            colspeed.Width = 35;
            colspeed.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(colspeed);

            DataGridViewTextBoxColumn colspeedLimit = new DataGridViewTextBoxColumn();
            colspeedLimit.DataPropertyName = "speedLimit"; // 資料來源欄位
            colspeedLimit.HeaderText = "限速";
            colspeedLimit.Name = "限速";
            colspeedLimit.Width = 35;
            colspeedLimit.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(colspeedLimit);

            DataGridViewTextBoxColumn colstate = new DataGridViewTextBoxColumn();
            colstate.DataPropertyName = "state"; // 資料來源欄位
            colstate.HeaderText = "狀態";
            colstate.Name = "車輛狀態";
            colstate.Width = 40;
            colstate.SortMode = DataGridViewColumnSortMode.NotSortable;
            dgvVehicle.Columns.Add(colstate);
        }

        /// <summary>
        /// DataGridView控制元件的當前單元格變化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvVehicle_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgvVehicle.CurrentRow != null)
            {
                if (clsPreviousRowIndex == dgvVehicle.CurrentRow.Index)
                {
                    return;
                }
                else
                {
                    clsPreviousRowIndex = dgvVehicle.CurrentRow.Index;
                    ShowCurrentRecord(dgvVehicle.CurrentRow.Index);
                }
            }
        }

        /// <summary>
        /// 顯示當前記錄
        /// </summary>
        /// <param name="noRecordFlag">此引數為true時載入無記錄資訊</param>
        private void ShowCurrentRecord(bool noRecordFlag)
        {
            if (noRecordFlag)
            {
                txtLicense.BackColor = Color.Gray;
                txtLicense.Text = "";
                lblCurrentVehicleDetail.Text = "";
                picFull.Image = null;
                picSpecial.Image = null;
            }
        }

        /// <summary>
        /// 顯示當前記錄
        /// </summary>
        /// <param name="recordIndex">此引數大於等於0時介面載入顯示DataSet中索引為recordIndex的記錄資訊</param>
        private void ShowCurrentRecord(int recordIndex)
        {
            if (recordIndex < 0)
            {
                return;
            }

            string licenseColorCode = clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[4].ToString().Trim();
            switch (licenseColorCode)
            {
                case "0":
                    txtLicense.BackColor = Color.White;
                    txtLicense.ForeColor = Color.Black;
                    break;
                case "1":
                    txtLicense.BackColor = Color.Yellow;
                    txtLicense.ForeColor = Color.Black;
                    break;
                case "2":
                    txtLicense.BackColor = Color.Blue;
                    txtLicense.ForeColor = Color.White;
                    break;
                case "3":
                    txtLicense.BackColor = Color.Black;
                    txtLicense.ForeColor = Color.White;
                    break;
                case "5": // 底色為綠色時字型色為白 2008-01-24從羅兵華處得知
                    txtLicense.BackColor = Color.Green;
                    txtLicense.ForeColor = Color.White;
                    break;
                default:
                    txtLicense.BackColor = Color.Gray;
                    txtLicense.ForeColor = Color.Black;
                    break;
            } //End Switch 
            txtLicense.Text = clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[2].ToString().Trim();

            string detail = "";
            detail = detail + "索引:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[1].ToString();
            detail = detail + "/r/n車牌號碼:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[2].ToString();
            detail = detail + "/r/n車牌顏色:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[3].ToString();
            detail = detail + "/r/n時間:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[6].ToString();
            detail = detail + "/r/n位置:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[7].ToString();
            detail = detail + "/r/n車道:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[8].ToString();
            detail = detail + "/r/n車型:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[9].ToString();
            detail = detail + "/r/n方向:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[10].ToString();
            //detail = detail + "/r/n小車限速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[13].ToString();
            //detail = detail + "/r/n卡車限速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[14].ToString();
            detail = detail + "/r/n本車限速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[15].ToString();
            detail = detail + "/r/n觸發車速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[12].ToString();
            detail = detail + "/r/n車速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[11].ToString();
            if (CLSOverSpeedStateNormal == (int)clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[16])
            {
                detail = detail + "/r/n狀態:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[16].ToString() + "  一般超速,超速小於等於50%";
            }
            else if (CLSOverSpeedStateSerious == (int)clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[16])
            {
                detail = detail + "/r/n狀態:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[16].ToString() + "  嚴重超速,超速大於50%";
            }
            lblCurrentVehicleDetail.Text = detail + "/r/n";

            try
            {
                string imageFullPath = clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[17].ToString().Trim();
                if (File.Exists(imageFullPath))
                {
                    if (clsImageFull != null)
                    {
                        clsImageFull.Dispose();
                    }
                    clsImageFull = Image.FromFile(imageFullPath);
                    picFull.Image = clsImageFull;
                }
                else
                {
                    picFull.Image = null;
                    lblCurrentVehicleDetail.Text = lblCurrentVehicleDetail.Text + "未找到全景影象!";
                }

                string imageSpecialPath =  clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[18].ToString().Trim();
                if (File.Exists(imageSpecialPath))
                {
                    if (clsImageSpecial != null)
                    {
                        clsImageSpecial.Dispose();
                    }
                    clsImageSpecial = Image.FromFile(imageSpecialPath);
                    picSpecial.Image = clsImageSpecial;
                }
                else
                {
                    picSpecial.Image = null;
                    lblCurrentVehicleDetail.Text = lblCurrentVehicleDetail.Text + "未找到牌識影象!";
                }
            }
            catch (Exception ex)
            {
                Log.WriteToFile("FrmFind.ShowCurrentRecord Exception","recordIndex=" + recordIndex.ToString() + " ex.Message: " + ex.ToString());

                // 介面顯示影象載入異常資訊
                lblCurrentVehicleDetail.Text = lblCurrentVehicleDetail.Text + "顯示影象發生異常!詳細請查閱日誌!";
            }
        }

        private void chkTimeStart_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTimeStart.Checked)
            {
                dtpTimeStart.Enabled = true;
                dtpTimeStart.Value = DateTime.Now;
            }
            else
            {
                dtpTimeStart.Enabled = false;
            }
        }

        private void chkTimeEnd_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTimeEnd.Checked)
            {
                dtpTimeEnd.Enabled = true;
                dtpTimeEnd.Value = DateTime.Now;
            }
            else
            {
                dtpTimeEnd.Enabled = false;
            }
        }

        private void chkLicense_CheckedChanged(object sender, EventArgs e)
        {
            if (chkLicense.Checked)
            {
                txtLicenseFind.Enabled = true;
                if (txtLicenseFind.Text.Length == 0)
                {
                    txtLicenseFind.Text = "無車牌";
                }
                txtLicenseFind.Select();
            }
            else
            {
                txtLicenseFind.Enabled = false;
            }
        }

        private void chkLicenseColor_CheckedChanged(object sender, EventArgs e)
        {
            if (chkLicenseColor.Checked)
            {
                cboLicenseColor.Enabled = true;
                if (cboLicenseColor.SelectedIndex < 0)
                {
                    cboLicenseColor.SelectedIndex = 2;
                }
            }
            else
            {
                cboLicenseColor.Enabled = false;
            }
        }

        private void chkLane_CheckedChanged(object sender, EventArgs e)
        {
            if (chkLane.Checked)
            {
                cboLane.Enabled = true;
                if (cboLane.SelectedIndex < 0)
                {
                    cboLane.SelectedIndex = 0;
                }
            }
            else
            {
                cboLane.Enabled = false;
            }
        }

        private void chkSpeed_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSpeed.Checked)
            {
                cboSpeedCompareSymbol.Enabled = true;
                if (cboSpeedCompareSymbol.SelectedIndex < 0)
                {
                    cboSpeedCompareSymbol.SelectedIndex = 0;
                }
                cboSpeed.Enabled = true;
                if (cboSpeed.Text.Length == 0)
                {
                    cboSpeed.Text = "60";
                }
            }
            else
            {
                cboSpeedCompareSymbol.Enabled = false;
                cboSpeed.Enabled = false;
            }
        }

        private void txtLicenseFind_Validated(object sender, EventArgs e)
        {
            int inputLicenseLength = txtLicenseFind.Text.Length;
            if (inputLicenseLength > CLSLicenseMaxLength)
            {
                MessageBox.Show("您輸入的車牌字串的超過了長度限制,最長為" + CLSLicenseMaxLength.ToString() + "個字元!/r/n" + "一般車牌字串長度為7。/r/n" + "本軟體支援模糊查詢,例如您輸入:123,可以查詢所有含123的車牌號碼!", "請重新輸入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtLicenseFind.Focus();
                txtLicenseFind.Select(0, inputLicenseLength);
            }
        }

        private void cboLane_Validated(object sender, EventArgs e)
        {
            try
            {
                int lane = int.Parse(cboLane.Text);

                if (lane < CLSLaneMinNumber || lane > CLSLaneMaxNumber)
                {
                    MessageBox.Show("您輸入的車道號超出了允許範圍,必須為" + CLSLaneMinNumber.ToString() + "-" + CLSLaneMaxNumber.ToString() + "之間的數字!", "請重新輸入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    cboLane.Focus();
                    cboLane.Select(0, cboLane.Text.Length);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("您輸入的車道號不符合要求,導致以下異常產生:/r/n" + ex.Message + "/r/n輸入必須為" + CLSLaneMinNumber.ToString() + "-" + CLSLaneMaxNumber.ToString() + "之間的數字!", "請重新輸入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                cboLane.Text = "1";
                cboLane.Focus();
                cboLane.Select(0, cboLane.Text.Length);
            }
        }

        private void cboSpeed_Validated(object sender, EventArgs e)
        {
            try
            {
                int speed = int.Parse(cboSpeed.Text);

                if (speed < CLSSpeedMinNumber || speed > CLSSpeedMaxNumber)
                {
                    MessageBox.Show("您輸入的車速超出了允許範圍,必須為" + CLSSpeedMinNumber.ToString() + "-" + CLSSpeedMaxNumber.ToString() + "之間的數字!", "請重新輸入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    cboSpeed.Focus();
                    cboSpeed.Select(0, cboSpeed.Text.Length);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("您輸入的車速不符合要求,導致以下異常產生:/r/n" + ex.Message + "/r/n輸入必須為" + CLSSpeedMinNumber.ToString() + "-" + CLSSpeedMaxNumber.ToString() + "之間的數字!", "請重新輸入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                cboSpeed.Text = "60";
                cboSpeed.Focus();
                cboSpeed.Select(0, cboSpeed.Text.Length);
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            string errorMsg = "";
            if (!ValidateSqlParameter(ref errorMsg))
            {
                MessageBox.Show("您輸入的查詢條件不正確:/r/n" + errorMsg + "/r/n請重新選擇查詢條件!", "錯誤提示!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                this.Cursor = Cursors.Arrow;
                return;
            }

            GetVehicleFromDatabase(0, GetSearchSqlCommand(0));

            this.Cursor = Cursors.Arrow;
        }

        /// <summary>
        /// 驗證Sql查詢條件間的引數有效性
        /// </summary>
        /// <param name="errorMsg">檢測到錯誤則寫入錯誤資訊並立即返回</param>
        /// <returns>全部有效返回 true;否則返回false,並將錯誤資訊寫入errorMsg</returns>
        private bool ValidateSqlParameter(ref string errorMsg)
        {
            string timeStart;
            string timeEnd;
            errorMsg = null;

            if (chkTimeStart.Checked && chkTimeEnd.Checked)
            {
                timeStart = dtpTimeStart.Value.ToString("yyyy-MM-dd HH:mm:ss");
                timeEnd = dtpTimeEnd.Value.ToString("yyyy-MM-dd HH:mm:ss");

                if ((dtpTimeStart.Value > dtpTimeEnd.Value) && (!timeStart.Equals(timeEnd)))
                {
                    errorMsg = "終止時間必須大於等於起始時間!/r/n" + "本次查詢終止時間:" + dtpTimeEnd.Value.ToString() + " 小於起始時間:" + dtpTimeStart.Value.ToString();
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 獲取查詢語句【目前只支援Access資料庫】
        /// </summary>
        /// <param name="databaseType">資料庫型別 0 Access;1 MySql;2 Sql Server;3 Oracle</param>
        /// <returns>返回查詢語句</returns>
        private string GetSearchSqlCommand(int databaseType)
        {
            string sqlCommand;
          
            string timeStart;
            string timeEnd;
            bool otherChecked = false;

            if (chkNotLimitReturnRecord.Checked)
            {
                sqlCommand = "select * from tbVehicle";
            }
            else
            {
                sqlCommand = "select top " + CLSDefaultReturnRecord.ToString() + " * from tbVehicle";
            }

            if (chkTimeStart.Checked)
            {
                if (otherChecked)
                {
                    sqlCommand = sqlCommand + " and ";
                }
                else
                {
                    sqlCommand = sqlCommand + " where ";
                }

                timeStart = dtpTimeStart.Value.ToString("yyyy-MM-dd HH:mm:ss");
                if (0 == databaseType) // 如果是Access資料,時間標識字元不一樣
                {
                    sqlCommand = sqlCommand + "passTime >=" + "#" + timeStart + "#";
                }
                else
                {
                    sqlCommand = sqlCommand + "passTime >=" + "'" + timeStart + "'";
                }
                otherChecked = true;
            }

            if (chkTimeEnd.Checked)
            {
                if (otherChecked)
                {
                    sqlCommand = sqlCommand + " and ";
                }
                else
                {
                    sqlCommand = sqlCommand + " where ";
                }

                timeEnd = dtpTimeEnd.Value.ToString("yyyy-MM-dd HH:mm:ss");
                if (0 == databaseType) // 如果是Access資料,時間標識字元不一樣
                {
                    sqlCommand = sqlCommand + "passTime <=" + "#" + timeEnd + "#";
                }
                else
                {
                    sqlCommand = sqlCommand + "passTime <=" + "'" + timeEnd + "'";
                }
                otherChecked = true;
            }

            if (chkLicense.Checked)
            {
                if (otherChecked)
                {
                    sqlCommand = sqlCommand + " and ";
                }
                else
                {
                    sqlCommand = sqlCommand + " where ";
                }

                sqlCommand = sqlCommand + "license like " + "'%" + txtLicenseFind.Text.Trim() + "%'";
                otherChecked = true;
            }

            if (chkLicenseColor.Checked)
            {
                if (otherChecked)
                {
                    sqlCommand = sqlCommand + " and ";
                }
                else
                {
                    sqlCommand = sqlCommand + " where ";
                }

                sqlCommand = sqlCommand + "licenseColor = " + "'" + cboLicenseColor.Text.Trim() + "'";
                otherChecked = true;
            }

            if (chkLane.Checked)
            {
                if (otherChecked)
                {
                    sqlCommand = sqlCommand + " and ";
                }
                else
                {
                    sqlCommand = sqlCommand + " where ";
                }

                sqlCommand = sqlCommand + "lane = " + cboLane.Text.Trim();
                otherChecked = true;
            }

            if (chkSpeed.Checked)
            {
                if (otherChecked)
                {
                    sqlCommand = sqlCommand + " and ";
                }
                else
                {
                    sqlCommand = sqlCommand + " where ";
                }

                switch (cboSpeedCompareSymbol.SelectedIndex)
                {
                    case 0:
                        sqlCommand = sqlCommand + "Speed >= " + cboSpeed.Text.Trim();
                        break;
                    case 1:
                        sqlCommand = sqlCommand + "Speed = " + cboSpeed.Text.Trim();
                        break;
                    case 2:
                        sqlCommand = sqlCommand + "Speed <= " + cboSpeed.Text.Trim();
                        break;
                    default:
                        sqlCommand = sqlCommand + "Speed >= " + cboSpeed.Text.Trim();
                        break;
                } //End Switch 

                otherChecked = true;
            }

            if (chkOrderByPassTime.Checked)
            {
                sqlCommand = sqlCommand + " order by passTime";
            }

            return sqlCommand;
        }

        /// <summary>
        /// 從資料庫裝載車輛資訊
        /// </summary>
        /// <param name="recordNumber">當此引數大於0小於CLSDefaultReturnRecord時,返回前recordNumber條記錄</param>
        /// <param name="sqlCommand">當recordNumber無效時用指定查詢語句查詢</param>
        private void GetVehicleFromDatabase(int recordNumber,string sqlCommand)
        {
            try
            {
                if (clsDSVehicle != null)
                {
                    clsDSVehicle.Clear();
                    clsDSVehicle.Dispose();
                }

                if (recordNumber > 0 && recordNumber < CLSDefaultReturnRecord)
                {
                    clsDSVehicle = DataAccess.GetDataSet("select top " + recordNumber.ToString() + " * from tbVehicle");
                }
                else
                {
                    clsDSVehicle = DataAccess.GetDataSet(sqlCommand);
                }

                if (clsDSVehicle != null)
                {                  
                    clsDSVehicle.Tables[0].TableName = "tbVehicle";

                    if (clsDSVehicle.Tables[0].Rows.Count <= 0)
                    {
                        // 顯示無資訊
                        ShowCurrentRecord(true);
                        return;
                    }

                    this.bsVehicle.DataSource = clsDSVehicle;
                    this.bsVehicle.DataMember = "tbVehicle";
                    this.bsVehicle.AllowNew = false;

                    this.bnVehicle.BindingSource = this.bsVehicle;

                    clsPreviousRowIndex = -1;
                }
                else
                {
                    // 異常
                    ShowCurrentRecord(true);
                    MessageBox.Show("從資料庫查詢時發生異常,可能是表tbVehicle不存在!/r/n" + "recordNumber=" + recordNumber.ToString() + "/r/nsqlCommand=" + sqlCommand, "錯誤提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Log.WriteToFile("FrmFind.GetVehicleFromDatabase", "clsDSVehicle == null recordNumber=" + recordNumber.ToString() + " sqlCommand=" + sqlCommand);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("從資料庫查詢時發生以下異常:/r/n" + ex.Message , "錯誤提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Log.WriteToFile("FrmFind.GetVehicleFromDatabase exception", "recordNumber=" + recordNumber.ToString() + " sqlCommand=" + sqlCommand + " ex.Message=" + ex.Message);
            }
        }
    }