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 pControls)

            {

                if (ctl is SkinTextBox)

                {

                    SkinTextBox sktxt = (SkinTextBox)pControls[ctlName];

                    ListBox list = (ListBox)pControls[sktxtNameSubstring(0, sktxtNameLength - 1) + "l"];

                    sktxtIconClick += (Object sen, EventArgs ed) =>

                    {

                        ctlcondition toolform = new ctlcondition();

                        if (listItemsCount > 0)

                        {

                            sktxtText = "┅";

                            sktxtReadOnly = true;

                        }

                        foreach (var sm in listItems)

                        {

                            toolformdvRowsAdd(smToString());

                        }

                        toolformShow();

                        toolformcleari += (Object send, EventArgs er) =>

                         {

                             sktxtText = "";

                             sktxtReadOnly = false;

                         };

                        toolformsubm += (Object send, EventArgs er) =>

                        {

                            listItemsClear();

                            foreach (DataGridViewRow dvr in toolformdvRows)

                            {

                                if (ConvertToString(dvrCells[0]Value) != "")

                                {

                                    listItemsAdd(ConvertToString(dvrCells[0]Value));

                                }

                            }

                            sktxtText = "┅";

                            sktxtReadOnly = true;

                        };

                    };

                }

            }

        }

        public string condit(Panel p)

        {

            string master_condition = "";

            foreach (Control ctl in pControls)

            {

                //遍历panel查找条件

                #region 

                string condition = "";

                if (ctl is SkinTextBox)

                {

                    //文本框条件组合

                    #region

                    SkinTextBox sktxt = (SkinTextBox)pControls[ctlName];

                    string ziduan_name = sktxtNameSubstring(0, sktxtNameLength - 1);

                    SkinCheckBox skck = (SkinCheckBox)pControls[ctlNameSubstring(0, ctlNameLength - 1) + "c"];

                    ListBox list = (ListBox)pControls[ctlNameSubstring(0, ctlNameLength - 1) + "l"];

                    if (sktxtText != "")

                    {

                        if (sktxtText == "┅")

                        {

                            string blur_str = "", blur = "";

                            foreach (var itm in listItems)

                            {

                                if (itmToString()Contains(""))

                                {

                                    if (skckChecked == true)

                                    {

                                        blur += " and " + ziduan_name + " not like '" + itmToString()Replace("", "%") + "'";

                                    }

                                    else

                                    {

                                        blur += " or " + ziduan_name + " like '" + itmToString()Replace("", "%") + "'";

                                    }

                                }

                                else

                                {

                                    blur_str += "'" + ConvertToString(itm) + "',";

                                }

                            }

                            string blur_sql = (blur == "")  "" : blurSubstring(4, blurLength - 4)Trim();

                            string in_condition = "", like_condition = "";

                            if (skckChecked == 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_strSubstring(0, blur_strLength - 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 (!sktxtTextContains(""))

                            {

                                if (skckChecked == true)

                                {

                                    condition = ziduan_name + "<>'" + sktxtText + "'";

                                }

                                else

                                {

                                    condition = ziduan_name + "='" + sktxtText + "'";

                                }

                            }

                            else

                            {

                                if (skckChecked == true)

                                {

                                    condition = ziduan_name + " not like '" + sktxtTextReplace("", "%") + "'";

                                }

                                else

                                {

                                    condition = ziduan_name + " like '" + sktxtTextReplace("", "%") + "'";

                                }

                            }

                        }

                    }

                    #endregion

                }

                if (ctl is SkinDateTimePicker)

                {

                    //日期条件组合

                    #region

                    if (ctlNameSubstring(ctlNameLength - 1, 1) != "t")

                    {

                        SkinDateTimePicker skdate_sta = (SkinDateTimePicker)pControls[ctlName];

                        SkinDateTimePicker skdate_end = (SkinDateTimePicker)pControls[ctlName + "t"];

                        if (skdate_statext != "")

                        {

                            if (skdate_endtext == "")

                            {

                                condition = skdate_staName + "='" + skdate_statext + "'";

                            }

                            else

                            {

                                condition = skdate_staName + ">='" + skdate_statext + "' and " + skdate_staName + "<='" + skdate_endtext + "'";

                            }

                        }

                    }

                    #endregion

                }

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

                #endregion //遍历panel查找条件

            }

            string condition_sql = (master_condition == "")  "" : master_conditionSubstring(0, master_conditionLength - 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);

            thisDispose();

        }

        private void clearit_Click(object sender, EventArgs e)

        {

            thisDispose();

            dvRowsClear();

            cleari(sender, e);

        }

    }

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

使用SQL进行数据查询时,如果查询结果中包含了许多重复的行,可以使用( )去掉结果中的重复行。

ADISTINCT

BDISTINCTROW

CDIFFERENT

DDIFFERENTROW

没什么好办法,只能定义一个临时变量来衔接各个字段,然后判断各个控件是否为空来执行。具体如下:

var tempstr : string ;

begin

tempstr := 'select from table_name where 1=1 ';

with qry1 do

begin

Close ;

SQLClear ;

SQLAdd(tempstr) ;

if edt1Text<>'' then

begin

SQLAdd(' and where column_name like '+ QuotedStr(edt1Text)) ;

end;

(其他判断语句)

Open ;

end;

end;

最好能描述清楚错误的提示内容,这样才能判断问题所在,有时出错可能是其他方面引起的!

试试这条SQL语句:

Dim SqlStr As String

'如果 IN_Date 的字段为 Date 类型

SqlStr = "select from [tb_IN] " _

& "where IN_Date between cast('" & DTPicker1Value & "' as DateTime) And cast('" & DTPicker2Value & "' as DateTime) " _

& "and 供应商 like '%" & Text1Text & "%' "

Adodc1RecordSource = SqlStr

以上就是关于C#连接mysql数据库如何实现多条件查询全部的内容,包括:C#连接mysql数据库如何实现多条件查询、sql如何进行许多条件重复查询、delphi 数据库的多条件或但条件查询等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/sjk/10190371.html

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

发表评论

登录后才能评论

评论列表(0条)

保存