connection连接效率问题

Code:
  1. using System;   
  2. using System.Configuration;   
  3. using System.Data.SqlClient;   
  4. using System.Diagnostics;   
  5.   
  6. namespace ConsoleApplication1   
  7. {   
  8.     class connect   
  9.     {   
  10.         static void Main(string[] args)   
  11.         {   
  12.             string _connString = "Data Source=SAMEWAYDEV06;Initial Catalog=DataImport;Persist Security Info=True;User ID=sa;Password=sa";   
  13.   
  14.             SqlConnection conn = new SqlConnection(_connString);   
  15.   
  16.             Stopwatch sw = new Stopwatch();   
  17.   
  18.             sw.Start();   
  19.             conn.Open();   
  20.             sw.Stop();   
  21.             Console.WriteLine("1连接所用时间:{0}", sw.ElapsedTicks.ToString());   
  22.   
  23.             conn.Close();//关闭连接   
  24.   
  25.             sw.Reset();   
  26.             sw.Start();   
  27.             conn.Open();//现在是直接从连接池中分配的连接,所以速度要快很多   
  28.             sw.Stop();   
  29.   
  30.             Console.WriteLine("2连接所用时间:{0}", sw.ElapsedTicks.ToString());   
  31.   
  32.             conn.Dispose();//释放连接   
  33.   
  34.             conn.ConnectionString = _connString;   
  35.   
  36.             sw.Reset();   
  37.             sw.Start();   
  38.             conn.Open();//Dispose()后,仍然可以连接!(前提是必须重设连接字符串)   
  39.             sw.Stop();   
  40.             Console.WriteLine("3连接所用时间:{0}", sw.ElapsedTicks.ToString());//从输出结果上看,这一次仍然要比首次连接快很多,貌似也是从连接池中取的   
  41.   
  42.             conn.Close();   
  43.   
  44.   
  45.             using (SqlConnection conn2 = new SqlConnection(_connString))   
  46.             {   
  47.                 try  
  48.                 {   
  49.                     sw.Reset();   
  50.                     sw.Start();   
  51.                     conn2.Open();//即使创建一个完全不同的新Connection对象,只要连接字符串相同,仍然会从连接池内中分配已有连接,所以速度仍然很快   
  52.                     sw.Stop();   
  53.                     Console.WriteLine("4连接所用时间:{0}", sw.ElapsedTicks.ToString());   
  54.                 }   
  55.                 catch (Exception)   
  56.                 {   
  57.   
  58.                     throw;   
  59.                 }   
  60.                 finally  
  61.                 {   
  62.                     conn2.Close();   
  63.                 }   
  64.             }   
  65.   
  66.   
  67.   
  68.             //连续多次调用Close()或Dispose()不会抛出异常,但连续多次调用Open()会抛出异常   
  69.             //conn.Close();   
  70.             //conn.Close();   
  71.   
  72.             //conn.Dispose();   
  73.             //conn.Dispose();   
  74.             Console.Read();   
  75.   
  76.             //即使不关闭连接,程序退出时,连接也被会自动销毁   
  77.         }   
  78.     }   
  79. }  

1连接所用时间:480219
2连接所用时间:130
3连接所用时间:60
4连接所用时间:47

 

Code:
  1. using System;   
  2. using System.Reflection;   
  3. namespace console   
  4. {   
  5.     class Program   
  6.     {   
  7.         static void Main(string[] args)   
  8.         {   
  9.             //不会改变   
  10.             int i = 4;   
  11.             Console.WriteLine("before change,i = {0}", i);   
  12.             changeint(i, 5);   
  13.             Console.WriteLine("after change,i = {0}", i);   
  14.   
  15.             //改变   
  16.             Ints ii =new Ints(){i =4};   
  17.              Console.WriteLine("before change,ii = {0}", ii.i);   
  18.             changeints(ii, 5);   
  19.             Console.WriteLine("after change,ii = {0}", ii.i);   
  20.   
  21.             //改变   
  22.             Ints ii2 = new Ints() { i = 4 };   
  23.             Console.WriteLine("before change,ii = {0}", ii2.i);   
  24.             changeints2(ref ii2, 5);   
  25.             Console.WriteLine("after change,ii = {0}", ii2.i);   
  26.   
  27.             //反射修改private属性   
  28.             peason p = new peason();   
  29.             PropertyInfo info = p.GetType().GetProperty("Name");   
  30.             info.SetValue(p, "xueyong"null);   
  31.             Console.WriteLine(p.Name);   
  32.         }   
  33.   
  34.         static void changeint(int i, int j)   
  35.         {   
  36.             i = j;   
  37.         }   
  38.         static void changeints(Ints m, int p)   
  39.         {   
  40.             m.i = p;   
  41.         }   
  42.         static void changeints2(ref Ints m, int p)   
  43.         {   
  44.             m.i = p;   
  45.         }   
  46.   
  47.     }   
  48.     public class Ints   
  49.     {   
  50.         public int i { setget; }    
  51.     }   
  52.     public class peason   
  53.     {   
  54.         private string _name;   
  55.         public string Name   
  56.         {   
  57.             private set  
  58.             {   
  59.                 _name = value;   
  60.             }   
  61.             get  
  62.             {   
  63.                 return _name;   
  64.             }   
  65.         }   
  66.     }   
  67. }  


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值