using 常用来引用命名空间
1 using System; 2 using System.Data; 3 using System.Data.SqlClient; 4 using System.Collections.Generic; 5 using Model; 6 using IDAL; 7 using DBUnititly;
using另一个用途是给类和命名空间指定别名
1 using spacename = system.io 2 3 using system 4 using Introduction = system.text; 5 class Test 6 ( 7 publ ic static int Main() 8 ( 9 Introduction::NamespaceEXample NS=new Introduction::NamespaceExample() 10 Console.WriteLine(NS.GetNamespace()); 11 return 0; 12 )
using还有一个用途是自动帮你释放资源
1 public DataSet Sedd() 2 { 3 DataSet ds = new DataSet(); 4 using (SqlConnection sqlconntion = new SqlConnection(connectionstring)) 5 { 6 sqlconntion.Open(); 7 using (SqlCommand sqlcomm = new SqlCommand("select * from users",sqlconntion)) 8 { 9 SqlDataAdapter dad = new SqlDataAdapter(); 10 dad.SelectCommand = sqlcomm; 11 dad.Fill(ds); 12 13 } 14 } 15 return ds; 16 }