Connect to SQL Server with VS and perform simple query functions

Source: Internet
Author: User

VS can be edited in multiple languages, each language has a variety of different controls, I think the C # language in connection with the database is more convenient and simple, so through the C # language in VS in the new Window program to connect the database.

1. Open vs, select language C #, create a new window application.

2. Select View, Toolbox, drag the control in the data column of the GridView control, drag the control in the Public column button to form 1

3. Double-click the button to enter the code editing area and enter the following code

String Connsql ="SERVER=YQB (your computer name);d atabase=st024 (your database name); Integrated SECURITY=SSPI";//database connection string, database is set to the name of its databases, Windows authentication                Try    {        using(SqlConnection conn =NewSqlConnection ()) {Conn. ConnectionString=Connsql; Conn. Open (); //Open a database connectionString SQL="SELECT * from s024 (your table name)";//Query StatementsSqlDataAdapter Myda=NewSqlDataAdapter (SQL, conn);//instantiating an adapterDataTable DT=NewDataTable ();//instantiating a data tableMyda. Fill (DT);//Save DataDatagridview1.datasource= DT;//set to DataGridViewConn. Close (); //To close a database connection        }    }    Catch(Exception ex) {MessageBox.Show ("error message:"+ ex. Message,"error occurred"); }}

Note: You need to declare before:

using System.Data.SqlClient;

Interested can try to modify the query statement

SELECT * FROM s024 (your table name)

Replace some of these variables with Butoon or a ComboBox for more interesting operations.

Connect to SQL Server with VS and perform simple query functions

Related Article

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.