我正在尝试使用以下代码从数据库中检索数据:
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的连接)
这看起来很容易修复,但是我对数据库不是很好。任何帮助将不胜感激。
我不知道您已经安装了什么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)
那您真的应该没事-您有一个未命名的默认实例...。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句