簡體   English   中英

asp.net sqlconnection無法打開

[英]asp.net sqlconnection won't open

我正在使用以下內容從數據庫檢索數據,但是sqlconnection無法打開。 它在scon.Open()引發錯誤。 我確定這是基礎知識,但我無法解決。

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();
                }
            }
        }
    }
}

我正在使用此鏈接https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx作為我的參考之一。 如果這些信息有幫助,我還將使用SQLEXPRESS 2008 R2 這是錯誤消息的一部分:

消息=建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱正確,並且已將SQL Server配置為允許遠程連接。 (提供者:命名管道提供程序,錯誤:40-無法打開與SQL Server的連接)

如果能克服這個問題繼續前進,將不勝感激。 提前致謝。

如果您在SQL Server上配置了用戶實例(默認設置),則需要將連接字符串更改為此:

數據源=。\\ SQLExpress;集成安全性= true;

初始目錄= database_Name

從表名中選擇*

您的數據庫名稱和表名稱是否相同?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM