请高手看看这段代码,插入数据问题
/// <summary>
/// 获取随机字符串
/// </summary>
/// <param name= "strLength "> 字符串长度 </param>
/// <param name= "Seed "> 随机函数种子值 </param>
/// <param name= "Rows "> 字符串行数 </param>
/// <param name= "num "> 批号 </param>
/// <returns> 指定长度的随机字符串 </returns>
public static string RndString(int num,int Rows,params int[] Seed )
{
string strSep = ", ";
char[] chrSep = strSep.ToCharArray();
//因1与l不容易分清楚,所以去除
string strChar = "2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z ";
string[] aryChar= strChar.Split( chrSep, strChar.Length );
string strRandom = string.Empty;
int strLength=7;
Random Rnd;
if( Seed != null && Seed.Length > 0 )
{
Rnd = new Random( Seed[0] );
}
else
{
Rnd = new Random();
}
for(int j=0 ; j < Rows; j++)//分行
{
//生成随机字符串
for( int i=0; i < strLength; i++ )
{
strRandom +=aryChar[Rnd.Next( aryChar.Length )];
}
string sql = "Insert into edu_card(card_id) values( ' "+strRandom+ " ') ";
include.insertData(sql);
strRandom =strRandom+num+ " <br> ";
}
return strRandom;
}
我要随机生成7位字符串,分行插入数据库,现在这个方法只能插入第一行,不能循环插入,怎么写才能循环插入?
------解决方案--------------------这个函数是 生成串
你可以使用如下方法:
ArrayList al = new ArrayList();
private void button1_Click(object sender, EventArgs e)
{
//ArrayList al = new ArrayList();
for (int i = 0; i < 100; i++)
{
string s_Rand = RndString(/*your params*/);
al.Add(s_Rand);
}
}
private bool AddToSQL(string s_Rand)
{
//处理SQL操作
}
public void AddToSQLList(ArrayList _al)
{
for (int i = 0; i < _al.Count; i++)
{
AddToSQL(((string)_al[i]));
}
}