码迷,mamicode.com
首页 > Web开发 > 详细

用MVC4练习,后台用aspx《MvcUserDemo》

时间:2015-11-14 16:33:43      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:

将ado.net的cs文件SqlHelper.cs放入解决方案

技术分享
  1 1 using System;
  2   2 using System.Collections.Generic;
  3   3 using System.Linq;
  4   4 using System.Text;
  5   5 using System.Configuration;
  6   6 using System.Data.SqlClient;
  7   7 using System.Data;
  8   8 
  9   9 namespace MvcUserDemo
 10  10 {
 11  11     public static class SqlHelper
 12  12     {
 13  13         public static readonly string connstr =
 14  14             ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
 15  15 
 16  16         public static SqlConnection OpenConnection()
 17  17         {
 18  18             SqlConnection conn = new SqlConnection(connstr);
 19  19             conn.Open();
 20  20             return conn;
 21  21         }
 22  22 
 23  23         public static int ExecuteNonQuery(string cmdText,
 24  24             params SqlParameter[] parameters)
 25  25         {
 26  26             using (SqlConnection conn = new SqlConnection(connstr))
 27  27             {
 28  28                 conn.Open();
 29  29                 return ExecuteNonQuery(conn, cmdText, parameters);
 30  30             }
 31  31         }
 32  32 
 33  33         public static object ExecuteScalar(string cmdText,
 34  34             params SqlParameter[] parameters)
 35  35         {
 36  36             using (SqlConnection conn = new SqlConnection(connstr))
 37  37             {
 38  38                 conn.Open();
 39  39                 return ExecuteScalar(conn, cmdText, parameters);
 40  40             }
 41  41         }
 42  42 
 43  43         public static DataTable ExecuteDataTable(string cmdText,
 44  44             params SqlParameter[] parameters)
 45  45         {
 46  46             using (SqlConnection conn = new SqlConnection(connstr))
 47  47             {
 48  48                 conn.Open();
 49  49                 return ExecuteDataTable(conn, cmdText, parameters);
 50  50             }
 51  51         }
 52  52 
 53  53         public static int ExecuteNonQuery(SqlConnection conn, string cmdText,
 54  54            params SqlParameter[] parameters)
 55  55         {
 56  56             using (SqlCommand cmd = conn.CreateCommand())
 57  57             {
 58  58                 cmd.CommandText = cmdText;
 59  59                 cmd.Parameters.AddRange(parameters);
 60  60                 return ExecuteNonQuery(conn, cmdText, parameters);
 61  61             }
 62  62         }
 63  63 
 64  64         public static object ExecuteScalar(SqlConnection conn, string cmdText,
 65  65             params SqlParameter[] parameters)
 66  66         {
 67  67             using (SqlCommand cmd = conn.CreateCommand())
 68  68             {
 69  69                 cmd.CommandText = cmdText;
 70  70                 cmd.Parameters.AddRange(parameters);
 71  71                 return cmd.ExecuteScalar();
 72  72             }
 73  73         }
 74  74 
 75  75         public static DataTable ExecuteDataTable(SqlConnection conn, string cmdText,
 76  76             params SqlParameter[] parameters)
 77  77         {
 78  78             using (SqlCommand cmd = conn.CreateCommand())
 79  79             {
 80  80                 cmd.CommandText = cmdText;
 81  81                 cmd.Parameters.AddRange(parameters);
 82  82                 using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
 83  83                 {
 84  84                     DataTable dt = new DataTable();
 85  85                     adapter.Fill(dt);
 86  86                     return dt;
 87  87                 }
 88  88             }
 89  89         }
 90  90 
 91  91         public static object ToDBValue(this object value)
 92  92         {
 93  93             return value == null ? DBNull.Value : value;
 94  94         }
 95  95 
 96  96         public static object FromDBValue(this object dbValue)
 97  97         {
 98  98             return dbValue == DBNull.Value ? null : dbValue;
 99  99         }
100 100     }
101 101 }
SqlHelper.cs

在web.config中配置连接数据库文件

技术分享
1 <connectionStrings>
2     <add name="connstr" connectionString="server=.;uid=sa;pwd=321654;database=DemoDb"/>
3   </connectionStrings>
add

创建一个控制器和页面,并获取数据库中的UserInfo表中的数据,把数据传递到前台页面进行展示

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.Mvc;
 7 
 8 namespace MvcUserDemo.Controllers
 9 {
10     public class UserInfoController : Controller
11     {
12         //
13         // GET: /UserInfo/
14 
15         public ActionResult Index()
16         {
17             //获取数据库中的UserInfo表中的数据
18             DataTable dt=SqlHelper.ExecuteDataTable("select Id, UserName, Age from dbo.UserInfo");
19 
20             //把数据传递到前台页面进行展示
21             ViewData["dt"] = dt;
22             return View();
23         }
24 
25     }
26 }
UserInfoController.cs

 前台页面展示数据库中的数据

技术分享
 1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
 2 
 3 <%@ Import Namespace="System.Data" %>
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head runat="server">
 9     <meta name="viewport" content="width=device-width" />
10     <title>Index</title>
11 </head>
12 <body>
13     <div>
14         <%
15             DataTable dt = (DataTable)ViewData["dt"]; 
16           
17         %>
18 
19         <table>
20             <tr>
21                 <th>编号</th>
22                 <th>姓名</th>
23                 <th>年龄</th>
24             </tr>
25 
26             <%foreach (DataRow dataRow in dt.Rows)
27               {%>
28 
29             <tr>
30                 <td />
31                 <%:dataRow["Id"] %><td />
32                 <td />
33                 <%:dataRow["UserName"]%><td />
34                 <td><%:dataRow["Age"]%>
35                 <td />
36             </tr>
37 
38             <%}%>
39         </table>
40     </div>
41 </body>
42 </html>
Index.aspx

 

用MVC4练习,后台用aspx《MvcUserDemo》

标签:

原文地址:http://www.cnblogs.com/mylovemygirl/p/4964505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!