C # Connecting SQL Server Simple Tutorial

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

1. Create a new C # Console Application

2. go to Server Explorer, right-click Add Connection at Data connection (PIC1), enter the server name and database name (sometimes the server name does not appear in the dropdown box, you can go to SQL Server Management Studio Pic2 in the black bar), point out the test connection, if it fails, you need to see if the service is started

3. Write the following code in the program to run

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data.SqlClient;namespacelinksqlconsole{classProgram {Static voidMain (string[] args) {SqlConnection conn=NewSqlConnection (); Conn. ConnectionString="Connection String";//See figure Pic3Conn.                Open (); SqlCommand cmd=NewSqlCommand (); Cmd. Connection=Conn; Cmd.commandtext="SQL statements";//SELECT * FROM table namecmd.                ExecuteNonQuery (); SqlDataReader SqlDataReader=cmd.                ExecuteReader ();  while(Sqldatareader.read ()) {stringStuid = sqldatareader.getstring (0); stringStuname = sqldatareader.getstring (1); intAge = Sqldatareader.getint32 (2); Console.WriteLine (Stuid+" "+ Stuname +" "+Age );                } sqldatareader.close (); Conn.        Close (); }    }}

Pic1

Pic2

Pic3

C # Connecting SQL Server Simple Tutorial

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.