C# 可变参数params的一个应用
public bool UpdateCodeBak(string OrderExId, params string[] CodeBak)
{
bool bo = false;
StringBuilder set=new StringBuilder();
System.Data.SqlClient.SqlParameter[] cmdParms = new System.Data.SqlClient.SqlParameter[CodeBak.Length];
set.Append(" set ");
for (int i = 1; i <= CodeBak.Length; i++)
{
System.Data.SqlClient.SqlParameter par=new System.Data.SqlClient.SqlParameter("@CodeBak"+i.ToString(), CodeBak[i-1]);
set.Append(" CodeBak"+i.ToString()+"=@CodeBak"+i.ToString()+",");
cmdParms[i - 1] = par;
}
string sql = "update OrderEx_GongYi" + set.ToString().Substring(0,set.Length - 1) + " where OrderExId='" + OrderExId + "'";
bo=Convert.ToBoolean(DbHelperSQL.ExecuteSql(sql, cmdParms));
return bo;
}
调用:
方式1: dms.UpdateCodeBak("4aaf85fc-9706-4515-96a4-d77eeee7e588", "1asdafadff1", "jjjj2", "g3", "g4"); //主键字符串之后,可以加不定个数的参数
方式2:
string[] CodeBak = { "aa", "bb", "cc" };
dms.UpdateCodeBak("4aaf85fc-9706-4515-96a4-d77eeee7e588", CodeBak);
复习传递字符数组作为参数:
private void button7_Click(object sender, EventArgs e)
{
//dms.UpdateCodeBak("4aaf85fc-9706-4515-96a4-d77eeee7e588", "aa", "bb", "cc");
string[] CodeBak = { "abba", "bbbb", "cbbc" };
UpdateCodeBak("4aaf85fc-9706-4515-96a4-d77eeee7e588", CodeBak);
//dms.UpdateCodeBak("4aaf85fc-9706-4515-96a4-d77eeee7e588", "1asdafadff1", "jjjj2", "g3", "g4");
}
public void UpdateCodeBak(string OrderExId, string[] CodeBak)
{
dms.UpdateCodeBak("4aaf85fc-9706-4515-96a4-d77eeee7e588", CodeBak);
}