C#连接mysql数据库如何实现多条件查询

C#连接mysql数据库如何实现多条件查询,第1张

给你一个稍微复杂一点的查询,我设计的

看上图我把第一列标签后面的控件命名有规律,比如计划工厂后面文本框是"计划工厂t",Exapt命名为"计划工厂c",后面还隐藏一个listbox,命名为"计划工厂l"。

计划工厂  这个名称本身就是我要查询的表里面包含的字段。

利用这种界面,我要完成一些复杂点的查询:点文本框里的小图标按钮,d出工具,可以输入多条件,条件可以成立为模糊条件(用*代替不认识部分),也可以成立为否决条件。

选中Exapt全部否定所成立条件;点击执行按钮,有条件的成立条件,无条件的不成立。

所以这种东西贯穿软件我就不能每个都去做,就只能写一个类文件:

    public class conditionset

    {

        public void load_condit(Panel p)

        {

            foreach (Control ctl in p.Controls)

            {

                if (ctl is SkinTextBox)

                {

                    SkinTextBox sktxt = (SkinTextBox)p.Controls[ctl.Name]

                    ListBox list = (ListBox)p.Controls[sktxt.Name.Substring(0, sktxt.Name.Length - 1) + "l"]

                    sktxt.IconClick += (Object sen, EventArgs ed) =>

                    {

                        ctl.condition toolform = new ctl.condition()

                        if (list.Items.Count > 0)

                        {

                            sktxt.Text = "┅"

                            sktxt.ReadOnly = true

                        }

                        foreach (var sm in list.Items)

                        {

                            toolform.dv.Rows.Add(sm.ToString())

                        }

                        toolform.Show()

                        toolform.cleari += (Object send, EventArgs er) =>

                         {

                             sktxt.Text = ""

                             sktxt.ReadOnly = false

                         }

                        toolform.subm += (Object send, EventArgs er) =>

                        {

                            list.Items.Clear()

                            foreach (DataGridViewRow dvr in toolform.dv.Rows)

                            {

                                if (Convert.ToString(dvr.Cells[0].Value) != "")

                                {

                                    list.Items.Add(Convert.ToString(dvr.Cells[0].Value))

                                }

                            }

                            sktxt.Text = "┅"

                            sktxt.ReadOnly = true

                        }

                    }

                }

            }

        }

        public string condit(Panel p)

        {

            string master_condition = ""

            foreach (Control ctl in p.Controls)

            {

                //遍历panel查找条件

                #region 

                string condition = ""

                if (ctl is SkinTextBox)

                {

                    //文本框条件组合

                    #region

                    SkinTextBox sktxt = (SkinTextBox)p.Controls[ctl.Name]

                    string ziduan_name = sktxt.Name.Substring(0, sktxt.Name.Length - 1)

                    SkinCheckBox skck = (SkinCheckBox)p.Controls[ctl.Name.Substring(0, ctl.Name.Length - 1) + "c"]

                    ListBox list = (ListBox)p.Controls[ctl.Name.Substring(0, ctl.Name.Length - 1) + "l"]

                    if (sktxt.Text != "")

                    {

                        if (sktxt.Text == "┅")

                        {

                            string blur_str = "", blur = ""

                            foreach (var itm in list.Items)

                            {

                                if (itm.ToString().Contains("*"))

                                {

                                    if (skck.Checked == true)

                                    {

                                        blur += " and " + ziduan_name + " not like '" + itm.ToString().Replace("*", "%") + "'"

                                    }

                                    else

                                    {

                                        blur += " or " + ziduan_name + " like '" + itm.ToString().Replace("*", "%") + "'"

                                    }

                                }

                                else

                                {

                                    blur_str += "'" + Convert.ToString(itm) + "',"

                                }

                            }

                            string blur_sql = (blur == "") ? "" : blur.Substring(4, blur.Length - 4).Trim()

                            string in_condition = "", like_condition = ""

                            if (skck.Checked == true)

                            {

                                in_condition = ziduan_name + " not in "

                                like_condition = " and "

                            }

                            else

                            {

                                in_condition = ziduan_name + " in "

                                like_condition = " or "

                            }

                            string contains_sql = (blur_str == "") ? "" : in_condition + "(" + blur_str.Substring(0, blur_str.Length - 1) + ")"

                            condition = contains_sql + blur_sql

                            if (blur_sql != "" && contains_sql != "")

                            {

                                condition = contains_sql + like_condition + blur_sql

                            }

                            else

                            {

                                condition = contains_sql + blur_sql

                            }

                        }

                        else

                        {

                            if (!sktxt.Text.Contains("*"))

                            {

                                if (skck.Checked == true)

                                {

                                    condition = ziduan_name + "<>'" + sktxt.Text + "'"

                                }

                                else

                                {

                                    condition = ziduan_name + "='" + sktxt.Text + "'"

                                }

                            }

                            else

                            {

                                if (skck.Checked == true)

                                {

                                    condition = ziduan_name + " not like '" + sktxt.Text.Replace("*", "%") + "'"

                                }

                                else

                                {

                                    condition = ziduan_name + " like '" + sktxt.Text.Replace("*", "%") + "'"

                                }

                            }

                        }

                    }

                    #endregion

                }

                if (ctl is SkinDateTimePicker)

                {

                    //日期条件组合

                    #region

                    if (ctl.Name.Substring(ctl.Name.Length - 1, 1) != "t")

                    {

                        SkinDateTimePicker skdate_sta = (SkinDateTimePicker)p.Controls[ctl.Name]

                        SkinDateTimePicker skdate_end = (SkinDateTimePicker)p.Controls[ctl.Name + "t"]

                        if (skdate_sta.text != "")

                        {

                            if (skdate_end.text == "")

                            {

                                condition = skdate_sta.Name + "='" + skdate_sta.text + "'"

                            }

                            else

                            {

                                condition = skdate_sta.Name + ">='" + skdate_sta.text + "' and " + skdate_sta.Name + "<='" + skdate_end.text + "'"

                            }

                        }

                    }

                    #endregion

                }

                master_condition += (condition == "") ? "" : "(" + condition + ") and "

                #endregion //遍历panel查找条件

            }

            string condition_sql = (master_condition == "") ? "" : master_condition.Substring(0, master_condition.Length - 5)

            return condition_sql

        }

    }

工具用一个窗体代替:

    public partial class condition : Form

    {

        public condition()

        {

            InitializeComponent()

        }

        public event EventHandler subm

        public event EventHandler cleari

        private void submit_Click(object sender, EventArgs e)

        {

            subm(sender, e)

            this.Dispose()

        }

        private void clearit_Click(object sender, EventArgs e)

        {

            this.Dispose()

            dv.Rows.Clear()

            cleari(sender, e)

        }

    }

当我们执行多条件的时候就等于拼接条件

看你的代码存在两点疑问,第一,SQL语句构造完成你执行了没,如利用SqlCommand或SqlDataAdapter执行了没?第二,你的SQL语句应该是存在问题,如find+=" and 书名="+bookname这一句,至少是缺少单引号,正确的应该是find+=" and 书名='"+bookname+"'"其它的也类似

当有多个分支选择时,可采用if-else-if语句,其一般形式为:

if(表达式1)

语句1

else  if(表达式2)

语句2

else  if(表达式3)

语句3

else  if(表达式m)

语句m

else

语句n

其语义是:依次判断表达式的值,当出现某个值为真时,则执行其对应的语句。然后跳到整个if语句之外继续执行程序。 如果所有的表达式均为假,则执行语句n。然后继续执行后续程序。 if-else-if语句的执行过程如下图所示。

参考代码如下:

char c

printf("input a character:    ")

c=getchar()

if(c<32)

printf("This is a control character\n")

else if(c>='0'&&c<='9')

printf("This is a digit\n")

else if(c>='A'&&c<='Z')

printf("This is a capital letter\n")

else if(c>='a'&&c<='z')

printf("This is a small letter\n")

else

printf("This is an other character\n")

return 0

本例要求判别键盘输入字符的类别。可以根据输入字符的ASCII码来判别类型。由ASCII码表可知ASCII值小于32的为控制字符。在“0”和“9”之间的为数字,在“A”和“Z”之间为大写字母, 在“a”和“z”之间为小写字母,其余则为其它字符。

这是一个多分支选择的问题,用if-else-if语句编程,判断输入字符ASCII码所在的范围,分别给出不同的输出。例如输入为“g”,输出显示它为小写字符。

扩展资料:

在使用if语句中还应注意以下问题:

1、在if关键字之后均为表达式。 该表达式通常是逻辑表达式或关系表达式,但也可以是其它表达式,如赋值表达式等,甚至也可以是一个变量。例如:

if(a=5) 语句

if(b) 语句

都是允许的。只要表达式的值为非0,即为“真”。如在:

if(a=5)…

中表达式的值永远为非0,所以其后的语句总是要执行的,当然这种情况在程序中不一定会出现,但在语法上是合法的。

2、在if语句中,条件判断表达式必须用括号括起来,在语句之后必须加分号。

3、在if语句中,所有的语句应为单个语句,如果要想在满足条件时执行一组(多个)语句,则必须把这一组语句用{}括起来组成一个复合语句。但要注意的是在}之后不能再加分号。

参考资料:

Microsoft-if-else(C# 参考)

菜鸟教程-C# if 语句


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9550666.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存