不知道ASP.NET操作SQL数据库,那种方法好,我查了好多资料,最多的是用WEB.CONFIG,按他们说的,我加了<add key="connstring" value="uid=flash;password=3.1415926;database=news;server=(local)" />  
一段在WEB.CONFIG中,结果IIS里提示了,ADD没有声明,晕了,怎么办...............

解决方案 »

  1.   

    http://blog.csdn.net/zheninchangjiang/archive/2005/10/12/500286.aspx
      

  2.   

     CS页面调用//连接字符串
    string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionSqlServer"].ConnectionString;WEB.CONFIG里如下 链接自己改一下
    <connectionStrings>
    <add name="ConnectionSqlServer" connectionString="Data Source=.;Initial Catalog=w2sw;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
    </connectionStrings>
      

  3.   

    按楼上几位DX的意思,加在WEB.CONFIG后,所有页面都打开不开了,提示无法读取配置节“add”,因为它缺少节声明,
    PS.暂时我只修改了WEB.CONFIG,没有在界面里加其它的东西
      

  4.   

    <connectionStrings>
        <add name="ConnectionString" connectionString="server=192.168.0.2;database=test_t;uid=sa;pwd=sa"/>
        
    </connectionStrings>
    <system.web>看 清楚了你写的位置好像不对
      

  5.   

    <add>这个节点要放到<connectionStrings>或<appSettings>节点里面,你想用的话就直接
    string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionSqlServer"].ToString();
      

  6.   

    <appSettings>
        <add key="ConnectionStr" value="Server=(local);User id=xxxx;Pwd=xxxx;Database=xxxx"></add>
      </appSettings> 
      

  7.   

    问吧问吧..没人会笑你的..看看4楼的.看看你把add放哪了..
      

  8.   


    <configuration>
    <appSettings>
    <add key="con" value="User ID=sa;Initial Catalog=Test;Data Source=.;password=sa"/>
    </appSettings>
    <connectionStrings/>
    //2003
    String conn = System.Configuration.ConfigurationSettings.AppSettings["con"];
    //2005
    String conn = System.Configuration.ConfigurationManager.AppSettings["con"];
    String conn = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("con");       
      

  9.   


     <connectionStrings>
        <add name="testConnectionString" connectionString="Data Source=.;Initial Catalog=test;Integrated Security=True"
         providerName="System.Data.SqlClient" />
      </connectionStrings>using System.Data.Common;
    using System.Data.SqlClient;
    public static DbConnection GetConn()
            {
                ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["testConnectionString"];
                DbConnection conn = new SqlConnection(cts.ConnectionString);
                return conn;
            }
    这样就OK··没问题了
      

  10.   

    注意下你添加<appSettings><add name="connectionstring" value=""></add></appSettings>标签的位置(在configuration中)。
    程序中如下引用
    string ConnectionString = System.ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString; 
    以上是vs2003中用法。
      

  11.   

    我以前写类连接,结果调用的时候死活说上下文没有DB(类名是DB),最后我没办法,
    就直接在.ASPX.CS文件里写连接了,到现在我也还是不会写那个类.
      

  12.   

    <appSettings> 
    <add key="ConnectionStr" value="Server=(local);User id=xxxx;Pwd=xxxx;Database=xxxx"> </add> 
    </appSettings> 我一般都是这样写的。。
      

  13.   

    我已经超郁闷了,不过还是要谢谢大家的热心,我装的是VS2008,网站是VS生成的WEB项目,WEB.CONFIG是系统自动生成的,我没改,只是把数据库的连接字加进去,以下是原文件,<?xml version="1.0" encoding="utf-8"?>
    <!-- 
        注意: 除了手动编辑此文件以外,您还可以使用 
        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 name="Conn" value="server=127.0.01;database=safe_system;uid=sa;pwd=jiahao"/>  
          
    </connectionStrings>    <system.web>
        <!-- 
                设置 compilation debug="true" 可将调试符号插入
                已编译的页面中。但由于这会 
                影响性能,因此只应在开发过程中设置 
                此值。            Visual Basic 选项:
                设置 strict="true" 将禁止所有会导致 
                数据丢失的类型转换。 
                设置 explicit="true" 将强制声明所有变量。
            -->
        <compilation debug="false" strict="false" explicit="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>
        <pages>
          <namespaces>
            <clear />
            <add namespace="System" />
            <add namespace="System.Collections" />
            <add namespace="System.Collections.Generic" />
            <add namespace="System.Collections.Specialized" />
            <add namespace="System.Configuration" />
            <add namespace="System.Text" />
            <add namespace="System.Text.RegularExpressions" />
            <add namespace="System.Linq" />
            <add namespace="System.Xml.Linq" />
            <add namespace="System.Web" />
            <add namespace="System.Web.Caching" />
            <add namespace="System.Web.SessionState" />
            <add namespace="System.Web.Security" />
            <add namespace="System.Web.Profile" />
            <add namespace="System.Web.UI" />
            <add namespace="System.Web.UI.WebControls" />
            <add namespace="System.Web.UI.WebControls.WebParts" />
            <add namespace="System.Web.UI.HtmlControls" />
          </namespaces>      <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>
        <!--
                通过 <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>
            -->
        <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>
      

  14.   


      <!-- 
            在 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>系统提示:
    错误摘要
    HTTP 错误 500.19 - Internal Server Error
    无法访问请求的页面,因为该页的相关配置数据无效。 详细错误信息模块 IIS Web Core 
    通知 BeginRequest 
    处理程序 尚未确定 
    错误代码 0x80070032 
    配置错误 无法读取配置节“add”,因为它缺少节声明  
    配置文件 \\?\D:\项目工程\WebSite\web.config 
    请求的 URL http://127.0.0.1:80/ 
    物理路径 D:\项目工程\WebSite 
    登录方法 尚未确定 
    登录用户 尚未确定 
     配置源
       31:   <connectionStrings/>
       32:   <add name="Conn" value="server=127.0.01;database=safe_system;uid=sa;pwd=jiahao"/>  
       33:       
    5555555请大家指教吧
      

  15.   

    <connectionStrings/> 
       32:    <add name="Conn" value="server=127.0.01;database=safe_system;uid=sa;pwd=jiahao"/>   
       33:        
    把那个connectionStrings/后面的/去掉就可以了
      

  16.   

    31:    <connectionStrings/> 
      32:    <add name="Conn" value="server=127.0.01;database=safe_system;uid=sa;pwd=jiahao"/>   
      33:   很明显你把位置搞错了··
    应该是
    <connectionStrings> 
          <add name="Conn" value="server=127.0.01;database=safe_system;uid=sa;pwd=jiahao"/> 
    </connectionStrings>
      

  17.   

    那个激动啊,终于解决问题了,菜鸟果然容易出错,谢谢大家了,另外,想问一下,就是可以在WEBCONFIG中加个东西,然后整个网站的TITLE就统一了