• ASP.NET MVC与Sql Server建立连接


    用惯了使用Entity Framework连接数据库,本篇就来体验使用SqlConnection连接数据库。


    打开Sql Server 2008,创建数据库,创建如下表:

    create table Product
    
    (
    
        Id int identity(1,1) not null primary key,
    
        Name nvarchar(50) null,
    
        quantity nvarchar(50) null,
    
        Price nvarchar(50) null
    
        
    
    )
    
    go

    点击Visual Studio中"工具"菜单下的"连接到数据库",选择"Microsoft SQL Server"作为数据源。

    1

    点击"继续"。

    连接刚创建的数据库,点击"确定"。

    2

    打开"服务器资源管理器",如下:

    3

    右键"服务器资源管理器",点击"属性",复制连接字符串。并粘帖到Web.config中的connectionStrings节点下。

      <connectionStrings>
    
        <add name="DefaultConnection" connectionString="Data Source=PC201312021114;Initial Catalog=MVC;User ID=sa;Password=密码"
    
          providerName="System.Data.SqlClient" />
    
      </connectionStrings>

    现在,需要一个处理连接的帮助类,如下:

      public class SqlDB
    
        {
    
            protected SqlConnection conn;
    
            //打开连接
    
            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;
    
                }
    
            }
    
            //关闭连接
    
            public bool CloseConnection()
    
            {
    
                try
    
                {
    
                    conn.Close();
    
                    return true;
    
                }
    
                catch (Exception ex)
    
                {
    
                    return false;
    
                }
    
            }
    
        }
    

    创建TestController如下:

       public class TestController : Controller
    
        {
    
            private SqlDB _db = new SqlDB();
    
            //
    
            // GET: /Test/
    
            public ActionResult Index()
    
            {
    
                bool r = _db.OpenConnection();
    
                if (r)
    
                {
    
                    return Content("连接成功");
    
                }
    
                else
    
                {
    
                    return Content("连接失败");
    
                }
    
            }
    
        }
    

    浏览Test/Index视图页,显示连接成功。

  • 相关阅读:
    Leetcode题目:House Robber II
    Leetcode题目:Compare Version Numbers
    Leetcode题目:Intersection of Two Arrays II
    Leetcode题目:Intersection of Two Arrays
    Mac OS X 好用的软件包管理工具 Homebrew
    Linux 安装配置 JDK 8
    Centos 6.5 RedHat 6 安装mysql
    Ubuntu 源
    grub2 使用memdisk工具 启动任意iso
    Fedora 21 设置开机启动脚本
  • 原文地址:https://www.cnblogs.com/darrenji/p/4624856.html
Copyright © 2020-2023  润新知