C#类库编译及使用

问题的提出:
    现有类库文件 login.cs

 

   using  System;
   
namespace  conn
    {

   
public   class  Login
   {
           
// 在此类中定义一个静态的字段(属性),返回一个字符串
      public   static   string  Connection
      {
    
get  {  return   @" Server=database_servername;DataBase=Northwind;user id=sa;password=yourpassword; " ; }
      }
          
// 注意  @不可以少!
     }
    } 


  


*******************************************************************

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

  DataReaderSql.cs
******************************************************************
  

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


**********************************************************

也就是说目前我们需要解决的问题是如何在编译的时候可以及时的让程序可以知道Login.Connection 在哪里。
那么我们应该怎么做呢?
  在这里我们不依靠namespace我们使用动态链接库。
  分2步:
   I  使用命令csc /t:library  login.cs   编译得到  login.dll

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值