錯誤訊息

System.NullReferenceException: '並未將物件參考設定為物件的執行個體。'

System.Configuration.ConnectionStringSettingsCollection.this[string].get 傳回 null

 

未將物件參考設定為物件的執行個體

web.config要改成

 <!--本機資料庫連線設定 -->
 <!-- add name是這裡的dbservices的cnstr = ConfigurationManager.ConnectionStrings["guestbooks"].ConnectionString;

    
 <connectionStrings>
   <add name="guestbooks" connectionString="Persist Security Info=False;Integrated Security=true;       
   Server=localhost; Initial Catalog=guestbooks ;User ID=;Password=;" providerName="System.Data.SqlClient"></add>
 </connectionStrings>

就可以正常顯示畫面了

未將物件參考設定為物件的執行個體

但是輸入之後 會出現錯誤

未將物件參考設定為物件的執行個體

解決方法

<!--本機資料庫連線設定 -->
<!-- add name是這裡的dbservices的cnstr = ConfigurationManager.ConnectionStrings["guestbooks"].ConnectionString;-->    
<connectionStrings>
  <add name="guestbooks" connectionString="Persist Security Info=False;Integrated Security=true;       
  Server=(localdb)\MSSQLLocalDB; Initial Catalog=ASP ;User ID=;Password=;" providerName="System.Data.SqlClient"></add>
</connectionStrings>

name的guestbooks是建立資料庫連線的字串  

ConnectionStrings["guestbooks"].ConnectionString; ( 擷取其中一段 )

Catalog = ASP , ASP是資料庫的名稱 不是資料表的名稱

( guestbook才是資料表 會寫在程式裡面 )

Server=(localdb)\MSSQLLocalDB ( 本機SSMS的登入端 )

自己SSMS的截圖

ASP連線到資料庫

參考資料

微軟官方資料 

SQL Server Express LocalDB

終於 連到資料庫了 ... 好感動

如果都沒錯誤的話 其實連到資料庫的速度蠻快的

要是會卡卡 就代表應該是有錯誤 會跳出錯誤訊息

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 湯尼 的頭像
    湯尼

    湯尼的部落格

    湯尼 發表在 痞客邦 留言(0) 人氣()