C# 我的数据库系统找不到asp.net中指定的文件
我正在尝试使用以下代码从数据库检索数据:C# 我的数据库系统找不到asp.net中指定的文件,c#,html,asp.net,sql-server,C#,Html,Asp.net,Sql Server,我正在尝试使用以下代码从数据库检索数据: 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, Ev
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()导致错误:
Message=建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40-无法打开到SQL Server的连接)
这看起来很容易修复,但我对数据库不是很在行。任何帮助都将不胜感激。我不知道您安装了什么SQL Server版本,以及您将其命名为什么(作为实例名称) 转到
Start>sqlserver>configurationtools>configurationmanager
;在SQL Server Services
下,搜索SQL Server
服务-名称是什么
如果是SQL Server(SQLEXPRESS)
,则意味着您拥有Express edition,实例名称为SQLEXPRESS
-将连接字符串更改为:
Data Source=.\SQLEXPRESS;Initial Catalog=populate;Integrated Security=true;
如果是
SQL Server(MSSQLSERVER)
,那么您应该很好-您有一个未命名的默认实例……数据库是否与您的表同名,请填充?在我看来这有点奇怪。@Christos:我知道这很奇怪,但它是一样的:populate.mdf,表也叫“populate”。它在哪里(在哪行代码上)抛出错误?@marc_s:看起来像scon.Open():这是部分错误;Message=建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40-无法打开到SQL Server的连接)因此,这可能意味着您的连接字符串错误-很可能是错误的SQL Server实例名称…感谢您的帮助。我正在使用sqlexpress。我尝试使用“Data Source=。\\SQLEXPRESS”创建新字符串,但在“scon.Open()”中出现相同的错误。下面的链接为连接字符串((local)、localhotst)提供了很大的灵活性:或者说它不是真的。我正在使用SQL Server 2008 R2。在学习和测试Management Studio一段时间后,我开始了解我连接的.mdf数据库何时处于活动状态。在此之前,我假设如果MS正在运行,则我的db处于活动状态;不对。现在一切正常,只需要连接到MS。。