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