zoukankan      html  css  js  c++  java
  • 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视图页,显示连接成功。

  • 相关阅读:
    bzoj 1176 cdq分治套树状数组
    Codeforces 669E cdq分治
    Codeforces 1101D 点分治
    Codeforces 1100E 拓扑排序
    Codeforces 1188D Make Equal DP
    Codeforces 1188A 构造
    Codeforces 1188B 式子转化
    Codeforces 1188C DP 鸽巢原理
    Codeforces 1179D 树形DP 斜率优化
    git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题
  • 原文地址:https://www.cnblogs.com/darrenji/p/4624856.html
Copyright © 2011-2022 走看看