using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;/// <summary>
///yishilalaDBSQL 的摘要说明
/// </summary>
public class yishilalaDBSQL
{
public yishilalaDBSQL()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
    public bool TestConnect()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data source=.\\SQLExpree;Integrated Security=true; AttachdbFilename=|DataDirectory|\\yishilala.mdf;User Instance=true";
        try
        {
            conn.Open();
            return (conn.State == ConnectionState.Open);
        }
        catch (Exception ex)
        {
            return false;
        }
        finally
        {
            conn.Close();
        }
    }
}
后台代码是:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;public partial class web_New : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        yishilalaDBSQL db = new yishilalaDBSQL();
        bool Connect = db.TestConnect();
        Response.Write(Connect);    }
}如果连接数据库成功,网页会显示true,而我的是显示false,表明是连接不成功,而测试时代码没有提示错误;本人是菜鸟;希望各位大虾多多指点!!

解决方案 »

  1.   

     catch (Exception ex)
      {
      throw ex;
      return false;
      }
    看看抛出的异常是什么
      

  2.   

    在浏览器是出现    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错) 这个是什么错误!!
      

  3.   

    SQL 语句有问题, 你是连接本机 还是远程连接 其他人的服务器, 看看 是否配置了 允许远程连接
      

  4.   

    在浏览器上出现    
    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错) 这个是什么错误呢?
      

  5.   

    看看你Web.Config文件中的IP和你本机IP是否一至,数据库名是否正确!
      

  6.   

    连接字符串中的实例名错了 是sqlexpress