0

下面指定的是c#代码,我正在使用带有c#的asp.net,下面的代码描述了2个月后它将更新表格的日期差异,

cmd3.CommandText = "update TrackingFaculty_det " 
                   + "SET Type=@Type WHERE " 
                   + "DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,@Today)>60";

我需要将月份值硬编码到 web.config 中,这样如果我想将日期差异更改为三个月左右,那么在 web.config 中,如果我进行更改,那么更改将发生在每个 c# 页面中我有这些代码行。

我的 Web.Config 如下

  <?xml version="1.0"?>
   <!-- 
Note: As an alternative to hand editing this file you can use the 
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in 
machine.config.comments usually located in 
\Windows\Microsoft.Net\Framework\v2.x\Config 
-->
 <configuration>
<appSettings/>
<connectionStrings>
    <add name="ProjectConnectionString" connectionString="Data  S ource=BOPSERVER;Initial Catalog=Project;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="ProjectConnectionString2" connectionString="Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="ProjectConnectionString3" connectionString="Data Source=BOPSERVER;Initial Catalog=Project;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
    <!-- 
        Set compilation debug="true" to insert debugging 
        symbols into the compiled page. Because this 
        affects performance, set this value to true only 
        during development.
    -->

    <compilation debug="true" targetFramework="4.0">
        <assemblies>
            <add assembly="Microsoft.Office.Interop.Word, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/></assemblies>
    </compilation>
    <!--
        The <authentication> section enables configuration 
        of the security authentication mode used by 
        ASP.NET to identify an incoming user. 
    -->

    <authentication mode="Windows"/>
    <!--
        The <customErrors> section enables configuration 
        of what to do if/when an unhandled error occurs 
        during the execution of a request. Specifically, 
        it enables developers to configure html error pages 
        to be displayed in place of a error stack trace.

    <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
        <error statusCode="403" redirect="NoAccess.htm" />
        <error statusCode="404" redirect="FileNotFound.htm" />
    </customErrors>
    -->
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>

4

2 回答 2

1

Web.config 代码

   <configuration>
      ....
     <appSettings>
     <add key="Difference" value="60"/>

     </appSettings>
     ....
   </configuration>

从 web.config 获取密钥

    string str_diff = Convert.ToString(ConfigurationManager.AppSettings["Difference"]);
    cmd3.CommandText = "update TrackingFaculty_det SET Type=@Type WHERE (FID=@FID) and DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,@Today)>'"+str_diff+"'";

这就是我需要的,我得到了解决方案

于 2013-10-04T11:26:39.847 回答
1

像这样在 web.config 中添加密钥

<configuration>
   ....
   <appSettings>
      <add key="Difference" value="60"/>

   </appSettings>
   ....
</configuration>

像这样从 web.config 获取密钥

string str_Diff = WebConfigurationManager.AppSettings["Difference"].ToString();

cmd3.CommandText = 
    "update TrackingFaculty_det " 
    + "SET Type=@Type WHERE " 
    + "DATEDIFF(d,TrackingFaculty_det.LastUpdateDate,@Today) > " + str_Diff;
于 2013-10-04T07:13:07.223 回答