liuyuqi-dellpc 8 years ago
parent
commit
882b083d90
7 changed files with 395 additions and 0 deletions
  1. 35 0
      AccessHelper.sln
  2. BIN
      AccessHelper.suo
  3. 183 0
      App_Code/AccessHelper.cs
  4. BIN
      App_Data/db.mdb
  5. 22 0
      Default.aspx
  6. 33 0
      Default.aspx.cs
  7. 122 0
      web.config

+ 35 - 0
AccessHelper.sln

@@ -0,0 +1,35 @@
+
+Microsoft Visual Studio Solution File, Format Version 10.00
+# Visual Studio 2008
+Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "AccessHelper", ".", "{0ECB4FC4-FF80-454D-8257-D2BFCA2F004C}"
+	ProjectSection(WebsiteProperties) = preProject
+		TargetFramework = "3.5"
+		Debug.AspNetCompiler.VirtualPath = "/AccessHelper"
+		Debug.AspNetCompiler.PhysicalPath = "..\..\..\workspace\AccessHelper\"
+		Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\AccessHelper\"
+		Debug.AspNetCompiler.Updateable = "true"
+		Debug.AspNetCompiler.ForceOverwrite = "true"
+		Debug.AspNetCompiler.FixedNames = "false"
+		Debug.AspNetCompiler.Debug = "True"
+		Release.AspNetCompiler.VirtualPath = "/AccessHelper"
+		Release.AspNetCompiler.PhysicalPath = "..\..\..\workspace\AccessHelper\"
+		Release.AspNetCompiler.TargetPath = "PrecompiledWeb\AccessHelper\"
+		Release.AspNetCompiler.Updateable = "true"
+		Release.AspNetCompiler.ForceOverwrite = "true"
+		Release.AspNetCompiler.FixedNames = "false"
+		Release.AspNetCompiler.Debug = "False"
+		VWDPort = "8072"
+	EndProjectSection
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{0ECB4FC4-FF80-454D-8257-D2BFCA2F004C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{0ECB4FC4-FF80-454D-8257-D2BFCA2F004C}.Debug|Any CPU.Build.0 = Debug|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal

BIN
AccessHelper.suo


+ 183 - 0
App_Code/AccessHelper.cs

@@ -0,0 +1,183 @@
+using System;
+using System.Data;
+using System.Data.OleDb;
+
+/// <summary>
+///Access数据库操作类
+///创建时间:2010年4月16日17时9分
+///作者:牛腩
+///QQ: 164423073
+/// </summary>
+public class AccessHelper
+{
+    private OleDbConnection conn = null;
+    private OleDbCommand cmd = null;
+    private OleDbDataReader sdr = null;
+
+    public AccessHelper()
+    {
+        //  string connStr = WebConfigurationManager.ConnectionStrings["connStr"].ToString();
+        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|db.mdb";
+        conn = new OleDbConnection(connStr);
+    }
+
+    /// <summary>创建Command对象
+    /// 
+    /// </summary>
+    /// <param name="sql">SQL语句</param>
+    public void CreateCommand(string sql)
+    {
+        conn.Open();
+        cmd = new OleDbCommand(sql, conn);
+    }
+
+    /// <summary>添加参数
+    /// 
+    /// </summary>
+    /// <param name="paramName">参数名称</param>
+    /// <param name="value">值</param>
+    public void AddParameter(string paramName, object value)
+    {
+        cmd.Parameters.Add(new OleDbParameter(paramName, value));
+
+    }
+
+    /// <summary>执行不带参数的增删改SQL语句
+    ///  
+    /// </summary>
+    /// <param name="cmdText">增删改SQL语句</param>
+    /// <param name="ct">命令类型</param>
+    /// <returns></returns>
+    public bool ExecuteNonQuery()
+    {
+        int res;
+        try
+        {
+            res = cmd.ExecuteNonQuery();
+            if (res > 0)
+            {
+                return true;
+            }
+        }
+        catch (Exception ex)
+        {
+            throw ex;
+        }
+        finally
+        {
+            if (conn.State == ConnectionState.Open)
+            {
+                conn.Close();
+            }
+        }
+        return false;
+    }
+
+    /// <summary>执行查询SQL语句
+    ///  
+    /// </summary>
+    /// <param name="cmdText">查询SQL语句</param>
+    /// <returns></returns>
+    public DataTable ExecuteQuery()
+    {
+        DataTable dt = new DataTable();
+        using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
+        {
+            dt.Load(sdr);
+        }
+        return dt;
+    }
+
+    /// <summary>分页
+    /// 
+    /// </summary>
+    /// <param name="tblName">表名</param>
+    /// <param name="fldName">字段名</param>
+    /// <param name="OrderfldName">排序字段名</param>
+    /// <param name="OrderType">排序方式:asc或者desc</param>
+    /// <param name="strWhere">条件,不用加where</param>
+    /// <param name="PageSize">页大小</param>
+    /// <param name="PageIndex">页索引</param>
+    /// <returns></returns>
+    public DataTable FengYe(string tblName, string fldName, string OrderfldName, string OrderType, string strWhere, int PageSize, int PageIndex)
+    {
+        DataTable dt = new DataTable();
+        string strSQL = ""; // 主语句 
+        string strTmp = ""; // 临时变量 
+        string strOrder = ""; // 排序类型
+
+        if (OrderType == "desc")
+        {
+            // 降序
+            strTmp = "<(select min";
+            strOrder = " order by " + OrderfldName + " desc";
+        }
+        else
+        {
+            // 升序
+            strTmp = ">(select max";
+            strOrder = " order by " + OrderfldName + " asc";
+        }
+
+        #region 第一页
+        if (PageIndex == 1)
+        {
+            strTmp = string.IsNullOrEmpty(strWhere) ? "" : " where " + strWhere;
+            strSQL = "select top " + PageSize + " " + fldName + " from " + tblName + strTmp + " " + strOrder;
+            CreateCommand(strSQL);
+            dt = ExecuteQuery();
+            return dt;
+        }
+        #endregion
+
+        #region 不是第一页
+        if (string.IsNullOrEmpty(strWhere))
+        {
+            // 条件为空
+            strSQL = string.Format("select top {0} {1} from {2} where {3}{4}({5}) from (select top {6} {7} from {8} {9}) as tblTmp) {10}", PageSize, fldName, tblName, OrderfldName, strTmp, OrderfldName, (PageIndex - 1) * PageSize, OrderfldName, tblName, strOrder, strOrder);
+            CreateCommand(strSQL);
+            dt = ExecuteQuery();
+        }
+        else
+        {
+            // 条件不为空
+            strSQL =string.Format( "select top {0} {1} from {2} where  {3}{4}({5}) from (select top {6} {7} from {8} where {9} {10}) as tblTmp) and {11} {12}",PageSize,fldName,tblName,OrderfldName,strTmp,OrderfldName,(PageIndex-1)*PageSize,OrderfldName,tblName,strWhere,strOrder,strWhere,strOrder);
+            CreateCommand(strSQL);
+            dt = ExecuteQuery();
+        }
+        #endregion
+
+        return dt;
+    }
+
+    /// <summary>返回查询SQL语句查询出的结果的第一行第一列的值
+    /// 
+    /// </summary>
+    /// <returns></returns>
+    public string ExecuteScalar()
+    {
+        string res = "";
+        try
+        {
+            object obj = cmd.ExecuteScalar();
+            if (obj != null)
+            {
+                res = obj.ToString();
+            }
+        }
+        catch (Exception ex)
+        {
+            throw ex;
+        }
+        finally
+        {
+            if (conn.State == ConnectionState.Open)
+            {
+                conn.Close();
+            }
+        }
+        return res;
+    }
+
+}
+

BIN
App_Data/db.mdb


+ 22 - 0
Default.aspx

@@ -0,0 +1,22 @@
+<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
+
+<%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head runat="server">
+    <title></title>
+</head>
+<body>
+    <form id="form1" runat="server">
+    <div>
+    <asp:gridview ID="gv" runat="server"></asp:gridview>
+        <webdiyer:AspNetPager ID="anp" PageSize="9" runat="server" 
+            onpagechanged="anp_PageChanged">
+        </webdiyer:AspNetPager>
+    </div>
+    </form>
+</body>
+</html>
+

+ 33 - 0
Default.aspx.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.UI;
+using System.Web.UI.WebControls;
+
+public partial class _Default : System.Web.UI.Page
+{
+    AccessHelper helper = new AccessHelper();
+    string cond = "caid=6";
+    protected void Page_Load(object sender, EventArgs e)
+    {
+        if (!IsPostBack)
+        {
+            helper.CreateCommand("select count(*) from news where "+cond);
+            anp.RecordCount = int.Parse(helper.ExecuteScalar());
+            bindrep();
+        }
+    }
+    protected void anp_PageChanged(object sender, EventArgs e)
+    {
+        bindrep();
+    }
+
+    private void bindrep()
+    {
+        int pagesize = anp.PageSize;
+        int pageindex = anp.CurrentPageIndex;
+        gv.DataSource = helper.FengYe("(news n inner join newsca ca on n.caid=ca.id)", "*", "createdate", "desc", cond, pagesize, pageindex);
+        gv.DataBind();
+    }
+}

+ 122 - 0
web.config

@@ -0,0 +1,122 @@
+<?xml version="1.0"?>
+<!-- 
+    注意: 除了手动编辑此文件以外,您还可以使用 
+    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
+     “网站”->“Asp.Net 配置”选项。
+    设置和注释的完整列表在 
+    machine.config.comments 中,该文件通常位于 
+    \Windows\Microsoft.Net\Framework\v2.x\Config 中
+-->
+<configuration>
+	<configSections>
+		<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
+			<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
+				<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
+				<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
+					<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
+					<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
+					<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
+					<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
+				</sectionGroup>
+			</sectionGroup>
+		</sectionGroup>
+	</configSections>
+	<appSettings/>
+	<connectionStrings>
+		<add connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|charge.mdb" name="testConnStr"/>
+	</connectionStrings>
+	<system.web>
+		<!-- 
+            设置 compilation debug="true" 可将调试符号插入
+            已编译的页面中。但由于这会 
+            影响性能,因此只在开发过程中将此值 
+            设置为 true。
+        -->
+		<compilation debug="true">
+			<assemblies>
+				<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
+				<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+				<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
+				<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
+			</assemblies>
+		</compilation>
+		<!--
+            通过 <authentication> 节可以配置 ASP.NET 用来 
+            识别进入用户的
+            安全身份验证模式。 
+        -->
+		<authentication mode="Windows"/>
+		<!--
+            如果在执行请求的过程中出现未处理的错误,
+            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
+            开发人员通过该节可以配置
+            要显示的 html 错误页
+            以代替错误堆栈跟踪。
+
+        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
+            <error statusCode="403" redirect="NoAccess.htm" />
+            <error statusCode="404" redirect="FileNotFound.htm" />
+        </customErrors>
+        -->
+		<pages>
+			<controls>
+				<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+				<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+			</controls>
+		</pages>
+		<httpHandlers>
+			<remove verb="*" path="*.asmx"/>
+			<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+			<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+			<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
+		</httpHandlers>
+		<httpModules>
+			<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+		</httpModules>
+	</system.web>
+	<system.codedom>
+		<compilers>
+			<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+				<providerOption name="CompilerVersion" value="v3.5"/>
+				<providerOption name="WarnAsError" value="false"/>
+			</compiler>
+			<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+				<providerOption name="CompilerVersion" value="v3.5"/>
+				<providerOption name="OptionInfer" value="true"/>
+				<providerOption name="WarnAsError" value="false"/>
+			</compiler>
+		</compilers>
+	</system.codedom>
+	<!-- 
+        在 Internet 信息服务 7.0 下运行 ASP.NET AJAX 需要 system.webServer
+        节。对早期版本的 IIS 来说则不需要此节。
+    -->
+	<system.webServer>
+		<validation validateIntegratedModeConfiguration="false"/>
+		<modules>
+			<remove name="ScriptModule"/>
+			<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+		</modules>
+		<handlers>
+			<remove name="WebServiceHandlerFactory-Integrated"/>
+			<remove name="ScriptHandlerFactory"/>
+			<remove name="ScriptHandlerFactoryAppServices"/>
+			<remove name="ScriptResource"/>
+			<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+			<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+			<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
+		</handlers>
+	</system.webServer>
+	<runtime>
+		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
+			<dependentAssembly>
+				<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
+				<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
+			</dependentAssembly>
+			<dependentAssembly>
+				<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
+				<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
+			</dependentAssembly>
+		</assemblyBinding>
+	</runtime>
+</configuration>