C # Self-study Road 38

Source: Internet
Author: User

38. Connecting the database and disconnecting the database


There are two ways to connect to a database, one to use a wizard, and not to introduce it. Another is the use of code, SQL Connetion class.


Using System;

Using System.Collections.Generic;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Linq;

Using System.Text;

Using System.Threading.Tasks;

Using System.Windows.Forms;

Using System.Data.SqlClient;


Namespace WindowsFormsApplication33

{

public partial class Form1:form

{

Public Form1 ()

{

InitializeComponent ();

}


private void Form1_Load (object sender, EventArgs e)

{

Createsqlconnection ();

}


public void Createsqlconnection ()

{

SqlConnection conn = new SqlConnection ();

Conn. ConnectionString = "Data Source = (local); DataBase = master; Integrated Security = true ";


Try

{

Conn. Open ();

}

catch (SqlException e)

{

MessageBox.Show (E.tostring ());

}


This.toolStripStatusLabel1.Text = "Connection database status is:" + Conn. State.tostring ();

}

}

}



650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6B/74/wKioL1UuNeKT_AfOAAD_F1_yK6g880.jpg "title=" 9] 8tu3qufx[~y}{n@ ' V1vah.png "alt=" Wkiol1uunekt_afoaad_f1_yk6g880.jpg "/>


Disconnect the database and conn the connection. Close () method.

This article from "Very blog" blog, reproduced please contact the author!

C # Self-study Road 38

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.