日期:2014-05-18 浏览次数:20613 次
ArrayList arylist = new ArrayList(); arylist.Add(TextBox1.Text.ToString()); arylist.Add(TextBox2.Text.ToString()); arylist.Add(TextBox3.Text.ToString()); for (int i = arylist.Count - 1; i >= 0; i--) { if (arylist[i].ToString().Equals("")) { arylist.Remove(arylist[i]); } } StringBuilder strSql = new StringBuilder(); strSql.Append("insert into yourtable("); for (int i = 0; i < arylist.Count; i++) { if (i.Equals(arylist.Count - 1)) { strSql.Append(" cd" + i.ToString() + ")"); } else { strSql.Append(" cd" + i.ToString() + ","); } } strSql.Append(" values("); for (int i = 0; i < arylist.Count; i++) { if (i.Equals(arylist.Count - 1)) { strSql.Append("{'" + i.ToString() + "'})"); } else { strSql.Append("{'" + i.ToString() + "'},"); } } string strQ = string.Format(strSql.ToString(), arylist.ToArray());
------解决方案--------------------
string[] textStrArray=new string[3] {TextBox1.Text ,TextBox2.Text ,TextBox3.Text };
string[] dataFieldArray=new string[3]{"cd1","cd2","cd3"};
string insertString="":
int index=0;
foreach(string tmpStr in textStrArray)
{
if(!string.IsNullOrEmpty(tmpStr))
insertString+= dataFieldArray[index++]+ tmpStr;
}
后面加的那个应该不是太正确的,自己改吧,我自己贪方便,插入语句一定不是这个格式