//生成不重复随机数
int[] num = new int[10];
int[] newNum = new int[10];
int i, r = 9;
int n; int tmp;
Random rand = new Random();
for (i = 0; i < 10; i++)//初始化这个数组,0~9
{ num[i] = i; } ///这个算法只需要循环你的数范围的次数而已
for (i = 0; i < 10; i++)
{
n = rand.Next(0, r);//随机产生一个0~9之间的数,r的初始值是9
newNum[i] = num[n];//把产生的随机数当成num的下标赋给newNum数组
tmp = num[n];//然后把num[n]和它最后一个数交换(num[r]),这是避免重复
num[n] = num[r];
num[r] = tmp;
r--;//自减,下次产生的随机数就可以从0到8了,
}
for (i = 0; i < 10; i++)
{
Console.Write("{0} ", newNum[i]);
}
Console.Read();
//Int32转3字节数组
byte b1,b2,b3,b4,b5,b6,b7,b8,b9;
int it= -83886;
byte[] bs = System.BitConverter.GetBytes(it);
b1 = bs[0];
b2 = bs[1];
b3 = bs[2];
b4=bs[3];
b4 >>= 7;
b4 <<= 7;
b5 = (byte)(b4 | b3);
//3字节数组转Int32
b6 = b5;
b6 >>= 7;
if(b6==1)
b7 = (byte)(255);
else
b7=(byte)0;
int l = System.BitConverter.ToInt32(new byte[] { bs[0], bs[1],b5, b7 }, 0);
public static DataTable RunSPReturnDataTable(string spName, Paras paras, SqlConnection conn)
{
try
{
SqlCommand salesCMD = new SqlCommand(spName, conn);
salesCMD.CommandType = CommandType.StoredProcedure;
/// 加上他们的参数
foreach(Para para in paras)
{
SqlParameter myParm = salesCMD.Parameters.AddWithValue(para.ParaName, para.DAType);
myParm.Value = para.val;
}
salesCMD.CommandTimeout = 90;
SqlDataAdapter sda = new SqlDataAdapter(salesCMD);
if (conn.State==System.Data.ConnectionState.Closed)
conn.Open();
DataTable dt = new DataTable();
sda.Fill(dt);
if (conn.State == System.Data.ConnectionState.Open)
conn.Close();
sda.Dispose();
return dt;
}
catch(System.Exception ex)
{
throw ex;
}
}
public static int RunSP(string spName, Dictionary<string,string> paras=null, SqlConnection conn)
{
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand cmd = new SqlCommand(spName, conn);
cmd.CommandType = CommandType.StoredProcedure;
if(paras!=null)
{
// 加入参数
foreach (Para para in paras)
{
SqlParameter myParameter = new SqlParameter(para.ParaName, para.val);
myParameter.Size = para.Size;
cmd.Parameters.Add(myParameter);
}
}
int i = cmd.ExecuteNonQuery();
conn.Close();
return i;
}
///
private static DataTable RunSQLReturnTable(string sql, Paras paras)
{
SqlConnection conn = new SqlConnection(SystemConfig.AppCenterDSN);
if (conn.State != ConnectionState.Open)
conn.Open();
SqlDataAdapter ada = new SqlDataAdapter(sql, conn);
ada.SelectCommand.CommandType = CommandType.Text;
// 加入参数
foreach (Para para in paras)
{
SqlParameter myParameter = new SqlParameter(para.ParaName, para.val);
myParameter.Size = para.Size;
ada.SelectCommand.Parameters.Add(myParameter);
}
try
{
DataTable oratb = new DataTable("otb");
ada.Fill(oratb);
ada.Dispose();
conn.Close();
return oratb;
}
catch (Exception ex)
{
ada.Dispose();
conn.Close();
throw new Exception("SQL=" + sql + " Exception=" + ex.Message);
}
}