C#类库编译两步走

开发 后端
C#类库编译的使用步骤分两类:使用命令csc/t:librarylogin.cs编译得到login.dll和使用命令cscDataReaderSql.cs/r:login.dll编译并指向login.dll动态链接库文件得到DataReaderSql.exe
问题的提出:现有类库文件 login.cs
  1. usingSystem;  
  2. namespaceconn  
  3. {  
  4. publicclassLogin  
  5. {  
  6. //在此类中定义一个静态的字段(属性),  
  7. 返回一个字符串  
  8. publicstaticstringConnection  
  9. {  
  10. get{return@"Server=database_servername;  
  11. DataBase=Northwind;userid=sa;  
  12. password=yourpassword;";}  
  13. }  
  14. //注意@不可以少!  
  15. }  
  16. }  

此时需要在DataReaderSql使用到类login中的字段Connection(下面代码第9行)

DataReaderSql.cs

  1.  1  using System;  
  2.  2  using System.Data.SqlClient;  
  3.  3   
  4.  4   
  5.  5  public class DataReaderSql  
  6.  6   {  
  7.  7   public static int Main(  
  8. string[] args)  
  9.  8   {  
  10.  9     string  source = Login.Connection ;    
  11. 10    string   select = "  
  12. SELECT ContactName,CompanyName FROM Customers" ;  
  13. 11   
  14. 12  SqlConnection conn =   
  15. new SqlConnection ( source ) ;  
  16. 13   
  17. 14  try  
  18. 15  {  
  19. 16   using ( conn )  
  20. 17   {  
  21. 18   conn.Open ( ) ;  
  22. 19   
  23. 20  SqlCommand    cmd = new SqlCommand   
  24. select , conn ) ;  
  25. 21   
  26. 22  using ( SqlDataReader aReader =   
  27. cmd.ExecuteReader ( ) )  
  28. 23  {  
  29. 24 while ( aReader.Read ( ) )  
  30. 25    Console.WriteLine ( "'{0}'   
  31. from {1}" , aReader.GetString(0) ,   
  32. aReader.GetString ( 1 ) ) ;  
  33. 26   
  34. 27  aReader.Close ( ) ;  
  35. 28  }  
  36. 29  
  37. 30   conn.Close ( ) ;  
  38. 31   }  
  39. 32   }  
  40. 33     catch ( Exception e )  
  41. 34  {  
  42. 35       Console.WriteLine ( e ) ;  
  43. 36       Console.WriteLine ( ) ;  
  44. 37       Console.WriteLine ( "  
  45. Chances are your database does   
  46. not have a user" ) ;  
  47. 38       Console.WriteLine ( "  
  48. called QSUser, or you do not have   
  49. the NetSDK database installed." ) ;  
  50. 39  }  
  51. 40   
  52. 41  return 0;  
  53. 42   }  
  54. 43 }  

也就是说目前我们需要解决的问题是如何在C#类库编译的时候可以及时的让程序可以知道Login.Connection在哪里。

那么我们应该怎么做才能实现C#类库编译呢?

在这里我们不依靠namespace我们使用动态链接库。

C#类库编译分2步:

一、使用命令csc/t:librarylogin.cs编译得到login.dll

二、使用命令cscDataReaderSql.cs/r:login.dll编译并指向login.dll动态链接库文件得到DataReaderSql.exe

希望对刚开始学C#的并打算继续打好基础的人有所帮助!

【编辑推荐】

  1. C#中定义装箱和拆箱详解
  2. 浅谈C#类型系统
  3. 三种不同的C#异常类型
  4. 详细介绍C#编译器
  5. C#异常机制的相关解释
责任编辑:冰荷 来源: cnblogs
相关推荐

2010-09-10 13:34:41

2015-10-27 13:36:52

2009-10-21 09:46:13

VB使用ArrayLi

2009-12-21 18:02:02

WCF状态保存

2009-10-21 16:40:43

Oracle用户表空间

2010-09-06 15:56:12

PPPOE Serve

2010-07-13 10:52:02

2009-09-10 17:41:26

2010-07-15 19:41:43

Windows Vis

2011-08-01 15:48:35

数据库降级SQL Server

2009-11-03 13:02:28

VB.NET Wind

2010-05-14 11:46:53

Windows 7拨号设置

2011-05-03 10:42:24

打印照片

2011-05-03 10:38:31

打印机

2017-02-28 21:37:01

Windows 10重启系统

2009-10-19 13:34:18

VB.NET条形码编程

2009-08-14 15:07:00

C#编译过程

2009-08-28 16:29:02

C#类库工程

2013-04-18 11:07:31

2021-09-23 22:36:30

手机数据二手
点赞
收藏

51CTO技术栈公众号