国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#參數化SQL查詢

2024-07-21 02:48:40
字體:
來源:轉載
供稿:網友
C#參數化SQL查詢
//寫一個存儲過程
ALTER PROCEDURE dbo.Infosearch        (    @bmid smallint = null,    @xm varchar(10)=null,    @xb varchar(10)=null,    @strage smallint=null,    @endage smallint=null,    @zzmm varchar(10)=null,    @xl varchar(10)=null,    @zw varchar(10)=null    )    AS    /* SET NOCOUNT ON */     declare @sql varchar(100)    if @bmid is not null    begin         set @sql=' where 部門ID='+Convert(varchar(10),@bmid)    end        if @xm is not null    begin        if @sql is not null            set @sql=@sql+' and 姓名like'+@xm        else set @sql=' where 姓名like'+@xm    end        if @xb is not null    begin         if    @sql is not null            set @sql=@sql+' and 性別='+@xb        else set @sql=' where 性別='+@xb    end        if @strage is not null    begin        if @sql is not null            set @sql=@sql+' and 年齡between '+Convert(varchar(10),@strage)        else set @sql=' where 年齡between '+Convert(varchar(10),@strage)    end        if @endage is not null    begin         set @sql=@sql+' and '+Convert(varchar(10),@endage)    end        if @zzmm is not null    begin        if @sql is not null             set @sql=@sql+' and 政治面貌='+@zzmm        else set @sql=' where 政治面貌='+@zzmm    end        if @xl is not null    begin        if @sql is not null            set @sql=@sql+' and 學歷='+@xl        else set @sql=' where 學歷='+@xl    end        if @zw is not null    begin        if @sql is not null            set @sql=@sql+' and 職位='+@zw        else set @sql=' where 職位='+@zw    end        exec('select 職工號,姓名,性別,年齡,學歷,婚姻狀況,政治面貌from yuangong'+@sql)    RETURN ALTER PROCEDURE dbo.Infosearch        (    @bmid smallint = null,    @xm varchar(10)=null,    @xb varchar(10)=null,    @strage smallint=null,    @endage smallint=null,    @zzmm varchar(10)=null,    @xl varchar(10)=null,    @zw varchar(10)=null    )    AS    /* SET NOCOUNT ON */     declare @sql varchar(100)    if @bmid is not null    begin         set @sql=' where 部門ID='+Convert(varchar(10),@bmid)    end        if @xm is not null    begin        if @sql is not null            set @sql=@sql+' and 姓名like'+@xm        else set @sql=' where 姓名like'+@xm    end        if @xb is not null    begin         if    @sql is not null            set @sql=@sql+' and 性別='+@xb        else set @sql=' where 性別='+@xb    end        if @strage is not null    begin        if @sql is not null            set @sql=@sql+' and 年齡between '+Convert(varchar(10),@strage)        else set @sql=' where 年齡between '+Convert(varchar(10),@strage)    end        if @endage is not null    begin         set @sql=@sql+' and '+Convert(varchar(10),@endage)    end        if @zzmm is not null    begin        if @sql is not null             set @sql=@sql+' and 政治面貌='+@zzmm        else set @sql=' where 政治面貌='+@zzmm    end        if @xl is not null    begin        if @sql is not null            set @sql=@sql+' and 學歷='+@xl        else set @sql=' where 學歷='+@xl    end        if @zw is not null    begin        if @sql is not null            set @sql=@sql+' and 職位='+@zw        else set @sql=' where 職位='+@zw    end        exec('select 職工號,姓名,性別,年齡,學歷,婚姻狀況,政治面貌from yuangong'+@sql)    RETURN //判斷參數是否為空來決定怎樣拼接查詢語句

如果是多條件查詢的話存儲過程里面就一個參數就夠了這個參數是不定條件查詢語句多條件之中判斷那個是否為空如果為空填充1=1不為空就為條件

 public static IDataReader ExecuteReader(DbCommand comm, string sql, params object[] value)        {            comm.CommandText = sql;            if (value != null && value.Length >= 0)            {                if (comm.CommandText.IndexOf("?") == -1)                {                    string[] temp = sql.Split('@');                    for (int i = 0; i < value.Length; i++)                    {                        string pName;                        if (temp[i + 1].IndexOf(" ") > -1)                        {                            pName = "@" + temp[i + 1].Substring(0, temp[i + 1].IndexOf(" "));                        }                        else                        {                            pName = "@" + temp[i + 1];                        }                        //pName = "@p" + (i + 1).ToString();                        DbParameter p = comm.CreateParameter();                        p.DbType = DbType.String;                        p.ParameterName = pName;                        p.Value = value[i];                        comm.Parameters.Add(p);                    }                }                else                {                    string[] temp = sql.Split('?');                    for (int i = 0; i < value.Length; i++)                    {                        temp[i] = temp[i] + "@p" + (i + 1).ToString();                        string pName = "@p" + (i + 1).ToString();                        DbParameter p = comm.CreateParameter();                        p.DbType = DbType.String;                        p.ParameterName = pName;                        p.Value = value[i];                        comm.Parameters.Add(p);                    }                    StringBuilder sb = new StringBuilder();                    for (int i = 0; i < temp.Length; i++)                    {                        sb.Append(temp[i]);                    }                    comm.CommandText = sb.ToString();                }            }            if (comm.Connection.State != ConnectionState.Open)            {                comm.Connection.Open();            }            return comm.ExecuteReader(CommandBehavior.CloseConnection);        }調用的時候類似:ExecuteReaderParams(comm, "select * from xx where id=? and name=?",id,name);


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 恩平市| 隆子县| 元氏县| 荥经县| 花垣县| 蒙自县| 梨树县| 彩票| 北安市| 眉山市| 武冈市| 莫力| 襄垣县| 沙洋县| 普安县| 囊谦县| 和静县| 昌都县| 建始县| 温州市| 山东| 云阳县| 砀山县| 淳化县| 长乐市| 宁强县| 墨玉县| 安国市| 濮阳县| 噶尔县| 南华县| 曲靖市| 正宁县| 洪洞县| 伊春市| 福州市| 龙山县| 黄大仙区| 横山县| 常山县| 景洪市|