日期:2014-05-17 浏览次数:20598 次
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER procedure [dbo].[Sel]( @whereStr varchar(100),--根据那个字段查询 @selStr varchar(100) --查询内容 ) as declare @sql varchar(100) if (@whereStr='' or @selStr='') begin set @sql='select * from bjx_zt order by id desc' end else begin set @sql='select * from bjx_Zt where '+@whereStr+' like ''%'+@selStr+'%'' order by id desc' end print @sql exec(@sql)
SQLHelper.getConnection(); Hashtable hashtable = new Hashtable(); hashtable.Add("@whereStr", "title"); hashtable.Add("@selStr", "测试"); var test = SQLHelper.GetDTSP("Sel", hashtable);
/// <summary> /// 根据条件返回指定存储过程的DataTable /// </summary> /// <param name="spName">存储过程的名称</param> /// <param name="ht">哈希表[存储过程中的变量名][value:值]</param> /// <returns></returns> public static DataTable GetDTSP(string spName, Hashtable ht) { DataTable datatable = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(); try { using (SqlConnection connection = new SqlConnection(StrConn)) { using (SqlCommand cmd = new SqlCommand(spName, connection)) { try { connection.Open(); cmd.CommandType = CommandType.StoredProcedure; SqlParameter[] prams = new SqlParameter[ht.Count]; int i = 0; foreach (DictionaryEntry objDe in ht) { prams[i] = new SqlParameter(objDe.Key.ToString(), objDe.Value.ToString()); i++; } // 依次把参数传入命令文本 foreach (SqlParameter parameter in prams) { cmd.Parameters.Add(parameter); } da.SelectCommand = cmd; da.Fill(datatable); } catch { connection.Close(); } } } } catch { } return datatable; }