﻿{"id":1635,"date":"2011-06-28T12:47:11","date_gmt":"2011-06-28T04:47:11","guid":{"rendered":"http:\/\/blog.jixin.ntxz.net\/?p=1635"},"modified":"2011-06-28T12:47:11","modified_gmt":"2011-06-28T04:47:11","slug":"asp-net%e4%b8%89%e5%b1%82%e6%9e%b6%e6%9e%84%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=1635","title":{"rendered":"asp.net\u4e09\u5c42\u67b6\u6784\u8be6\u89e3"},"content":{"rendered":"<p>\u4e00\u3001\u6570\u636e\u5e93<\/p>\n<p>\/*==============================================================*\/<\/p>\n<p>\/* DBMS name:      Microsoft SQL Server 2000                    *\/<\/p>\n<p>\/*==============================================================*\/<\/p>\n<p>if exists (select 1<\/p>\n<p>            from sysobjects<\/p>\n<p>           where id = object_id(&#8216;newsContent&#8217;)<\/p>\n<p>            and   type = &#8216;U&#8217;)<\/p>\n<p>   drop table newsContent<\/p>\n<p>go<\/p>\n<p>\/*==============================================================*\/<\/p>\n<p>\/* Table: newsContent                                            *\/<\/p>\n<p>\/*==============================================================*\/<\/p>\n<p>create table newsContent (<\/p>\n<p>   ID           int              identity(1,1)   primary key,<\/p>\n<p>   Title          nvarchar(50)     not null,<\/p>\n<p>   Content       ntext            not null,<\/p>\n<p>   AddDate      datetime         not null,<\/p>\n<p>  CategoryID    int              not null<\/p>\n<p>)<\/p>\n<p>go<\/p>\n<p>\u4e8c\u3001\u9879\u76ee\u6587\u4ef6\u67b6\u6784<\/p>\n<p>\u5b9e\u73b0\u6b65\u9aa4\u4e3a\uff1a4-3-6-5-2-1<\/p>\n<p>ID<\/p>\n<p>\u9879\u76ee<\/p>\n<p>\u63cf\u8ff0<\/p>\n<p>\u7528\u9014<\/p>\n<p>\u9879\u76ee\u5f15\u7528\u5173\u7cfb<\/p>\n<p>\u5b9e\u4f8b\u6240\u9700\u6587\u4ef6<\/p>\n<p>\u76f8\u5173\u65b9\u6cd5<\/p>\n<p>1<\/p>\n<p>Web<\/p>\n<p>\u8868\u73b0\u5c42<\/p>\n<p>Web\u9875\u548c\u63a7\u4ef6<\/p>\n<p>\u5f15\u7528BLL<\/p>\n<p>WebUI.aspx<\/p>\n<p>WebUI.aspx.cs<\/p>\n<p>GetContent()<\/p>\n<p>2<\/p>\n<p>BLL<\/p>\n<p>\u4e1a\u52a1\u903b\u8f91\u5c42<\/p>\n<p>\u4e1a\u52a1\u903b\u8f91\u7ec4\u4ef6<\/p>\n<p>\u5f15\u7528 IDAL\uff0cModel\uff0c\u4f7f\u7528DALFactory\u521b\u5efa\u5b9e\u4f8b<\/p>\n<p>Content.cs<\/p>\n<p>ContentInfo GetContentInfo(int id)<\/p>\n<p>3<\/p>\n<p>IDAL<\/p>\n<p>\u6570\u636e\u8bbf\u95ee\u5c42\u63a5\u53e3\u5b9a\u4e49<\/p>\n<p>\u6bcf\u4e2aDAL\u5b9e\u73b0\u90fd\u8981\u5b9e\u73b0\u7684\u4e00\u7ec4\u63a5\u53e3<\/p>\n<p>\u5f15\u7528 Model<\/p>\n<p>IContent.cs<\/p>\n<p>ContentInfo GetContentInfo(int id)<\/p>\n<p>4<\/p>\n<p>Model<\/p>\n<p>\u4e1a\u52a1\u5b9e\u4f53<\/p>\n<p>\u4f20\u9012\u5404\u79cd\u6570\u636e\u7684\u5bb9\u5668<\/p>\n<p>\u65e0\u5f15\u7528<\/p>\n<p>ContentInfo.cs<\/p>\n<p>5<\/p>\n<p>DALFactory<\/p>\n<p>\u6570\u636e\u5c42\u7684\u62bd\u8c61\u5de5\u5382<\/p>\n<p>\u521b\u5efa\u53cd\u5c04\uff0c\u7528\u6765\u786e\u5b9a\u52a0\u8f7d\u54ea\u4e00\u4e2a\u6570\u636e\u5e93\u8bbf\u95ee\u7a0b\u5e8f\u96c6\u7684\u7c7b<\/p>\n<p>\u5f15\u7528IDAL\uff0c\u901a\u8fc7\u8bfb\u53d6web.config\u91cc\u8bbe\u7f6e\u7684\u7a0b\u5e8f\u96c6\uff0c\u52a0\u8f7d\u7c7b\u7684\u5b9e\u4f8b\uff0c\u8fd4\u56de\u7ed9BLL\u4f7f\u7528\u3002<\/p>\n<p>Content.cs<\/p>\n<p>IDAL.Icontent create()<\/p>\n<p>6<\/p>\n<p>SQLServerDAL<\/p>\n<p>SQLServer\u6570\u636e\u8bbf\u95ee\u5c42<\/p>\n<p>Microsoft SQL Server\u7279\u5b9a\u7684Pet Shop DAL\u5b9e\u73b0\uff0c\u4f7f\u7528\u4e86IDAL\u63a5\u53e3<\/p>\n<p>\u5f15\u7528 Model\u548cIDAL\uff0c\u88abDALFactory\u52a0\u8f7d\u7684\u7a0b\u5e8f\u96c6\uff0c\u5b9e\u73b0\u63a5\u53e3\u91cc\u7684\u65b9\u6cd5\u3002<\/p>\n<p>SqlHelper.cs<\/p>\n<p>Content.cs<\/p>\n<p>SqlDataReader ExecuteReader()<\/p>\n<p>PrepareCommand()<\/p>\n<p>ContentInfo GetContentInfo(int id)<\/p>\n<p>OracleDAL<\/p>\n<p>Oracle\u6570\u636e\u8bbf\u95ee\u5c42<\/p>\n<p>7<\/p>\n<p>DBUtility<\/p>\n<p>\u6570\u636e\u5e93\u8bbf\u95ee\u7ec4\u4ef6\u57fa\u7840\u7c7b<\/p>\n<p>GetSqlServerConnectionString\u5f97\u5230\u6570\u636e\u5e93\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u7701\u53bb\u8be5\u9879\u76ee\uff0c\u5728SQLServerDAL.SqlHelper\u4e2d\u7528static readonly string SqlConnectionString\u4ee3\u66ff\u3002<\/p>\n<p>\u65e0\u5f15\u7528<\/p>\n<p>\u5b9e\u73b0\u6b65\u9aa4\u8fc7\u7a0b<\/p>\n<p>1\u3001\u521b\u5efaModel\uff0c\u5b9e\u73b0\u4e1a\u52a1\u5b9e\u4f53\u3002<\/p>\n<p>2\u3001\u521b\u5efaIDAL\uff0c\u5b9e\u73b0\u63a5\u53e3\u3002<\/p>\n<p>3\u3001\u521b\u5efaSQLServerDAL\uff0c\u5b9e\u73b0\u63a5\u53e3\u91cc\u7684\u65b9\u6cd5\u3002<\/p>\n<p>4\u3001\u589e\u52a0web.config\u91cc\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u4e3aSQLServerDAL\u7684\u7a0b\u5e8f\u96c6\u3002<\/p>\n<p>5\u3001\u521b\u5efaDALFactory\uff0c\u8fd4\u56de\u7a0b\u5e8f\u96c6\u7684\u6307\u5b9a\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<p>6\u3001\u521b\u5efaBLL\uff0c\u8c03\u7528DALFactory\uff0c\u5f97\u5230\u7a0b\u5e8f\u96c6\u6307\u5b9a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5b8c\u6210\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<p>7\u3001\u521b\u5efaWEB\uff0c\u8c03\u7528BLL\u91cc\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>1\u3001web.config\u91cc\u7684\u7a0b\u5e8f\u96c6\u540d\u79f0\u5fc5\u987b\u4e0eSQLServerDAL\u91cc\u7684\u8f93\u51fa\u7a0b\u5e8f\u96c6\u540d\u79f0\u4e00\u81f4\u3002<\/p>\n<p>2\u3001DALFactory\u91cc\u53ea\u9700\u8981\u4e00\u4e2aDataAccess\u7c7b\uff0c\u53ef\u4ee5\u5b8c\u6210\u521b\u5efa\u6240\u6709\u7684\u7a0b\u5e8f\u96c6\u5b9e\u4f8b\u3002<\/p>\n<p>3\u3001\u9879\u76ee\u521b\u5efa\u540e\uff0c\u6ce8\u610f\u4fee\u6539\u5404\u9879\u76ee\u7684\u9ed8\u8ba4\u547d\u540d\u7a7a\u95f4\u548c\u7a0b\u5e8f\u96c6\u540d\u79f0\u3002<\/p>\n<p>4\u3001\u6ce8\u610f\u4fee\u6539\u89e3\u51b3\u65b9\u6848\u91cc\u7684\u9879\u76ee\u4f9d\u8d56\u3002<\/p>\n<p>5\u3001\u6ce8\u610f\u5728\u89e3\u51b3\u65b9\u6848\u91cc\u589e\u52a0\u5404\u9879\u76ee\u5f15\u7528\u3002<\/p>\n<p>\u4e09\u3001\u5404\u5c42\u95f4\u7684\u8bbf\u95ee\u8fc7\u7a0b<\/p>\n<p>1\u3001\u4f20\u5165\u503c\uff0c\u5c06\u503c\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362(\u4e3a\u6574\u578b)\u3002<\/p>\n<p>2\u3001\u521b\u5efaBLL\u5c42\u7684content.cs\u5bf9\u8c61c\uff0c\u901a\u8fc7\u5bf9\u8c61c\u8bbf\u95eeBLL\u5c42\u7684\u65b9\u6cd5GetContentInfo(ID)\u8c03\u7528BLL\u5c42\u3002<\/p>\n<p>3\u3001BLL\u5c42\u65b9\u6cd5GetContentInfo(ID)\u4e2d\u53d6\u5f97\u6570\u636e\u8bbf\u95ee\u5c42SQLServerDAL\u7684\u5b9e\u4f8b,\u5b9e\u4f8b\u5316IDAL\u5c42\u7684\u63a5\u53e3\u5bf9\u8c61dal\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u662f\u7531\u5de5\u5382\u5c42DALFactory\u521b\u5efa\u7684\uff0c\u7136\u540e\u8fd4\u56deIDAL\u5c42\u4f20\u5165\u503c\u6240\u67e5\u627e\u7684\u5185\u5bb9\u7684\u65b9\u6cd5dal.GetContentInfo(id)\u3002<\/p>\n<p>4\u3001\u6570\u636e\u5de5\u5382\u901a\u8fc7web.config\u914d\u7f6e\u6587\u4ef6\u4e2d\u7ed9\u5b9a\u7684webdal\u5b57\u4e32\u8bbf\u95eeSQLServerDAL\u5c42\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b8c\u6574\u7684\u8c03\u7528SQLServerDAL\u5c42\u7684\u8def\u5f84\u7ed9 BLL\u5c42\u3002<\/p>\n<p>5\u3001\u5230\u6b64\u8981\u8c03\u7528SQLServerDAL\u5c42\uff0cSQLServerDAL\u5c42\u5b8c\u6210\u8d4b\u503cModel\u5c42\u7684\u5bf9\u8c61\u503c\u4e3a\u7a7a\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u53c2\u6570\uff0c\u8c03\u7528SQLServerDAL\u5c42\u7684SqlHelper\u7684ExecuteReader\u65b9\u6cd5,\u8bfb\u51fa\u6bcf\u4e2a\u5b57\u6bb5\u7684\u6570\u636e\u8d4b\u503c\u7ed9\u4ee5\u5b9a\u4e49\u4e3a\u7a7a\u7684Model\u5c42\u7684\u5bf9\u8c61\u3002<\/p>\n<p>6\u3001SqlHelper\u6267\u884csql\u547d\u4ee4\uff0c\u8fd4\u56de\u4e00\u4e2a\u6307\u5b9a\u8fde\u63a5\u7684\u6570\u636e\u5e93\u8bb0\u5f55\u96c6\uff0c\u5728\u8fd9\u91cc\u9700\u8981\u5f15\u7528\u53c2\u6570\u7c7b\u578b\uff0c\u63d0\u4f9b\u4e3a\u6253\u5f00\u8fde\u63a5\u547d\u4ee4\u6267\u884c\u505a\u597d\u51c6\u5907PrepareCommand\u3002<\/p>\n<p>7\u3001\u8fd4\u56deModel\u5c42\u628a\u67e5\u8be2\u5f97\u5230\u7684\u4e00\u884c\u8bb0\u5f55\u503c\u8d4b\u503c\u7ed9SQLServerDAL\u5c42\u7684\u5f15\u5165\u7684Model\u5c42\u7684\u5bf9\u8c61ci\uff0c\u7136\u540e\u628a\u8fd9\u4e2a\u5bf9\u8c61\u8fd4\u56de\u7ed9BLL\u3002<\/p>\n<p>8\u3001\u56de\u5230Web\u5c42\u7684BLL\u5c42\u7684\u65b9\u6cd5\u8c03\u7528,\u628a\u5f97\u5230\u7684\u5bf9\u8c61\u503c\u8d4b\u503c\u7ed9Lable\u6807\u7b7e\uff0c\u5728\u524d\u53f0\u663e\u793a\u7ed9\u754c\u9762<\/p>\n<p>\u56db\u3001\u9879\u76ee\u4e2d\u7684\u6587\u4ef6\u6e05\u5355<\/p>\n<p>1\u3001DBUtility\u9879\u76ee<\/p>\n<p>(1)connectionInfo.cs<\/p>\n<p>using System;<\/p>\n<p>using System.Configuration;<\/p>\n<p>namespace Utility<\/p>\n<p>{<\/p>\n<p>       \/\/\/ <\/p>\n<summary>\n<p>       \/\/\/ ConnectionInfo \u7684\u6458\u8981\u8bf4\u660e\u3002<\/p>\n<p>       \/\/\/ <\/summary>\n<p>       public class ConnectionInfo<\/p>\n<p>       {<\/p>\n<p>              public static string GetSqlServerConnectionString()<\/p>\n<p>              {<\/p>\n<p>                     return ConfigurationSettings.AppSettings[&#8220;SQLConnString&#8221;];<\/p>\n<p>              }<\/p>\n<p>       }<\/p>\n<p>}<\/p>\n<p>2\u3001SQLServerDAL\u9879\u76ee<\/p>\n<p>\uff081\uff09SqlHelper.cs\u62bd\u8c61\u7c7b<\/p>\n<p>using System;<\/p>\n<p>using System.Data;<\/p>\n<p>using System.Data.SqlClient;<\/p>\n<p>using DBUtility;<\/p>\n<p>namespace SQLServerDAL<\/p>\n<p>{<\/p>\n<p>       \/\/\/ <\/p>\n<summary>\n<p>       \/\/\/ SqlHelper \u7684\u6458\u8981\u8bf4\u660e\u3002<\/p>\n<p>       \/\/\/ <\/summary>\n<p>       public abstract class SqlHelper<\/p>\n<p>       {<\/p>\n<p>              public static readonly string CONN_STR = ConnectionInfo.GetSqlServerConnectionString();<\/p>\n<p>              \/\/\/ <\/p>\n<summary>\n<p>              \/\/\/ \u7528\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u6267\u884cSQL\u547d\u4ee4\uff0c\u8fd4\u56de\u4e00\u4e2a\u4ece\u6307\u5b9a\u8fde\u63a5\u7684\u6570\u636e\u5e93\u8bb0\u5f55\u96c6<\/p>\n<p>              \/\/\/ <\/summary>\n<p>              \/\/\/ <remarks><\/p>\n<p>              \/\/\/ \u4f8b\u5982\uff1a<\/p>\n<p>              \/\/\/ SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, &#8220;PublishOrders&#8221;, new SqlParameter(&#8220;@prodid&#8221;, 24));<\/p>\n<p>              \/\/\/ <\/remarks><\/p>\n<p>              \/\/\/ <param name=\"connectionString\">SqlConnection\u6709\u6548\u7684SQL\u8fde\u63a5\u5b57\u7b26\u4e32<\/param>\n<p>              \/\/\/ <param name=\"commandType\">CommandType\uff1aCommandType.Text\u3001CommandType.StoredProcedure<\/param>\n<p>              \/\/\/ <param name=\"commandText\">SQL\u8bed\u53e5\u6216\u5b58\u50a8\u8fc7\u7a0b<\/param>\n<p>              \/\/\/ <param name=\"commandParameters\">SqlParameter[]\u53c2\u6570\u6570\u7ec4<\/param>\n<p>              \/\/\/ <returns>SqlDataReader\uff1a\u6267\u884c\u7ed3\u679c\u7684\u8bb0\u5f55\u96c6<\/returns><\/p>\n<p>              public static SqlDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)<\/p>\n<p>              {<\/p>\n<p>                     SqlCommand cmd = new SqlCommand();<\/p>\n<p>                     SqlConnection conn = new SqlConnection(connString);<\/p>\n<p>                     \/\/ \u6211\u4eec\u5728\u8fd9\u91cc\u7528 try\/catch \u662f\u56e0\u4e3a\u5982\u679c\u8fd9\u4e2a\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\uff0c\u6211\u4eec\u76ee\u7684\u662f\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u518d\u629b\u51fa\u5f02\u5e38\uff0c<\/p>\n<p>                     \/\/ \u56e0\u4e3a\u8fd9\u65f6\u4e0d\u4f1a\u6709DataReader\u5b58\u5728\uff0c\u6b64\u540ecommandBehaviour.CloseConnection\u5c06\u4e0d\u4f1a\u5de5\u4f5c\u3002<\/p>\n<p>                     try<\/p>\n<p>                     {<\/p>\n<p>                            PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);<\/p>\n<p>                            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);<\/p>\n<p>                            cmd.Parameters.Clear();<\/p>\n<p>                            return rdr;<\/p>\n<p>                     }<\/p>\n<p>                     catch<\/p>\n<p>                     {<\/p>\n<p>                            conn.Close();<\/p>\n<p>                            throw;<\/p>\n<p>                     }<\/p>\n<p>              }<\/p>\n<p>              \/\/\/ <\/p>\n<summary>\n<p>              \/\/\/ \u4e3a\u6267\u884c\u547d\u4ee4\u505a\u597d\u51c6\u5907\uff1a\u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u547d\u4ee4\u8bed\u53e5\uff0c\u8bbe\u7f6e\u547d\u4ee4\u7c7b\u578b\uff08SQL\u8bed\u53e5\u6216\u5b58\u50a8\u8fc7\u7a0b\uff09\uff0c\u51fd\u6570\u8bed\u53d6\u3002<\/p>\n<p>              \/\/\/ <\/summary>\n<p>              \/\/\/ <param name=\"cmd\">SqlCommand \u7ec4\u4ef6<\/param>\n<p>              \/\/\/ <param name=\"conn\">SqlConnection \u7ec4\u4ef6<\/param>\n<p>              \/\/\/ <param name=\"trans\">SqlTransaction \u7ec4\u4ef6\uff0c\u53ef\u4ee5\u4e3anull<\/param>\n<p>              \/\/\/ <param name=\"cmdType\">\u8bed\u53e5\u7c7b\u578b\uff1aCommandType.Text\u3001CommandType.StoredProcedure<\/param>\n<p>              \/\/\/ <param name=\"cmdText\">SQL\u8bed\u53e5\uff0c\u53ef\u4ee5\u4e3a\u5b58\u50a8\u8fc7\u7a0b<\/param>\n<p>              \/\/\/ <param name=\"cmdParms\">SQL\u53c2\u6570\u6570\u7ec4<\/param>\n<p>              private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)<\/p>\n<p>              {<\/p>\n<p>                     if (conn.State != ConnectionState.Open)<\/p>\n<p>                            conn.Open();<\/p>\n<p>                     cmd.Connection = conn;<\/p>\n<p>                     cmd.CommandText = cmdText;<\/p>\n<p>                     if (trans != null)<\/p>\n<p>                            cmd.Transaction = trans;<\/p>\n<p>                     cmd.CommandType = cmdType;<\/p>\n<p>                     if (cmdParms != null)<\/p>\n<p>                     {<\/p>\n<p>                            foreach (SqlParameter parm in cmdParms)<\/p>\n<p>                                   cmd.Parameters.Add(parm);<\/p>\n<p>                     }<\/p>\n<p>              }<\/p>\n<p>       }<\/p>\n<p>}<\/p>\n<p>\uff082\uff09Content.cs\u7c7b<\/p>\n<p>using System;<\/p>\n<p>using System.Data;<\/p>\n<p>using System.Data.SqlClient;<\/p>\n<p>using Model;<\/p>\n<p>using IDAL;<\/p>\n<p>namespace SQLServerDAL<\/p>\n<p>{<\/p>\n<p>       \/\/\/ <\/p>\n<summary>\n<p>       \/\/\/ Content \u7684\u6458\u8981\u8bf4\u660e\u3002<\/p>\n<p>       \/\/\/ <\/summary>\n<p>       public class Content:IContent <\/p>\n<p>       {<\/p>\n<p>              private const string PARM_ID = &#8220;@ID&#8221;;<\/p>\n<p>              private const string SQL_SELECT_CONTENT = &#8220;Select ID, Title, Content, AddDate, CategoryID From newsContent Where ID = @ID&#8221;;<\/p>\n<p>              public ContentInfo GetContentInfo(int id)<\/p>\n<p>              {<\/p>\n<p>                     \/\/\u521b\u610f\u6587\u7ae0\u5185\u5bb9\u7c7b<\/p>\n<p>                     ContentInfo ci = null;<\/p>\n<p>                     \/\/\u521b\u5efa\u4e00\u4e2a\u53c2\u6570<\/p>\n<p>                     SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.BigInt, 8);<\/p>\n<p>                     \/\/\u8d4b\u4e0aID\u503c<\/p>\n<p>                     parm.Value = id;<\/p>\n<p>                     using(SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.CONN_STR, CommandType.Text, SQL_SELECT_CONTENT, parm))<\/p>\n<p>                     {<\/p>\n<p>                            if(sdr.Read())<\/p>\n<p>                            { <\/p>\n<p>                                   ci = new ContentInfo(sdr.GetInt32(0),sdr.GetString(1), sdr.GetString(2),<\/p>\n<p>                                          sdr.GetDateTime(3), sdr.GetInt32(4), sdr.GetInt32(5), sdr.GetString(6));<\/p>\n<p>                            }<\/p>\n<p>                     }<\/p>\n<p>                     return ci;<\/p>\n<p>              }<\/p>\n<p>       }<\/p>\n<p>}<\/p>\n<p>3\u3001Model\u9879\u76ee<\/p>\n<p>\uff081\uff09contentInfo.cs<\/p>\n<p>using System;<\/p>\n<p>namespace Model<\/p>\n<p>{<\/p>\n<p>       \/\/\/ <\/p>\n<summary>\n<p>       \/\/\/ Class1 \u7684\u6458\u8981\u8bf4\u660e\u3002<\/p>\n<p>       \/\/\/ <\/summary>\n<p>       public class ContentInfo<\/p>\n<p>       {<\/p>\n<p>              private int _ID;<\/p>\n<p>              private string _Content;<\/p>\n<p>              private string _Title;<\/p>\n<p>              private string _From;<\/p>\n<p>              private DateTime _AddDate;<\/p>\n<p>              private int _clsID;<\/p>\n<p>              private int _tmpID;<\/p>\n<p>              \/\/\/ <\/p>\n<summary>\n<p>              \/\/\/ \u6587\u7ae0\u5185\u5bb9\u6784\u9020\u51fd\u6570<\/p>\n<p>              \/\/\/ <\/summary>\n<p>              \/\/\/ <param name=\"id\">\u6587\u7ae0\u6d41\u6c34\u53f7ID<\/param>\n<p>              \/\/\/ <param name=\"content\">\u6587\u7ae0\u5185\u5bb9<\/param>\n<p>              \/\/\/ <param name=\"title\">\u6587\u7ae0\u6807\u9898<\/param>\n<p>              \/\/\/ <param name=\"from\">\u6587\u7ae0\u6765\u6e90<\/param>\n<p>              \/\/\/ <param name=\"clsid\">\u6587\u7ae0\u7684\u5206\u7c7b\u5c5e\u6027ID<\/param>\n<p>              \/\/\/ <param name=\"tmpid\">\u6587\u7ae0\u7684\u6a21\u677f\u5c5e\u6027ID<\/param>\n<p>              public ContentInfo(int id,string title,string content,string from,DateTime addDate,int clsid,int tmpid )<\/p>\n<p>              {<\/p>\n<p>                     this._ID = id;<\/p>\n<p>                     this._Content = content;<\/p>\n<p>                     this._Title = title;<\/p>\n<p>                     this._From = from;<\/p>\n<p>                     this._AddDate = addDate;<\/p>\n<p>                     this._clsID = clsid;<\/p>\n<p>                     this._tmpID = tmpid;<\/p>\n<p>              }<\/p>\n<p>              \/\/\u5c5e\u6027<\/p>\n<p>              public int ID<\/p>\n<p>              {<\/p>\n<p>                     get   { return _ID; }<\/p>\n<p>              }<\/p>\n<p>              public string Content<\/p>\n<p>              {<\/p>\n<p>                     get   { return _Content; }<\/p>\n<p>              }<\/p>\n<p>              public string Title<\/p>\n<p>              {<\/p>\n<p>                     get   { return _Title; }<\/p>\n<p>              }<\/p>\n<p>              public string From<\/p>\n<p>              {<\/p>\n<p>                     get   { return _From; }<\/p>\n<p>              }<\/p>\n<p>              public DateTime AddDate<\/p>\n<p>              {<\/p>\n<p>                     get   { return _AddDate; }<\/p>\n<p>              }<\/p>\n<p>              public int ClsID<\/p>\n<p>              {<\/p>\n<p>                     get   { return _clsID; }<\/p>\n<p>              }<\/p>\n<p>              public int TmpID<\/p>\n<p>              {<\/p>\n<p>                     get   { return _tmpID; }<\/p>\n<p>              }<\/p>\n<p>       }<\/p>\n<p>}<\/p>\n<p>4\u3001IDAL\u9879\u76ee<\/p>\n<p>\uff081\uff09Icontent.cs<\/p>\n<p>using System;<\/p>\n<p>using Model;<\/p>\n<p>namespace IDAL<\/p>\n<p>{<\/p>\n<p>       \/\/\/ <\/p>\n<summary>\n<p>       \/\/\/ \u6587\u7ae0\u5185\u5bb9\u64cd\u4f5c\u63a5\u53e3<\/p>\n<p>       \/\/\/ <\/summary>\n<p>       public interface IContent<\/p>\n<p>       {<\/p>\n<p>              \/\/\/ <\/p>\n<summary>\n<p>              \/\/\/ \u53d6\u5f97\u6587\u7ae0\u7684\u5185\u5bb9\u3002<\/p>\n<p>              \/\/\/ <\/summary>\n<p>              \/\/\/ <param name=\"id\">\u6587\u7ae0\u7684ID<\/param>\n<p>              \/\/\/ <returns><\/returns><\/p>\n<p>              ContentInfo GetContentInfo(int id);<\/p>\n<p>       }<\/p>\n<p>}<\/p>\n<p>5\u3001DALFactory\u9879\u76ee<\/p>\n<p>\uff081\uff09Content.cs<\/p>\n<p>using System;<\/p>\n<p>using System.Reflection;<\/p>\n<p>using System.Configuration;<\/p>\n<p>using IDAL;<\/p>\n<p>namespace DALFactory<\/p>\n<p>{<\/p>\n<p>       \/\/\/ <\/p>\n<summary>\n<p>       \/\/\/ \u5de5\u4ea7\u6a21\u5f0f\u5b9e\u73b0\u6587\u7ae0\u63a5\u53e3\u3002<\/p>\n<p>       \/\/\/ <\/summary>\n<p>       public class Content<\/p>\n<p>       {<\/p>\n<p>              public static IDAL.IContent Create()<\/p>\n<p>              {<\/p>\n<p>                     \/\/ \u8fd9\u91cc\u53ef\u4ee5\u67e5\u770b DAL \u63a5\u53e3\u7c7b\u3002<\/p>\n<p>                     string path = System.Configuration.ConfigurationSettings.AppSettings[&#8220;WebDAL&#8221;].ToString();<\/p>\n<p>                     string className = path+&#8221;.Content&#8221;;<\/p>\n<p>                     \/\/ \u7528\u914d\u7f6e\u6587\u4ef6\u6307\u5b9a\u7684\u7c7b\u7ec4\u5408<\/p>\n<p>                     return (IDAL.IContent)Assembly.Load(path).CreateInstance(className);<\/p>\n<p>              }<\/p>\n<p>       }<\/p>\n<p>}<\/p>\n<p>6\u3001BLL\u9879\u76ee<\/p>\n<p>\uff081\uff09Content.cs<\/p>\n<p>using System;<\/p>\n<p>using Model;<\/p>\n<p>using IDAL;<\/p>\n<p>namespace BLL<\/p>\n<p>{<\/p>\n<p>       \/\/\/ <\/p>\n<summary>\n<p>       \/\/\/ Content \u7684\u6458\u8981\u8bf4\u660e\u3002<\/p>\n<p>       \/\/\/ <\/summary>\n<p>       public class Content<\/p>\n<p>       {<\/p>\n<p>              public ContentInfo GetContentInfo(int id)<\/p>\n<p>              {<\/p>\n<p>                     \/\/ \u53d6\u5f97\u4ece\u6570\u636e\u8bbf\u95ee\u5c42\u53d6\u5f97\u4e00\u4e2a\u6587\u7ae0\u5185\u5bb9\u5b9e\u4f8b<\/p>\n<p>                     IContent dal = DALFactory.Content.Create();<\/p>\n<p>                     \/\/ \u7528DAL\u67e5\u627e\u6587\u7ae0\u5185\u5bb9<\/p>\n<p>                     return dal.GetContentInfo(id);<\/p>\n<p>              }<\/p>\n<p>       }<\/p>\n<p>}<\/p>\n<p>7\u3001Web\u9879\u76ee<\/p>\n<p>1\u3001Web.config\uff1a<\/p>\n<p> <appSettings><\/p>\n<p>       <add key=\"SQLConnString\" value=\"Data Source=localhost;Persist Security info=True;Initial Catalog=newsDB;User ID=sa;Password= \" \/><\/p>\n<p>    <add key=\"WebDAL\" value=\"SQLServerDAL\" \/>   <\/p>\n<p> <\/appSettings><\/p>\n<p>2\u3001WebUI.aspx<\/p>\n<p><%@ Page language=\"c#\" Codebehind=\"WebUI.aspx.cs\" AutoEventWireup=\"false\" Inherits=\"Web.WebUI\" %><\/p>\n<p><!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" ><\/p>\n<p><HTML><\/p>\n<p>       <HEAD><\/p>\n<p>              <title>WebUI<\/title><\/p>\n<p>              <meta name=\"GENERATOR\" Content=\"Microsoft Visual Studio .NET 7.1\"><\/p>\n<p>              <meta name=\"CODE_LANGUAGE\" Content=\"C#\"><\/p>\n<p>              <meta name=\"vs_defaultClientScript\" content=\"JavaScript\"><\/p>\n<p>              <meta name=\"vs_targetSchema\" content=\"http:\/\/schemas.microsoft.com\/intellisense\/ie5\"><\/p>\n<p>       <\/HEAD><\/p>\n<p>       <body MS_POSITIONING=\"GridLayout\"><\/p>\n<form id=\"Form1\" method=\"post\" runat=\"server\">\n<p>                     <FONT\">\u5b8b\u4f53&#8221;><\/FONT><\/p>\n<table width=\"600\" border=\"1\">\n<tr>\n<td style=\"WIDTH: 173px\">&nbsp;<\/td>\n<td>&nbsp;<\/p>\n<p>                                          <asp:Label id=\"lblTitle\" runat=\"server\"><\/asp:Label><\/td>\n<\/tr>\n<tr>\n<td style=\"WIDTH: 173px; HEIGHT: 22px\">&nbsp;<\/td>\n<td style=\"HEIGHT: 22px\">&nbsp;<\/p>\n<p>                                          <asp:Label id=\"lblDataTime\" runat=\"server\"><\/asp:Label><\/td>\n<\/tr>\n<tr>\n<td style=\"WIDTH: 173px\">&nbsp;<\/td>\n<td>&nbsp;<\/p>\n<p>                                          <asp:Label id=\"lblContent\" runat=\"server\"><\/asp:Label><\/td>\n<\/tr>\n<tr>\n<td style=\"WIDTH: 173px\">&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td style=\"WIDTH: 173px; HEIGHT: 23px\">&nbsp;<\/td>\n<td style=\"HEIGHT: 23px\">&nbsp;<\/td>\n<\/tr>\n<tr>\n<td style=\"WIDTH: 173px\">&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td style=\"WIDTH: 173px\">&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td style=\"WIDTH: 173px\">&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td style=\"WIDTH: 173px\">&nbsp;<\/td>\n<td>&nbsp;<\/p>\n<p>                                          <asp:Label id=\"lblMsg\" runat=\"server\">Label<\/asp:Label><\/td>\n<\/tr>\n<\/table><\/form>\n<p>       <\/body><\/p>\n<p><\/HTML><\/p>\n<p>3\u3001WebUI.aspx.cs\u540e\u53f0\u8c03\u7528\u663e\u793a\uff1a<\/p>\n<p>using System;<\/p>\n<p>using System.Collections;<\/p>\n<p>using System.ComponentModel;<\/p>\n<p>using System.Data;<\/p>\n<p>using System.Drawing;<\/p>\n<p>using System.Web;<\/p>\n<p>using System.Web.SessionState;<\/p>\n<p>using System.Web.UI;<\/p>\n<p>using System.Web.UI.WebControls;<\/p>\n<p>using System.Web.UI.HtmlControls;<\/p>\n<p>using BLL;<\/p>\n<p>using Model;<\/p>\n<p>namespace myWeb<\/p>\n<p>{<\/p>\n<p>       \/\/\/ <\/p>\n<summary>\n<p>       \/\/\/ WebForm1 \u7684\u6458\u8981\u8bf4\u660e\u3002<\/p>\n<p>       \/\/\/ <\/summary>\n<p>       public class WebUI : System.Web.UI.Page<\/p>\n<p>       {<\/p>\n<p>              protected System.Web.UI.WebControls.Label lblTitle;<\/p>\n<p>              protected System.Web.UI.WebControls.Label lblDataTime;<\/p>\n<p>              protected System.Web.UI.WebControls.Label lblContent;<\/p>\n<p>              protected System.Web.UI.WebControls.Label lblMsg;<\/p>\n<p>              private ContentInfo ci ;<\/p>\n<p>              private void Page_Load(object sender, System.EventArgs e)<\/p>\n<p>              {<\/p>\n<p>                     if(!Page.IsPostBack)<\/p>\n<p>                     {<\/p>\n<p>                            GetContent(&#8220;1&#8221;);<\/p>\n<p>                     }<\/p>\n<p>              }<\/p>\n<p>              private void GetContent(string id)<\/p>\n<p>              {<\/p>\n<p>                     int ID = WebComponents.CleanString.GetInt(id);<\/p>\n<p>                     Content c = new Content();<\/p>\n<p>                     ci = c.GetContentInfo(ID);<\/p>\n<p>                     if(ci!=null)<\/p>\n<p>                     {<\/p>\n<p>                            this.lblTitle.Text = ci.Title;<\/p>\n<p>                            this.lblDataTime.Text = ci.AddDate.ToString(&#8220;yyyy-MM-dd&#8221;);<\/p>\n<p>                            this.lblContent.Text = ci.Content;<\/p>\n<p>                     }<\/p>\n<p>                     else<\/p>\n<p>                     {<\/p>\n<p>                            this.lblMsg.Text = &#8220;\u6ca1\u6709\u627e\u5230\u8fd9\u7bc7\u6587\u7ae0&#8221;;<\/p>\n<p>                     }<\/p>\n<p>              }<\/p>\n<p>              #region Web \u7a97\u4f53\u8bbe\u8ba1\u5668\u751f\u6210\u7684\u4ee3\u7801<\/p>\n<p>              override protected void OnInit(EventArgs e)<\/p>\n<p>              {<\/p>\n<p>                     \/\/<\/p>\n<p>                     \/\/ CODEGEN: \u8be5\u8c03\u7528\u662f ASP.NET Web \u7a97\u4f53\u8bbe\u8ba1\u5668\u6240\u5fc5\u9700\u7684\u3002<\/p>\n<p>                     \/\/<\/p>\n<p>                     InitializeComponent();<\/p>\n<p>                     base.OnInit(e);<\/p>\n<p>              }<\/p>\n<p>              \/\/\/ <\/p>\n<summary>\n<p>              \/\/\/ \u8bbe\u8ba1\u5668\u652f\u6301\u6240\u9700\u7684\u65b9\u6cd5 &#8211; \u4e0d\u8981\u4f7f\u7528\u4ee3\u7801\u7f16\u8f91\u5668\u4fee\u6539<\/p>\n<p>              \/\/\/ \u6b64\u65b9\u6cd5\u7684\u5185\u5bb9\u3002<\/p>\n<p>              \/\/\/ <\/summary>\n<p>              private void InitializeComponent()<\/p>\n<p>              {   <\/p>\n<p>                     this.Load += new System.EventHandler(this.Page_Load);<\/p>\n<p>              }<\/p>\n<p>              #endregion<\/p>\n<p>       }<\/p>\n<p>}<\/p>\n<p>4\u3001WebComponents\u9879\u76ee<\/p>\n<p>\uff081\uff09CleanString.cs<\/p>\n<p>using System;<\/p>\n<p>using System.Text;<\/p>\n<p>namespace myWeb.WebComponents<\/p>\n<p>{<\/p>\n<p>       \/\/\/ <\/p>\n<summary>\n<p>       \/\/\/ CleanString \u7684\u6458\u8981\u8bf4\u660e\u3002<\/p>\n<p>       \/\/\/ <\/summary>\n<p>       public class CleanString<\/p>\n<p>       {<\/p>\n<p>              public static int GetInt(string inputString)<\/p>\n<p>              {<\/p>\n<p>                     try<\/p>\n<p>                     {<\/p>\n<p>                            return Convert.ToInt32(inputString);<\/p>\n<p>                     }<\/p>\n<p>                     catch<\/p>\n<p>                     {<\/p>\n<p>                            return 0;<\/p>\n<p>                     }<\/p>\n<p>              }<\/p>\n<p>              public static string InputText(string inputString, int maxLength)<\/p>\n<p>              {<\/p>\n<p>                     StringBuilder retVal = new StringBuilder();<\/p>\n<p>                     \/\/ check incoming parameters for null or blank string<\/p>\n<p>                     if ((inputString != null) &#038;&#038; (inputString != String.Empty))<\/p>\n<p>                     {<\/p>\n<p>                            inputString = inputString.Trim();<\/p>\n<p>                            \/\/chop the string incase the client-side max length<\/p>\n<p>                            \/\/fields are bypassed to prevent buffer over-runs<\/p>\n<p>                            if (inputString.Length > maxLength)<\/p>\n<p>                                   inputString = inputString.Substring(0, maxLength);<\/p>\n<p>                            \/\/convert some harmful symbols incase the regular<\/p>\n<p>                            \/\/expression validators are changed<\/p>\n<p>                            for (int i = 0; i < inputString.Length; i++)\n\n                            {\n\n                                   switch (inputString[i])\n\n                                   {\n\n                                          case '\"':\n\n                                                 retVal.Append(\"&quot;\");\n\n                                                 break;\n\n                                          case '<':\n\n                                                 retVal.Append(\"&lt;\");\n\n                                                 break;\n\n                                          case '>&#8216;:<\/p>\n<p>                                                 retVal.Append(&#8220;&gt;&#8221;);<\/p>\n<p>                                                 break;<\/p>\n<p>                                          default:<\/p>\n<p>                                                 retVal.Append(inputString[i]);<\/p>\n<p>                                                 break;<\/p>\n<p>                                   }<\/p>\n<p>                            }<\/p>\n<p>                            \/\/ Replace single quotes with white space<\/p>\n<p>                            retVal.Replace(&#8220;&#8216;&#8221;, &#8221; &#8220;);<\/p>\n<p>                     }<\/p>\n<p>                     return retVal.ToString();<\/p>\n<p>              }<\/p>\n<p>       }<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6570\u636e\u5e93 \/*=============================================== [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[457],"class_list":["post-1635","post","type-post","status-publish","format-standard","hentry","category-fromnetwork","tag-asp-net"],"views":348,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1635"}],"version-history":[{"count":1,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1635\/revisions"}],"predecessor-version":[{"id":1636,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1635\/revisions\/1636"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1635"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}