C#动态添加SqlParamter参数动态向SqlParameter ⾥添加相应参数,⽅法如下
先定义⼀个List,然后再往List⾥⾯添加SqlParameter对象,然后将List转为SqlParameter数组即可
List<SqlParameter> ilistStr = new List<SqlParameter>();
ilistStr.Add(new SqlParameter("@Param1", "aa"));
ilistStr.Add(new SqlParameter("@Param2", "bb"));
ilistStr.Add(new SqlParameter("@Param3", "cc"));
ilistStr.Add(new SqlParameter("@Param4", "dd"));
ilistStr.Add(new SqlParameter("@ParamLike","%keyword%"));//Like的写法
SqlParameter[] param = ilistStr.ToArray();param name
相关⽅法:
///<summary>
/
//获取数据库参数
///</summary>
///<param name="name">名称</param>
///<param name="value">值</param>
///<param name="type">数据类型</param>
///<param name="size">长度</param>
///<returns></returns>
public static SqlParameter GetParameter(string name, object value, SqlDbType type, int size)
{
SqlParameter param = new SqlParameter();
param.ParameterName = name;
if (type == SqlDbType.DateTime)
{
DateTime dtime;
if (DateTime.TryParse(value.ToString(), out dtime) == false || dtime.Year == 1)
{
param.Value = new DateTime(1970, 1, 1, 0, 0, 0);
}
else
{
param.Value = value;
}
}
else if (value == null)
{
param.Value = DBNull.Value;
}
else
{
param.Value = value;
}
param.SqlDbType = type;
param.Size = size;
return param;
}
调⽤
public static List<XModel.PATROL_XPerson> Select(string name,string phone,DateTime dt1,DateTime dt2)
{
List<SqlParameter> param = new List<SqlParameter>();
string sql = $@"select * from PATROL_X_PERSON as p
inner join PERSON_INFO i on p.per_code = i.per_code
where 1=1";
if (name != "")
{
sql += $" and per_name like @name";
param.Add(XTools.XDB.SQLHelper.GetParameter("@name", name, SqlDbType.Char, 32));
}
if (phone != "")
{
sql += $" and per_mobile like @phone";
param.Add(XTools.XDB.SQLHelper.GetParameter("@phone", phone,SqlDbType.Char,32));
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论