ASP. NET MVC establishes a connection to SQL Server

Source: Internet
Author: User
Tags connectionstrings

Used to connect to the database using the Entity Framework, this article is to experience using the SqlConnection connection database.


Open SQL Server 2008, create the database, and create the following table:

CREATE TABLE Product
(
    Id int Identity (+) is not null primary key,
    Name nvarchar () null,
    Quantity nvarchar () null,
    Price nvarchar (+) null
    
)
Go

Click Connect to Database under the Tools menu in Visual Studio, and select Microsoft SQL Server as the data source.

Click "Continue".

Connect to the database you just created and click "OK".

Open Server Explorer, as follows:

Right click on "Server Explorer" and click "Properties" to copy the connection string. and paste it into the connectionstrings node in Web. config.

  < connectionStrings >
    < Add name="DefaultConnection"connectionString="Data source=pc201312021114;initial CATALOG=MVC; User Id=sa; password= Password "
      providerName="System.Data.SqlClient"/>
  </ connectionStrings >

Now, you need a helper class to handle the connection, as follows:

  public class SqlDB
    {
        protected SqlConnection Conn;
        Open connection
        public bool OpenConnection ()
        {
            conn = new SqlConnection (configurationmanager.connectionstrings["DefaultConnection"]. ConnectionString);
            Try
            {
                bool result = true;
                IF (Conn. State.tostring ()! = "Open")
                {
                    Conn. Open ();
                }
                return result;
            }
            catch (SqlException ex)
            {
                return false;
            }
        }
        Close connection
        public bool CloseConnection ()
        {
            Try
            {
                Conn. Close ();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

Create TestController as follows:

   public class Testcontroller:controller
    {
        Private SqlDB _db = new SqlDB ();
        //
        GET:/test/
        Public ActionResult Index ()
        {
            BOOL R = _db. OpenConnection ();
            if (R)
            {
                return Content ("Connection succeeded");
            }
            Else
            {
                return Content ("Connection failed");
            }
        }
    }

Browse the Test/index view page to show that the connection was successful.

ASP. NET MVC establishes a connection to SQL Server

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.