TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# SqlCommand Example: SELECT TOP, ORDER BY

Use SqlCommand from System.Data.SqlClient. Specify SELECT TOP, ORDER BY statements.
SqlCommand. This type deals with databases. It executes SQL commands on a database. It sends an SQL command to a database that is specified by an SqlConnection object.
We then call instance methods to physically apply the command to the database. We use a SELECT TOP command text, and also ORDER BY.
Example. This program uses the SqlCommand data object in C#. We use SqlCommand in the context of a complete but trivial program that reads data from a specific database on the disk.

Info: The SqlCommand type has a public parameterized constructor and it can be used with one or more arguments.

And: The first parameter specifies the SQL statement. The second parameter is the SqlConnection. The third parameter is the SqlTransaction.

Important: This program interfaces with the SQL Server engine. It uses some project-specific external data.

SqlClientSqlDataReader

So: It will not work for you unless you change the connection string reference and the SQL command text for your environment.

C# program that uses SqlCommand instance using System; using System.Data.SqlClient; class Program { static void Main() { // // The program accesses the connection string. // ... It uses it on a connection. // string conString = ConsoleApplication1.Properties.Settings.Default.ConnectionString; using (SqlConnection connection = new SqlConnection(conString)) { connection.Open(); // // SqlCommand should be created inside using. // ... It receives the SQL statement. // ... It receives the connection object. // ... The SQL text works with a specific database. // using (SqlCommand command = new SqlCommand( "SELECT TOP 3 * FROM Dogs1 ORDER BY Weight", connection)) { // // Instance methods can be used on the SqlCommand. // ... These read data. // using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { Console.WriteLine(reader.GetValue(i)); } Console.WriteLine(); } } } } } } Output 7 Candy Yorkshire Terrier 25 Charles Cavalier King Charles Spaniel 57 Koko Shar Pei
Using. The SQL data objects are wrapped in a resource acquisition statement. This ensures that resources are properly acquired and disposed of when no longer needed.Using
Constructor. The call to the SqlCommand constructor uses 2 parameters. The first parameter is the actual SQL source text. This string uses SQL and is not compiled by the C# compiler.

And: The second parameter is the SqlConnection object. This is set up elsewhere in the program.

Overview: You must manually specify the connection to use on the SqlCommand. This can be done with a parameter on the constructor.

A summary. We wrote a complete program that reads from a database on the local disk. The SqlCommand has a constructor. You can specify its connection and the SQL text.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf