• 复合查询


            /// <summary>
            /// 复合查询
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnCheck_Click(object sender, EventArgs e)
            {
                //方法一
                //StringBuilder sb = new StringBuilder();
                //sb.Append("select * from T_Customers where 1=1\n");
                //if (ckbName.Checked)
                //{
                //    sb.Append("and Name like @name\n");
                //}
                //if (ckbPhone.Checked)
                //{
                //    sb.Append("and Phone like @phone\n");
                //}
                //if (ckbCarNum.Checked)
                //{
                //    sb.Append("and CarNum like @carnum\n");
                //}
                //MessageBox.Show(sb.ToString());

                //方法二
                List<string> listWhere = new List<string>();
                if (ckbName.Checked)
                {
                    listWhere.Add("Name like @name\n");
                }
                if (ckbPhone.Checked)
                {
                    listWhere.Add("Phone like @phone\n");
                }
                if (ckbCarNum.Checked)
                {
                    listWhere.Add("CarNum like @carnum\n");
                }
                string sql = "select * from T_Custumers\n";
                if (listWhere.Count>0)
                {
                    string sqlWhere = string.Join("and ", listWhere.ToArray());
                    sql += " where " + sqlWhere;
                }
                MessageBox.Show(sql);

            }

    感谢来访,共同学习!
  • 相关阅读:
    1061 Dating (20 分)
    1042 Shuffling Machine (20 分)简单模拟
    1132 Cut Integer (20 分)
    1100 Mars Numbers (20 分)
    1077 Kuchiguse (20 分)求字符串最长相同后缀
    1065 A+B and C (64bit) (20 分)大数 溢出
    1107 Social Clusters (30 分)并查集
    1079 Total Sales of Supply Chain (25 分)
    1078 Hashing (25 分)
    1063 Set Similarity (25 分)
  • 原文地址:https://www.cnblogs.com/dingxiaowei/p/3058760.html
Copyright © 2020-2023  润新知