当前位置:  编程技术>.net/c#/asp.net
本页文章导读:
    ▪C# Random生成相同随机数的解决方案      1.生成任意随机数 Random random = new Random(); random.Next(minvale, maxvale); 时间短重复 2.利用种子生成不重复随机数 (a)生成随机数时:Random ran = new Random((int)DateTime.Now.Ticks); ran .Next(minvale, maxvale); .........
    ▪用C#实现单链表(取第i个结点元素,删除第i个结点)      初学C#记录历程,记录心情。节点类和链表类参考前篇。在接口IList中增加:           T GetElem(int i);   //取第i个元素       &nb.........
    ▪ASP.NET下将规则的Excel导入数据库      今天接到新的需求,要求将Excel表格中的数据显示在页面上。我个人分析,首先要将Excel中的数据存到数据库中,再进行页面显示,本人菜鸟级别,以前没有做过读取Excel数据,研究了一下(主.........

[1]C# Random生成相同随机数的解决方案
    来源:    发布时间: 2013-10-28

1.生成任意随机数

Random random = new Random();

random.Next(minvale, maxvale);

时间短重复

2.利用种子生成不重复随机数

(a)生成随机数时:Random ran = new Random((int)DateTime.Now.Ticks);

ran .Next(minvale, maxvale);

可以有效解决重复问题

PS: DateTime.Now.Ticks 为数据数的计时周期数

(b) Random random = new Random( GetRandomSeed( ) );//使用加密服务提供程序 (CSP) 提供的实现来实现加密随机数生成器 (RNG) s

tatic int GetRandomSeed( ) {

byte[] bytes = new byte[4];

System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider( ); rng.GetBytes( bytes );

return BitConverter.ToInt32( bytes , 0 );

}

或者 Random sourceGen = new Random(new Guid().GetHashCode());//利用Guid

3.利用延时运行时间解决重复问题

Thread.Sleep(100);

本文链接


    
[2]用C#实现单链表(取第i个结点元素,删除第i个结点)
    来源:    发布时间: 2013-10-28

初学C#记录历程,记录心情。

节点类和链表类参考前篇。

在接口IList中增加:

           T GetElem(int i);   //取第i个元素
           void ListDelete(int i); //删除第i个结点

在链表类LinkList里面直接增加方法。

取第i个元素:

1 /// <summary>
2 /// 取第i个元素
3 /// </summary>
4 /// <param name="i">i为要取元素的位置</param>
5 /// <returns></returns>
6 public T GetElem(int i)
7 {
8 if (i < 1 || i > this.GetLength() || this.head == null)
9 {
10 Console.WriteLine("The location you want to get is inexistent");
11 return default(T); //对于引用类型会返回空,对于数值类型会返回零
12 }
13 else
14 {
15 LinkNode<T> node = this.head;
16 int j = 1;
17 while (j < i) //寻找第i个结点,node指向它,i〉=1
18 {
19 node = node.Next;
20 j++;
21 }
22 return node.Data;
23 }
24 }

删除第i个结点:

1 /// <summary>
2 /// 删除第i个结点
3 /// </summary>
4 /// <param name="i">第i个</param>
5 public void ListDelete(int i)
6 {
7 LinkNode<T> node = this.head;
8 int j = 1;
9 if (i < 1 || i > this.GetLength() || this.head == null) //i不合法,大于链表长度或者是空链表时,给出message
10 {
11 Console.WriteLine("The location you want to get is inexistent");
12 }
13
14 else
15 {
16 if (i == 1) //如果是要删除第1个
17 {
18 this.head = node.Next;
19 }
20
21 else
22 {
23 while (j < i - 1) //寻找第i个结点,并令node指向其前趋, i〉=2
24 {
25 node = node.Next;
26 j++;
27 }
28 node.Next = node.Next.Next;
29 }
30 }
31
32 }

验证是否正确:

1 static void Main(string[] args)
2 {

    
[3]ASP.NET下将规则的Excel导入数据库
    来源:    发布时间: 2013-10-28

今天接到新的需求,要求将Excel表格中的数据显示在页面上。

我个人分析,首先要将Excel中的数据存到数据库中,再进行页面显示,本人菜鸟级别,以前没有做过读取Excel数据,研究了一下(主要是看别人的资料),写一下实现过程,我想写几篇关于Excel的,首先是规则的Excel数据导入,再有就是不规则的Excel数据导入,还有就是根据数据生成Excel。

下面开始:将规则的Excel导入数据库

首先看一下Excel结构,如图:

这是一个简单的、规整的Excel格式,将它导入到数据库中

1 protected void btnImport_Click(object sender, EventArgs e)
2 {
3 if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
4 {
5 Response.Write("<script>alert('请您选择Excel文件')</script> ");
6 return;//当无文件时,返回
7 }
8 string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
9 if (IsXls != ".xls")
10 {
11 if(IsXls!=".xlsx")
12 {
13 Response.Write("<script>alert('只可以选择Excel文件')</script>");
14 return;//当选择的不是Excel文件时,返回
15 }
16 }
17 string filename = FileUpload1.FileName; //获取Execle文件名 DateTime日期函数
18 string savePath = Server.MapPath(("upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径
19 FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上
20 DataSet ds = ExcelSqlConnection(savePath, filename,IsXls); //调用自定义方法
21 DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组
22 int rowsnum = ds.Tables[0].Rows.Count;
23 if (rowsnum == 0)
24 {
25 Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); //当Excel表为空时,对用户进行提示
26 }
27 else
28 {
29 for (int i = 0; i < dr.Length; i++)
30 {
31 //前面除了你需要在建立一个“upfiles”的文件夹外,其他的都不用管了,你只需要通过下面的方式获取Excel的值,然后再将这些值用你的方式去插入到数据库里面
32 string title = dr[i]["标题"].ToString();
33 string linkurl = dr[i]["链接地址"].ToString();
34 string categoryname = dr[i]["分类"].ToString();
35
36 //Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
37 }
38 Response.Write("<script>alert('Excle表导入成功!');</script>");
39 }
40
41 }
42
43 #region 连接Excel 读取Excel数据 并返回DataSet数据集合
44 /// <summary>
45 /// 连接Excel 读取Excel数据 并返回DataSet数据集合
46 /// </summary>
47 /// <param name="filepath">Excel服务器路径</param>
48 /// <p
    
最新技术文章:
 




特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3