动态添加SqlParameter

[方法一]:

动态向SqlParameter 里添加相应参数,方法如下

先定义一个List,然后再往List里面添加SqlParameter对象,然后将List转为SqlParameter数组即可 

List<SqlParameter> ilist = new List<SqlParameter>();
            ilist.Add(new SqlParameter("@Param1", "1"));
            ilist.Add(new SqlParameter("@Param2", "2"));
            ilist.Add(new SqlParameter("@Param3", "3"));
            ilist.Add(new SqlParameter("@Param4", "4"));
            ilist.Add(new SqlParameter("@ParamLike","%5%"));//Like的写法
            SqlParameter[] param = ilist.ToArray();
(demo1)
 List<SqlParameter> sqlList = new List<SqlParameter>();
            StringBuilder  sqltext = new StringBuilder("select * from ProductSn where REF >@REF");
            if (checkBox1.Checked == true & textBox1.Text.ToString().Trim() != "")
            {
                sqltext.Append(" and INVOICENO= @INVOICENO ");               
                SqlParameter INVOICENO =  new SqlParameter("@INVOICENO", SqlDbType.NVarChar, 50);
                INVOICENO.Value = textBox1.Text.ToString().Trim();
                INVOICENO.Direction = ParameterDirection.Input;
                sqlList.Add(INVOICENO);
            }
            if (checkBox1.Checked == true & textBox2.Text.ToString().Trim() != "" & textBox3.Text.ToString().Trim() != "")
            {
                sqltext.Append(" and [BIKE SN]  @beginSn and @endsn");
                SqlParameter BeginSn = new SqlParameter("@beginSn", SqlDbType.NVarChar, 50);
                BeginSn.Direction = ParameterDirection.Input;
                BeginSn.Value = this.textBox2.Text.ToString();
                SqlParameter EndSn = new SqlParameter("@endsn", SqlDbType.NVarChar, 50);
                EndSn.Direction = ParameterDirection.Input;
                EndSn.Value = this.textBox3.Text.ToString();
                sqlList.Add(BeginSn);
                sqlList.Add(EndSn);
            }
            SqlParameter[] parms = sqlList.ToArray();
            _DBConfig.DB.SqlQuery(sqltext.ToString(), parms);
 
 [方法二]
sqlsp = "BlogArticle_Update";

                SqlParameter[] param = {

                                        new SqlParameter("Content",Content.Value),

                                    };

                //重构数组

                SqlParameter[] param2;

                if (IsDefineWeight == "1")

                {

                    int newNum = param.Length + 1;

                    param2 = (SqlParameter[])Redim(param, newNum);

                    param2[newNum - 1] = new SqlParameter("Weight", weight.Text);

                }

                else

                {

                    param2 = param;

                }



                DAL.dbsp.ExecuteNonQuery(sqlsp, param2);
public static Array Redim(Array origArray, Int32 desiredsize)

        {

            //确定每个元素类
型    

            Type t = origArray.GetType().GetElementType();

            //创建一个含有期望元素个数的新数组  

            //新数组的类型必须匹配原数组的类型  

            Array newArray = Array.CreateInstance(t, desiredsize);

            //将原数组中的元素拷贝到新数组中  

            Array.Copy(origArray, 0, newArray, 0, Math.Min(origArray.Length, desiredsize));

            return newArray;

        }

 

 
posted @ 2013-05-16 09:13  孺牛码塔  阅读(833)  评论(0编辑  收藏  举报