C#类库编译及使用
2006-10-19 14:27
309 查看
问题的提出:
现有类库文件 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#的并打算继续打好基础的人有所帮助!
现有类库文件 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#的并打算继续打好基础的人有所帮助!
相关文章推荐
- C# 类库使用嵌入资源
- C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母
- 使用响应文件编译C#源文件
- C# .NET 使用第三方类库DotNetZip解压/压缩Zip文件 (ZT)
- C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母
- 简介 WatiN是一个开源的用于Web测试自动化的类库,从watir中获得的灵感,使用C#开发。WatiN通过与浏览器的交互来实现自动化,使用起来具有轻便,简单的特点。目前最新版本为2.0,加入了对
- C#动态编译引擎-CS-Script 简单使用
- C#反射实例(一) 利用反射使用类库
- 使用VS下载C#API(.NET Framework 类库)
- 使用.NET命令行编译器编译项目(如ASP.NET、C#等)
- C#调用exe解决PDF转图片问题(使用PDFBox方案,java语言编译jar包实现)
- C#反编译工具Reflector使用方法,(支持VB、Delphi等)
- 使用IKVMC命令编译shiro类库使其在.net环境下可用
- 转:使用IDispatch::Invoke函数在C++中调用C#实现的托管类库方法
- 使用反编译工具反编译Dll,编译成C#
- 编译及安装QCA类库及使用
- 开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式
- 开源Math.NET基础数学类库使用(05)C#解析Delimited Formats数据格式
- 手动使用Csc命令编译C#文件
- 在C#中使用c++编译的ocx的方法