我数据库的连接写在程序里就行,但一配置在web.config里就不行。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public class MSSqlserver
{
    private string connString = "Data Source=OLAPSERVER\\SQLSERVER2005;Initial Catalog=BusinessDB;User ID=sa;Password=sa;Pooling='true';Max Pool Size=60;Min Pool Size=6";
    private SqlConnection myConnection = null;    public MSSqlserver()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
    //获取用于连接mssqlserver数据库的字符串。
    public string GetConnectionString()
    {
        //connString = ConfigurationManager.AppSettings["MSSQLSERVER"].Trim().ToString();
        return (connString);
    }    //打开一个数据库的连接
    public void Open()
        {
            //使用using表示建立好myConnection就释放。
            if (myConnection == null)
            {
                myConnection = new SqlConnection(GetConnectionString());
            }            if (myConnection.State == ConnectionState.Closed)
            {
                try
                {
                    myConnection.Open();
                }
                catch (Exception ex)
                {
                    //记录数据库打开错误。
                    throw new Exception("MSSqlserver.Open():" + ex.Message);
                }
                finally
                {
                }
            }
        }
    /// <summary>
        /// 执行非查询的SQL语句。如语句的insert/update/delete。同时返回更新的记录数。
        /// </summary>
        /// <param name="sqlString"></param>
        /// <returns></returns>
    public int ExecuteSQL(string sqlString)
        {
            //定义返回值变量
            int Records = -1;            //打开数据库连接
            Open();            //建立一个SqlCommand对像
            SqlCommand myCommand = new SqlCommand(sqlString, myConnection);            //执行一个语句。
            try
            {
                Records = myCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //定义执行SQL语句错误。
                throw new Exception("MSSqlserver.ExecuteSQL(1):" + ex.Message);
            }
            finally
            {
                Close();
            }            return (Records);
        }    /// <summary>
        /// 返回一个只可查询的数据集。
        /// </summary>
        /// <param name="sqlString"></param>
        /// <param name="readData"></param>
    public void ExecuteSQL(string sqlString, out SqlDataReader DataRead)
        {
            //打开数据库连接
            Open();            //建立一个SqlCommand对像。
            SqlCommand myCommand = new SqlCommand(sqlString, myConnection);            //执行一个查询。
            try
            {
                DataRead = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                //定义执行SQL错误时的说法。
                DataRead = null;
                throw new Exception("MSSqlserver.ExecuteSQL(2):" + ex.Message);
            }
        }
    ///关闭一个数据库的连接
    public void Close()
        {
            if (myConnection != null)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
            }
        }
}web.config文件
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="MSSQLSERVER" value="Data Source=OLAPSERVER\SQLSERVER2005;Initial Catalog=BusinessDB;User ID=sa;Password=sa;Pooling='true';Max Pool Size=60;Min Pool Size=6" />
<add key="SQLSERVER" value="server=(local);Initial Catalog=BusinessDB;User ID=sa;Password=sa;Pooling='true';Max Pool Size=60;Min Pool Size=6" />
</appSettings>
<connectionStrings>
<add name="MSSQLSERVER" connectionString="Data Source=OLAPSERVER\SQLSERVER2005;Initial Catalog=BusinessDB;User ID=sa;Password=sa;Pooling='true';Max Pool Size=60;Min Pool Size=6" />
</connectionStrings>
<system.web>
<pages theme="MainTheme"></pages>
<!-- 
            璁剧疆 compilation debug="true" 灏嗚皟璇曠鍙锋彃鍏?
            宸茬紪璇戠殑椤甸潰涓€備絾鐢变簬杩欎細 
            褰卞搷鎬ц兘锛屽洜姝ゅ彧鍦ㄥ紑鍙戣繃绋嬩腑灏嗘鍊?
            璁剧疆涓?true銆?
        -->
<compilation debug="true"/>
<!--
            閫氳繃 <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>
        -->
</system.web>
</configuration>
如果直接用程序里写就可以。但使用web.config就报错:
在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)
server=(local)"

解决方案 »

  1.   

    server=localhost;database=northiwnd;user id=sa;password=sa;
      

  2.   

    <configuration>
    <appSettings>
    <add key="db" value="server=localhost;uid=sa;pwd=sa;database=MyDB"/>
    </appSettings>
    </configuration>
      

  3.   

    configuration>
    <appSettings>
    <add key="db" value="server=localhost;uid=sa;pwd=sa;database=MyDB"/>
    </appSettings>
    </configuration>这个方式也写了,也是不行。
      

  4.   

    连接字符串有问题
    Provider=SQLOLEDB;User Id=sa;Password=sa;Initial Catalog=BusinessDB;Data Source=.
      

  5.   

    那把server改下不就行了
    也就是<configuration>
    <appSettings>
    <add key="db" value="server=10.0.0.110;uid=sa;pwd=sa;database=MyDB"/>
    </appSettings>
    </configuration>
      

  6.   

    <add key="db" value="server=10.0.0.110;uid=sa;pwd=sa;database=MyDB"/>
    </appSettings>如果我哪台机器有多个实例。是否在server=12.12.2.2//onlinename
    ??
      

  7.   

    http://community.csdn.net/Expert/topic/4928/4928357.xml?temp=.9170038