C # Connection database--commonly used at present

Source: Internet
Author: User
Tags connectionstrings

Use C # to implement the user name login---Connect to the database, query and determine if there is a user name

First, the link class

1. Add DbConnection Class

2 Construction

    public class DBConnection    {        private static string _connectionstring = null;        public static SqlConnection Connection        {            get            {                //reads the link string from the configuration file                _connectionstring = configurationmanager.connectionstrings["MyDB"]. ConnectionString;                Building a Connection object using a link string                SqlConnection conn = new SqlConnection (_connectionstring);                Return conn;}}    }

(You can also define a string variable in get{} to receive the configuration)

    public class DBConnection    {public                static SqlConnection Connection        {            get            }                //Read link string from configuration file                string  _connectionstring = configurationmanager.connectionstrings["MyDB"]. ConnectionString;                Building a Connection object using a link string                SqlConnection conn = new SqlConnection (_connectionstring);                Return conn;}}    }

Second, add the configuration file

1. Reference right-click to add System.Configuration reference

2. Add New Item--Application configuration file

3.app.config

  <connectionStrings>    <add name= "mydb" connectionstring= "server=.; Database=mydb;uid=sa;pwd=123 "/>  </connectionStrings>

Note: The name name in the configuration file is the same as the field in Connectionstring["" in the DbConnection class

Third, open the close database

The 1.Main function calls the Connection property inside the DbConnection class

Class program    {        static void Main (string[] args)        {            SqlConnection conn = dbconnection.connection;            Console.WriteLine (Conn. State);            Conn. Open ();            Console.WriteLine (Conn. State);            Conn. Close ();            Console.WriteLine (Conn. State);        }    }

Execution results

C # Connection database--commonly used at present

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.