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