[英]asp.net cannot open database login failed
I'm using the following to retrieve data from a database but the SqlConnection
won't open. 我正在使用以下内容从数据库检索数据,但是
SqlConnection
无法打开。 It throws an error at scon.Open()
. 它在
scon.Open()
引发错误。 I'm sure it's elementary but I can't work it out. 我确定这是基础知识,但我无法解决。
protected void Page_Load(object sender, EventArgs e) {
SqlConnection scon = new SqlConnection("Data Source = .\\SQLEXPRESS; Database = populate.mdf; Integrated Security = true; Initial Catalog = populate");
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();
}
}
}
}
I'm using this link https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx as one of my references. 我正在使用此链接https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx作为我的参考之一。 I'm also using SQL Server 2008 R2 Express if these information are of any help.
如果这些信息有帮助,我还将使用SQL Server 2008 R2 Express。
Here's part of the error message: 这是错误消息的一部分:
SqlException (0x80131904): Cannot open database "populate" requested by the login.
SqlException(0x80131904):无法打开登录请求的数据库“填充”。 The login failed.
登录失败。
Any help would be greatly appreciated. 任何帮助将不胜感激。
Quoted from https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx#sse , if you want to use .mdf file as a database, you should use the following connection string containing AttacheDbFileName . 引自https://msdn.microsoft.com/zh-cn/library/jj653752(v=vs.110).aspx#sse ,如果要将.mdf文件用作数据库,则应使用以下连接字符串包含AttacheDbFileName 。
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />
I solved it. 我解决了 All the connection string and other code was correct.
所有连接字符串和其他代码均正确。 The database just needed connecting to the Management Studio with some extra attention.
该数据库仅需要连接到Management Studio,需要特别注意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.