【问题标题】:My database system cannot find the file specified in asp.net我的数据库系统找不到在 asp.net 中指定的文件
【发布时间】:2015-09-30 00:45:24
【问题描述】:

我正在尝试使用以下代码从数据库中检索数据:

public partial class populate : System.Web.UI.Page
{
    SqlConnection scon = new SqlConnection("Data Source = localhost; Integrated Security = true; Initial Catalog = populate");  

    protected void Page_Load(object sender, EventArgs e) {     
        StringBuilder htmlString = new StringBuilder(); 

        if(!IsPostBack)
        {
            using (SqlCommand scmd = new SqlCommand())
            {
                scmd.Connection = scon;
                scmd.CommandType = CommandType.Text;
                scmd.CommandText = "SELECT * FROM populate";

                scon.Open();

                SqlDataReader articleReader = scmd.ExecuteReader();

                htmlString.Append("'Populate page:'");                

                if (articleReader.HasRows)
                {
                    while (articleReader.Read())
                    {
                        htmlString.Append(articleReader["dateTime"]);
                        htmlString.Append(articleReader["firstName"]);
                        htmlString.Append(articleReader["lastName"]);
                        htmlString.Append(articleReader["address"]);
                        htmlString.Append(articleReader["details"]);                       
                    }
                    populatePlaceHolder.Controls.Add(new Literal { Text = htmlString.ToString() });
                    articleReader.Close();
                    articleReader.Dispose();
                }
            }
        }
    }
}

它抛出一个错误:

系统找不到指定的文件

我想知道是否有人可以告诉我错误在哪里或指导我完成调试。提前致谢。

(更新):更具体地说,scon.Open() 导致错误:

消息=在建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)

这看起来很容易修复,但我对数据库不是很好。任何帮助将不胜感激。

【问题讨论】:

  • 数据库是否与您的表同名,填充?我觉得有点奇怪。
  • @Christos:我知道这很奇怪,但它是一样的:populate.mdf 并且该表也称为“populate”。
  • 在哪里(在哪一行代码)会引发该错误?
  • @marc_s:看起来像 scon.Open():这是错误的一部分;消息=建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)
  • 所以这可能意味着您的连接字符串是错误的 - SQL Server 实例名称很可能是错误的......

标签: c# html asp.net sql-server


【解决方案1】:

我不知道您安装了哪个 SQL Server 版本,以及您如何称呼它(作为实例名称).....

转到Start > SQL Server > Configuration Tools > Configuration Manager;在SQL Server Services 下,搜索SQL Server 服务——它叫什么名字?

如果是 SQL Server (SQLEXPRESS),则表示您拥有 Express 版本,实例名称为 SQLEXPRESS - 将您的连接字符串更改为:

Data Source=.\SQLEXPRESS;Initial Catalog=populate;Integrated Security=true; 

如果是SQL Server (MSSQLSERVER),那么你应该没问题 - 你有一个未命名的默认实例......

【讨论】:

  • 感谢您的帮助。我正在使用 sqlexpress。我用“Data Source=.\\SQLEXPRESS”尝试了新字符串,但在“scon.Open()”中出现了同样的错误。下面的这个链接为连接字符串((local), localhotst):msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx 提供了很大的灵活性,或者不是真的。
  • 我使用的是 SQL Server 2008 R2。
  • 在对 Management Studio 进行了一段时间的学习和测试后,我开始了解我附加的 .mdf 数据库何时处于活动状态。在那之前,我假设如果 MS 正在运行,那么我的数据库是活动的;不正确。现在一切正常,只需要连接到 MS..
猜你喜欢
  • 2017-05-05
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 2019-08-03
  • 2014-03-16
相关资源
最近更新 更多