组合查询和存储过程

来源:互联网 发布:软件使用说明怎么写 编辑:程序博客网 时间:2024/06/11 19:17

父窗体代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using crcs.UI;using crcs.Entity;namespace crcs.UI{    public partial class frmZuhechaxunMuban : Form    {        public frmZuhechaxunMuban()        {            InitializeComponent();        }        //public class EmptyUI        //{        //}        //将查询的内容转换为数据库中的列名称        public virtual string ToName(string Combo)        {            return "";        }        //获得数据库名称        protected virtual string Getdbtable()        {            return "";        }        private void frmOpWorkRecord_Load(object sender, EventArgs e)        {          }        private void btnOK_Click(object sender, EventArgs e)        {            //组合查询判断是否为空            if (cmbRelation1.Text == "")            {                if (cmbName1.Text == "" || cmbOper1.Text == "" || txtContent1.Text == "")                {                    MessageBox.Show("查询内容不可为空,请重新输入", "提示");                    return;                }            }            if (cmbRelation1.Text != "" && cmbRelation2.Text == "")            {                if (cmbName1.Text == "" || cmbOper1.Text == "" || txtContent1.Text == "" || cmbName2.Text == "" || cmbOper2.Text == "" || txtContent2.Text == "")                {                    MessageBox.Show("查询内容不可为空,请重新输入", "警告");                    return;                }            }            if (cmbRelation1.Text != "" && cmbRelation2.Text != "")            {                if (cmbName1.Text == "" || cmbOper1.Text == "" || txtContent1.Text == "" || cmbName2.Text == "" || cmbOper2.Text == "" || txtContent2.Text == "" || cmbName3.Text == "" || cmbOper3.Text == "" || txtContent3.Text == "")                {                    MessageBox.Show("查询内容不可为空,请重新输入", "警告");                    return;                }            }            if (cmbRelation1.Text == "" && cmbRelation2.Text != "")            {                MessageBox.Show("输入错误", "警告");                return;            }            //将参数传递给实体            crcs.Entity.ZUHECHAXUN zuhechaxun = new ZUHECHAXUN();            zuhechaxun.name1 = ToName(cmbName1.Text);            zuhechaxun.name2 = ToName(cmbName2.Text);            zuhechaxun.name3 = ToName(cmbName3.Text);            zuhechaxun.Operator1 = cmbOper1.Text;            zuhechaxun.Operator2 = cmbOper2.Text;            zuhechaxun.Operator3 = cmbOper3.Text;            zuhechaxun.Context1 = txtContent1.Text;            zuhechaxun.Context2 = txtContent2.Text;            zuhechaxun.Context3 = txtContent3.Text;            zuhechaxun.Relation1 = ToName(cmbRelation1.Text);            zuhechaxun.Relation2 = ToName(cmbRelation2.Text);            zuhechaxun.table = Getdbtable();            //开始查询            crcs.Facade.QueryFactory ZHCX = new Facade.QueryFactory ();            DataTable table = ZHCX .Fzuhechaxun(zuhechaxun);            if (table.Rows.Count >1)            {                dataGridView1.DataSource = table;            }            else            {                MessageBox.Show("没有数据");            }        }    }}



子窗体代码


using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace crcs.UI{    public partial class frmInquiryLineState : crcs.UI.frmZuhechaxunMuban    {        public frmInquiryLineState()        {            InitializeComponent();        }        public override string ToName(string Combo)        {            switch (Combo)            {                case"账号":                    return "userID";                case"姓名":                    return "StudentName";                case "性别":                    return "Sex";                case "学院":                    return "College";                case "年级":                    return "Grade";                case "专业":                    return "Profession";                case "注册日期":                    return "Date";                case "注册时间":                    return "Time";                case "使用状态":                    return "Status";                case "注册者":                    return "Caozuoyuan";                case"和":                    return "and";                case"或":                    return"or";                default:                    return "";                            }        }      }}


存储过程代码


USE [crcs]GO/****** Object:  StoredProcedure [dbo].[zuhechaxun]    Script Date: 2017/7/23 11:51:19 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[zuhechaxun] -- Add the parameters for the stored procedure here@name1 varchar(20),@Operator1 varchar(20),@Context1 varchar(20),@name2 varchar(20),@Operator2 varchar(20),@Context2 varchar(20),@name3 varchar(20),@Operator3 varchar(20),@Context3 varchar(20),@Relation1 varchar(20),@Relation2 varchar(20),@Getdbtable varchar(20)--将数据表也设成一个变量,这样就可以使代码更加灵活ASdeclare @sql varchar(500)--临时存放sql语句BEGINSET NOCOUNT ON;    --char(32)空格;char(39)是单引号;SET @sql ='select * from '+@Getdbtable +' where ' +@name1 +@Operator1+char(39) + @Context1 +char(39)if (@Relation1!= '')--如果第一个组合关系不为空BEGIN--在第一个组合关系的基础上,加上第二个查询条件SET @sql=@sql +char(32) +@Relation1 +char(32)+@name2 +@Operator2+char(39) + @Context2 +char(39)if @Relation2!= ''BEGIN--加上第三个查询条件set @sql =@sql+@Relation2+char(32)+@name3 +@Operator3+char(39) + @Context3 +char(39)ENDENDEXECUTE(@sql)END




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 pr手写字多笔画怎么办 家中挖矿噪音很大怎么办 学了栏目包装不想干怎么办 在文本打字乱了怎么办 体考后觉得成绩有出入怎么办 荧光棒进眼睛里怎么办 无法读源文件或磁盘怎么办 爱剪辑视频横着怎么办 pr界面字体太小怎么办 0p手机卡槽坏了怎么办 pr视频有水波纹怎么办 手机耳机插口插上没声音怎么办 xp系统电脑没有声音怎么办 win10笔记本耳机没声音怎么办 电脑插耳机还是外放怎么办 主机电脑耳机插孔坏了怎么办 苹果6plus有点卡怎么办 手机拨通电话后黑屏怎么办 手机拨通后没声音怎么办 苹果手机铃声不响了怎么办 婴儿喘气粗好像有痰怎么办 金毛呼吸声很大怎么办 回看福建导视该怎么办 楼上木地板走路声音都很响怎么办 班里团支不搭理我我该怎么办 客户买家具说贵怎么办 英雄联盟超出显示范围怎么办 脸上有斑点怎么办内条 脖子扭到了一动就疼怎么办 杭州限行在路上怎么办 在路上限行了怎么办 微信号不能进群怎么办 在微信约妹子被骗2000怎么办 qq号进不了热聊怎么办 网上买彩票中五百万怎么办 别人登我支付宝怎么办 医保刷卡刷到现金支付怎么办 微信诈骗立案后怎么办 奔跑吧诈骗填写了身份证怎么办 快手绑定的手机号丢了怎么办 球球大作战绑定手机号丢了怎么办