C#類庫編譯及使用
阿新 • • 發佈:2019-02-07
1 using System;
2 using System.Data.SqlClient;
3 4 5 publicclass DataReaderSql
6 {
7 publicstaticint 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 try15 {
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 return0;
42 }
43 }
44 45
2 using System.Data.SqlClient;
3 4 5 publicclass DataReaderSql
6 {
7 publicstaticint Main(string[] args)
8 {
9 string source = Login.Connection ;
10 string select ="SELECT ContactName,CompanyName FROM Customers" ;
11 12 SqlConnection conn =new SqlConnection ( source ) ;
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(
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 return0;
42 }
43 }
44 45