C# WinForm 实现增删改查等功能(Access版) 系列之五-查询

查询记录模块设计与实现

/// <summary>

        /// 根据类别与内容进行模糊或是精确查询

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnSelect_Click(object sender, EventArgs e)

        {

            //首先进行模糊查询

            string strComboxSelect = ComboxSelect.Text.Trim();

            string strSearch = txtSearch.Text.Trim();

            if(strComboxSelect.Equals("请选择类别"))

            {

                MessageBox.Show("请选择类别!","提示");

                return;

            }

            if (strSearch == "" || strSearch == null)

            {

                MessageBox.Show("请输入查询内容!", "提示");

                return;

            }

            //手动代码把数据库中的数据显示出来

            OleDbConnection conn = GetConnection();

            string sqlText = "select 姓名,性别,出生日期,工作年限,证件类型,证件号,居住地,Email,手机号码,家庭电话,自我评价 from MResume where " + strComboxSelect + " like '%"+@strSearch+"%'";

            OleDbCommand cmd = new OleDbCommand(sqlText, conn);

            cmd.Parameters.AddWithValue("@strSearch", strSearch);

            try

            {

                conn.Open();

                //int i = cmd.ExecuteNonQuery();

                DataTable dt = new DataTable();

                OleDbDataAdapter oda = new OleDbDataAdapter(sqlText, conn);

                oda.Fill(dt);

                dataGridView1.DataSource = dt;

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

            finally

            {

                conn.Close();

            }

        }

posted @ 2010-04-01 21:04  叮当小马  阅读(983)  评论(0编辑  收藏  举报