Get the App
SLTechnology News&Howtos  ›  Development  › 

How to compile the C # Class Library

Shulou Source: shulou.com Published: 2022-06-02 09:05:52 10月06日 Update

This article will explain in detail how to compile the C# class library. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The question is raised: the existing class library file login.cs

UsingSystem; namespaceconn {publicclassLogin {/ / defines a static field (property) in this class and returns a string publicstaticstringConnection {get {return@ "Server=database_servername; DataBase=Northwind;userid=sa; password=yourpassword;";}} / / Note @ is not allowed! }}

At this point, you need to use the field Connection in class login in DataReaderSql (line 9 below)

DataReaderSql.cs

1 using System; 2 using System.Data.SqlClient; 3 45 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}

In other words, the problem we need to solve now is how to let the program know where Login.Connection is when the C # class library is compiled.

So what should we do to compile the C # class library?

Here we don't rely on namespace, we use dynamic link libraries.

The compilation of the C# class library is divided into 2 steps:

First, use the command csc/t:librarylogin.cs to compile login.dll

Second, use the command cscDataReaderSql.cs/r:login.dll to compile and point to the login.dll dynamic link library file to get DataReaderSql.exe

This is the end of the article on "how to compile the C# class library". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Compiling clocking articles dynamic commands fields library files more links questions not bad practical that is code content characters strings attributes point to Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Xiaomi vpn Microsoft