在asp.net中硬编码到web.config中

时间:2013-10-04 07:03:47

标签: c# asp.net web-config

下面指定的是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#pages我有这些代码行。

我的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>

    

    

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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+"'";

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