หัวข้อนี้ผู้เขียนจะสาทิตการใช้ ADO.NET อย่างง่ายที่สุด เมื่อท่านได้คิวรีที่ต้องการแล้วขั้นตอนต่อไปคือเขียนโปรแกรมภาษา C# ใน MSVS2008 ให้ท่านเปิดโปรแกรม MSVS2008 แล้วสร้างโปรเจ็กต์ใหม่ เลือกชนิดเท็มเพลตเป็น Console Application แล้วตั้งชื่อว่า AdoTest01 หรือชื่ออื่นๆ ตามใจชอบตามที่เห็นในภาพ 31-2 ภาพ 31-2: เลือกชนิดเท็มเพลตเป็น Console Application เมื่อสร้างโปรเจ็กต์ใหม่แล้วให้ป้อนพิมพ์โค้ดตามในภาพ 31-3 ลงในไฟล์ Program.cs เนื่องจากเป็นโค้ดสั้นๆ เพื่อทดสอบผู้เขียนจึงใส่ทั้งหมดไว้ภายในเมธอด Main เพื่อความสะดวก ในการเขียนโปรแกรมจริงท่านควรจะนิยามคลาสแยกไว้ต่างหาก บรรทัดที่ 1 และ 2 คำสั่ง Using จับรวมเนมสเปสที่จำเป็นต้องใช้สองเนมสเปสคือ System และ System.Data.SqlClient ซึ่งเป็นส่วนหนึ่งของ ADO.NET เพื่อให้โปรแกรมไคลแอนท์สามารถติดต่อกับ SQL2008 ได้ ภาพ 31-3: โค้ดภาษา C# บรรทัดที่ 10 ถึง 12 ทำหน้าที่ประกาศและกำหนดค่าให้แก่ตัวแปรชื่อ sqlConnect เป็นข้อความที่จะใช้เพื่อเชื่อมต่อกับเซอฟเวอร์ ท่านจะต้องแก้ไขชื่อเซอฟเวอร์ในบรรทัดที่ 10 ให้เป็นชื่อเซอฟเวอร์ที่ท่านต้องการติดต่อ หรือจะใส่เป็นไอพีแอดเดรสก็ได้เหมือนกัน โค้ดบรรทัดที่ 13 ถึง 18 ประกาศตัวแปรชื่อ sqlCommand ทำหน้าที่เก็บข้อความที่เป็นคิวรีเพื่อดึงข้อมูลจากเซอฟเวอร์ โปรดสังเกตว่าข้อความนี้คือคิวรีที่ผู้เขียนสาธิตการสร้างไว้ในหัวข้อที่ผ่านมา โค้ดบรรทัดที่ 19 ประกาศตัวแปรชื่อ connection เพื่อใช้ทำหน้าที่เป็นช่องทางติดต่อกับเซอฟเวอร์ โค้ดบรรทัดที่ 20 ประกาศตัวแปรชื่อ command เพื่อใช้ทำหน้าที่เป็นตัวส่งคำสั่งไปยัง SQL2008 โค้ดบรรทัดที่ 21 ประกาศตัวแปรชื่อ dataReader เพื่อใช้ทำหน้าที่อ่านแถวข้อมูลจาก SQL2008 การประกาศตัวแปรทั้งสามนี้ผู้เขียนกำหนดค่าเริ่มต้นเป็น null ไว้ทำให้ไม่มีออพเจ็กต์เกิดขึ้นจริงๆ เป็นเพียงแต่ตัวแปรที่มีชนิดข้อมูลตามระบุ ยกตัวอย่างเช่น dataReader ตอนนี้เป็นเพียงตัวแปรที่มีชนิดข้อมูลหรือไทป์เป็น SqlDataReader ยังไม่มีออพเจ็กต์ที่เป็น SqlDataReader เกิดขึ้นจริงๆ เพราะยังไม่ได้ใช้คำสั่ง new คำสั่ง new ในบรรทัดที่ 22 ทำหน้าที่สร้างออพเจ็กต์แบบ SqlConnection เพื่อใช้ทำหน้าที่เป็นช่องทางติดต่อกับเซอฟเวอร์ ผู้เขียนส่งค่า sqlConnect ไปเป็นอาร์กิวเมนต์เพราะเมธอดคอนสทรักเตอร์ของคลาสนี้รับพารามิเตอร์เป็นข้อความที่จะใช้เพื่อเชื่อมต่อกับเซอฟเวอร์ คำสั่งบรรทัดที่ 23 เรียกเมธอด Open ของคลาส SqlConnection เพื่อเริ่มต้นเปิดการเชื่อมต่อ คำสั่ง new ในบรรทัดที่ 24 ทำหน้าที่สร้างออพเจ็กต์แบบ SqlCommand เพื่อใช้ทำหน้าที่เก็บข้อความที่เป็นคิวรีซึ่งจะดึงข้อมูลจากเซอฟเวอร์ ผู้เขียนใส่อาร์กิวเมนต์สองตัวคือ sqlCommand และ connection เพราะเมธอดคอนสทรักเตอร์ของคลาสนี้รับพารามิเตอร์เป็นข้อความที่เป็นคิวรี และออพเจ็กต์ SqlConnection ที่ท่านจะใช้เป็นช่องทางติดต่อกับเซอฟเวอร์ บรรทัดที่ 25 สร้างออพเจ็กต์แบบ SqlDataReader ซึ่งมีวิธีสร้างวิตถารกว่าคลาสอื่นใด เพราะในขณะที่คลาสอื่นๆ สร้างโดยใช้คำสั่ง new และคอนสทรักเตอร์ แต่คลาส SqlDataReader กลับสร้างด้วยการเรียกเมธอด ExecuteReader ของคลาส SqlCommand ท่านสร้างออพเจ็กต์แบบ SqlDataReader ไว้เพื่อทำหน้าที่อ่านแถวข้อมูลแบบเดินหน้าได้ทางเดียว (ย้อนหลังไม่ได้) เมื่อโปรแกรมทำงานมาถึงบรรทัดที่ 26 นับว่าท่านมีเครื่องมือทั้งหมดที่จำเป็นต่อการเชื่อมต่อกับ SQL2008 และส่งคำสั่งเชื่อมต่อและคิวรีไปแล้ว ตอนนี้จึงพร้อมที่จะอ่านแถวข้อมูลผลลัพธ์ได้โดยใช้เมธอด Read ของออพเจ็กต์ dataReader คำสั่ง while มีไว้เพื่อให้ทำงานซ้ำอ่านแถวข้อมูลไปเรื่อยๆ จนกว่าจะหมด ตราบใดที่ยังมีข้อมูลอยู่เมธอด Read จะให้ค่าบูลีนเป็นจริง เมื่ออ่านไปถึงแถวสุดท้ายแล้ว Read จะให้ค่าบูลีนเป็นเท็จ ท่านสามารถใช้ค่าบูลีนนี้เป็นเงื่อนไขในคำสั่ง while เพื่อให้ยุติการวนซ้ำได้ บรรทัดที่ 27 ถึง 31 อันที่จริงเป็นคำสั่งบรรทัดเดียว ผู้เขียนแยกไว้หลายบรรทัดเพื่อให้ท่านอ่านเข้าใจได้ง่ายขึ้น ทั้งห้าบรรทัดนี้คือการใช้เมธอด WriteLine ของคลาส Console เพื่อพิมพ์แถวข้อมูลที่ได้รับมาจาก SQL2008 ออกบนจอภาพ โปรดสังเกตว่าวิธีอ้างถึงข้อมูลในคอลัมน์ต่างๆ ทำได้โดยใช้ชื่อ dataReader ตามด้วยวงเล็บเหลี่ยม และใส่ชื่อคอลัมน์เป็นสตริง (หรือจะใส่หมายเลขลำดับแทนก็ได้) ไว้ในวงเล็บเหลี่ยม สุดท้ายบรรทัดที่ 32 เมื่อท่านได้แถวข้อมูลครบหมดแล้ว และไม่จำเป็นต้องติดต่อกับ SQL2008 อีก ให้ปิดการเชื่อมต่อโดยเรียกเมธอด Close ของออพเจ็กต์ connect แล้วจบการทำงาน เมื่อป้อนพิมพ์และตรวจสอบดีแล้วว่าไม่มีการสะกดให้กดปุ่ม Ctrl+F5 เพื่อรันโปรแกรม ท่านจะเห็นกรอบ cmd ปรากฏขึ้นเหมือนในภาพ 31-4 สิ่งที่แสดงอยู่ใน cmd คือแถวข้อมูลที่เป็นผลลัพธ์ของการคิวรี โปรดสังเกตว่าข้อมูลเหล่านี้เหมือนผลลัพธ์ของคิวรีในหัวข้อก่อนหน้านี้ Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. System.Data.SqlClient Namespace
In this articleClassesSqlAuthenticationInitializer Defines the core behavior of authentication initializers that can be registered in the app.config file and provides a base for derived classes. SqlAuthenticationParameters Represents AD authentication parameters passed by a driver to authentication providers. SqlAuthenticationProvider Defines the core behavior of authentication providers and provides a base class for derived classes. SqlAuthenticationToken Represents an AD authentication token. SqlBulkCopy Lets you efficiently bulk load a SQL Server table with data from another source. SqlBulkCopyColumnMapping Defines the mapping between a column in a SqlBulkCopy instance's data source and a column in the instance's destination table. SqlBulkCopyColumnMappingCollection Collection of SqlBulkCopyColumnMapping objects that inherits from CollectionBase. SqlClientFactory Represents a set of methods for creating instances of the System.Data.SqlClient provider's implementation of the data source classes. SqlClientLogger Represents a SQL client logger. SqlClientMetaDataCollectionNames Provides a list of constants for use with the GetSchema method to retrieve metadata collections. SqlClientPermission Enables the .NET Framework Data Provider for SQL Server to help make sure that a user has a security level sufficient to access a data source. SqlClientPermissionAttribute Associates a security action with a custom security attribute. SqlColumnEncryptionCertificateStoreProvider The implementation of the key store provider for Windows Certificate Store. This class enables using certificates stored in the Windows Certificate Store as column master keys. For details, see Always Encrypted. SqlColumnEncryptionCngProvider The CMK Store provider implementation for using the Microsoft Cryptography API: Next Generation (CNG) with Always Encrypted. SqlColumnEncryptionCspProvider The CMK Store provider implementation for using Microsoft CAPI based Cryptographic Service Providers (CSP) with Always Encrypted. SqlColumnEncryptionEnclaveProvider The base class that defines the interface for enclave providers for Always Encrypted. SqlColumnEncryptionKeyStoreProvider Base class for all key store providers. A custom provider must derive from this class and override its member functions and then register it using SqlConnection.RegisterColumnEncryptionKeyStoreProviders(). For details see, Always Encrypted. SqlCommand Represents a Transact-SQL statement or stored procedure to execute against a SQL Server database. This class cannot be inherited. SqlCommandBuilder Automatically generates single-table commands that are used to reconcile changes made to a DataSet with the associated SQL Server database. This class cannot be inherited. SqlConnection Represents a connection to a SQL Server database. This class cannot be inherited. SqlConnectionStringBuilder Provides a simple way to create and manage the contents of connection strings used by the SqlConnection class. SqlCredential SqlCredential provides a more secure way to specify the password for a login attempt using SQL Server Authentication. SqlCredential is comprised of a user id and a password that will be used for SQL Server Authentication. The password in a SqlCredential object is of type SecureString. SqlCredential cannot be inherited. Windows Authentication ( SqlDataAdapter Represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database. This class cannot be inherited. SqlDataReader Provides a way of reading a forward-only stream of rows from a SQL Server database. This class cannot be inherited. SQLDebugging Included to support debugging applications. Not intended for direct use. SqlDependency The SqlDependency object represents a query notification dependency between an application and an instance of SQL Server. An application can create a SqlDependency object and register to receive notifications via the OnChangeEventHandler event handler. SqlEnclaveAttestationParameters Encapsulates the information SqlClient sends to SQL Server to initiate the process of attesting and creating a secure session with the enclave, SQL Server uses for computations on columns protected using Always Encrypted. SqlEnclaveSession Encapsulates the state of a secure session between SqlClient and an enclave inside SQL Server, which can be used for computations on encrypted columns protected with Always Encrypted. SqlError Collects information relevant to a warning or error returned by SQL Server. SqlErrorCollection Collects all errors generated by the .NET Framework Data Provider for SQL Server. This class cannot be inherited. SqlException The exception that is thrown when SQL Server returns a warning or error. This class cannot be inherited. SqlInfoMessageEventArgs Provides data for the event. SqlNotificationEventArgs Represents the set of arguments passed to the notification event handler. SqlParameter Represents a parameter to a SqlCommand and optionally its mapping to DataSet columns. This class cannot be inherited. For more information on parameters, see Configuring Parameters and Parameter Data Types. SqlParameterCollection Represents a collection of parameters associated with a SqlCommand and their respective mappings to columns in a DataSet. This class cannot be inherited. SqlProviderServices The DbProviderServices implementation for the SqlClient provider for SQL Server. SqlRowsCopiedEventArgs Represents the set of arguments passed to the SqlRowsCopiedEventHandler. SqlRowUpdatedEventArgs Provides data for the event. SqlRowUpdatingEventArgs Provides data for the event. SqlTransaction Represents a Transact-SQL transaction to be made in a SQL Server database. This class cannot be inherited. EnumsApplicationIntent Specifies a value for . Possible values are PoolBlockingPeriod Specifies a value for the property. SortOrder Specifies how rows of data are sorted. SqlAuthenticationMethod Describes the different SQL authentication methods that can be used by a client connecting to Azure SQL Database. For details, see Connecting to SQL Database By Using Azure Active Directory Authentication. SqlBulkCopyOptions Bitwise flag that specifies one or more options to use with an instance of SqlBulkCopy. SqlCommandColumnEncryptionSetting Specifies how data will be sent and received when reading and writing encrypted columns. Depending on your specific query, performance impact may be reduced by bypassing the Always Encrypted driver's processing when non-encrypted columns are being used. Note that these settings cannot be used to bypass encryption and gain access to plaintext data. For details, see Always Encrypted (Database Engine). SqlConnectionColumnEncryptionSetting Specifies that Always Encrypted functionality is enabled in a connection. Note that these settings cannot be used to bypass encryption and gain access to plaintext data. For details, see Always Encrypted (Database Engine). SqlNotificationInfo This enumeration provides additional information about the different notifications that can be received by the dependency event handler. SqlNotificationSource Indicates the source of the notification received by the dependency event handler. SqlNotificationType Describes the different notification types that can be received by an OnChangeEventHandler event handler through the SqlNotificationEventArgs parameter. DelegatesRemarksThe .NET Data Provider for SQL Server describes a collection of classes used to access a SQL Server database in the managed space. Using the SqlDataAdapter, you can fill a memory-resident DataSet that you can use to query and update the database. Note For conceptual information about using this namespace when programming with .NET, see SQL Server and ADO.NET. |