C # Connecting a SQL Server database

Source: Internet
Author: User

C # Connecting to a SQL Server database here is a brief introduction to common ways to connect to a SQL Server database.        The database to connect to is the local SQL Server, the official Northwind database.        Create a form application in VS2010, add a button that is clicked, and verify that the connection database succeeds. Write 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 = "Se            Rver=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 succeeded!                "," Crazygolf Alert "); else {MessageBox.Show ("Database connection failed!                "," Crazygolf Alert "); }            }        }    }}
Please note the following points:
    • The server property value is the address of the database, localhost or dot (.) can be used locally, or an IP address may be typed. Remote access should be enabled if remote is required.
    • The initial Catalog property value is the name of the database to which you want to connect, with Northwind instead.
    • User ID and password needless.
    • The Connect Timeout property value is the connection time-out. When the database open () method is called, the program goes into a blocking state, and the database is constantly opened according to the connection string we write, and if the network is different or the server and other fields are wrong, there is a risk of delay or even freezing, so the time-out is written smaller.
Run the program, click the button, you can see:

C # Connecting a SQL Server database

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.