C # Database related operations: [1]c# Connect SQL database

Source: Internet
Author: User
Tags visual studio 2010

for students or programmers, the general programming process, basically will use the database to save user login information or other related data, this experience is not to teach you what new knowledge, but will be a number of fragmented database knowledge into a book, convenient for people to access.

Visual Studio is a very powerful tool, especially in the C # language used more and more widely now.

Tools/Materials
    • Visual Studio 2010
    • SQL Server 2008
1. Create Test Database tests
    1. 1

      1.1 Open SQL Server Administration Tools

    2. 2

      1.2 Connecting to the database server

    3. 3

      1.3 Right-click on "Database" in the left View and select "New Database"

    4. 4

      1.4 Enter database name: Test, click OK

    5. 5

      1.5 The above database appears in the left view, indicating that the database was created successfully

      END
2. Connect to the database
  1. 2.1 Open Visual Studio 2010, create a form project, no more here, I use "CSharp Connect SQL database" as an example

  2. 2.2 Add a button named: CSharp Connect to SQL database

  3. 2.3 Double click the button, go to the Background Code Editor, add the connection code, the specific code text is as follows:

    private void Buttonconnsql_click (object sender, RoutedEventArgs e)

    {

    String strconnection = "server=localhost;";

    Strconnection + = "initial catalog=test;";

    Strconnection + = "user id=**;";

    Strconnection + = "password=******;";

    Strconnection + = "Connect timeout=5";

    BOOL Canconnectdb = false;

    using (SqlConnection objconnection = new SqlConnection (strconnection))

    {

    Try

    {

    Objconnection.open ();

    Canconnectdb = true;

    Objconnection.close ();

    }

    Catch {}

    if (CANCONNECTDB) MessageBox.Show ("Database connection succeeded! "," Baidu Experience Tip: ");

    else MessageBox.Show ("Database connection failed! "," Baidu Experience Tip: ");

    }

    }

    Say the meaning of the field in the connection string:

    ①server: The address of the database, if the local connection, with localhost, to use IP address access, the database needs to open the remote access function, as to how to set up, please Baidu, there are many predecessors.

    ②initial Catalog: Database name, complete the database test created in step one

    ③user ID and password: username and password, in this internet age, this represents what, believe not I said, if you do not know, then you are out!

    ④connect timeout: The connection time-out, call the database open () method, the program enters the blocking state, the period according to our written connection string to constantly open the database, if the network is not connected or server and other field errors, there will be delay or even the risk of death, so the time-out to write a smaller point.

  4. 2.4 Run the program, click the button, pop up the Connection Success dialog box

C # Database related operations: [1]c# Connect to SQL database (GO)

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.