C # connect to the SQL Server database,

Source: Internet
Author: User

C # connect to the SQL Server database,
C # connect to the SQL Server database. Here we will briefly introduce common methods for connecting to the SQL Server database. The database to be connected is the local SQL Server, the official Northwind database. Create a form application in VS2010 and add a button to verify that the database is connected successfully. Compile the button background Code as follows:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. data. sqlClient; namespace CSharpConnectSQL {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnCSharpConnectSQL_Click (object sender, EventArgs e) {string strConnection = "Server = localhost;"; strConnection + = "initial catalog = Northwind;"; strConnection + = "user id = sa;"; strConnection + = "password = 123 ;"; strConnection + = "Connect Timeout = 5"; bool canConnectSQL = false; using (SqlConnection objConnection = new SqlConnection (strConnection) {try {objConnection. open (); canConnectSQL = true; objConnection. close ();} catch {} if (canConnectSQL) MessageBox. show ("database connection Successful! "," Crazygolf Alert "); else {MessageBox. Show (" database connection failed! "," Crazygolf Alert ");}}}}}
Note the following:
  • The Server property value is the address of the database. You can use localhost or dot (.) locally, or enter an IP address. If remote access is required, the remote access function should be activated.
  • The initial catalog attribute value is the name of the database to be connected, which is replaced by Northwind.
  • The user id and password are repeated.
  • The value of Connect Timeout is the connection Timeout. When the Open () method of the database is called, the program becomes congested. During this period, the database is continuously opened based on the connection string we wrote. If the network is different or the Server or other fields are incorrect, there is a risk of latency or even crash, so the write timeout is smaller.
Run the program and click the button. You can see:

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.