C # database Query summary

Source: Internet
Author: User

First, create a SQL Server database as a test database, create a table and fill in the test data

Database: SQL Server

Database name: Blog

Table Name: Test

Note: The database connection can use "dbl" file test, the specific use of Baidu can be.

1. Query all data from the database to DataGridView

Statement: SELECT * from Test

1   Private voidButton1_Click (Objectsender, EventArgs e)2  {3       stringConnStr ="Data source=long-pc\\long;initial Catalog=blog; User Id=long; Password=longtao";4       stringCmdstr="SELECT * from Test";5             6SqlConnection conn =NewSqlConnection (CONNSTR);7 Conn. Open ();8 9SqlCommand cmd =NewSqlCommand ();TenCmd.commandtext =Cmdstr; OneCmd. Connection =Conn; ASqlDataAdapter adap =NewSqlDataAdapter (cmd); -DataSet ds =NewDataSet (); - ADAP. Fill (DS); theDatagridview1.datasource = ds. tables[0]. DefaultView; -}

2. Query the database for some data and display it to DataGridView

Statement: String cmdstr = "Select Name, age from Test";

1  Private voidButton2_Click (Objectsender, EventArgs e)2  {3      stringConnStr ="Data source=long-pc\\long;initial Catalog=blog; User Id=long; Password=longtao";4      stringCmdstr ="select name, age from Test";5 6SqlConnection conn =NewSqlConnection (CONNSTR);7 Conn. Open ();8 9SqlCommand cmd =NewSqlCommand ();TenCmd.commandtext =Cmdstr; OneCmd. Connection =Conn; ASqlDataAdapter adap =NewSqlDataAdapter (cmd); -DataSet ds =NewDataSet (); - ADAP. Fill (DS); theDatagridview1.datasource = ds. tables[0]. DefaultView; -}

3. Displaying data using column aliases

Statement: String cmdstr = "SELECT name as name, age as aged from Test";

Note: In the statement "as" keyword, you can use a space instead

1   Private voidButton3_Click (Objectsender, EventArgs e)2   {3       stringConnStr ="Data source=long-pc\\long;initial Catalog=blog; User Id=long; Password=longtao";4       stringCmdstr ="select name as name, age as the from Test";5 6SqlConnection conn =NewSqlConnection (CONNSTR);7 Conn. Open ();8 9SqlCommand cmd =NewSqlCommand ();TenCmd.commandtext =Cmdstr; OneCmd. Connection =Conn; ASqlDataAdapter adap =NewSqlDataAdapter (cmd); -DataSet ds =NewDataSet (); - ADAP. Fill (DS); theDatagridview1.datasource = ds. tables[0]. DefaultView; -}

4. Using column calculations

Statement: String cmdstr = "Select name, age, language, math, English, Chinese + math + English as total score from Test";

1   Private voidButton4_Click (Objectsender, EventArgs e)2   {3      stringConnStr ="Data source=long-pc\\long;initial Catalog=blog; User Id=long; Password=longtao";4      stringCmdstr ="select name, age, language, math, English, Chinese + math + English as total score from Test";5 6SqlConnection conn =NewSqlConnection (CONNSTR);7 Conn. Open ();8 9SqlCommand cmd =NewSqlCommand ();TenCmd.commandtext =Cmdstr; OneCmd. Connection =Conn; ASqlDataAdapter adap =NewSqlDataAdapter (cmd); -DataSet ds =NewDataSet (); - ADAP. Fill (DS); theDatagridview1.datasource = ds. tables[0]. DefaultView; -}

5. Limit query Value range size

Statement: String cmdstr = "Select name, age, language, math, English, Chinese + math + English as total score from Test where (language + math + English) >215";

1   Private voidButton5_click (Objectsender, EventArgs e)2   {3       stringConnStr ="Data source=long-pc\\long;initial Catalog=blog; User Id=long; Password=longtao";4       stringCmdstr ="select name, age, language, math, English, Chinese + math + English as total score from Test where (language + math + English) >215";5 6SqlConnection conn =NewSqlConnection (CONNSTR);7 Conn. Open ();8 9SqlCommand cmd =NewSqlCommand ();TenCmd.commandtext =Cmdstr; OneCmd. Connection =Conn; ASqlDataAdapter adap =NewSqlDataAdapter (cmd); -DataSet ds =NewDataSet (); - ADAP. Fill (DS); theDatagridview1.datasource = ds. tables[0]. DefaultView; -}

C # database Query summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.