共用方式為


第 2 課:建立通往範例資料庫的連接

第一步是建立 AdventureWorks2008R2 範例資料庫的連接,以便產生報表定義的欄位清單。

[!附註]

如果您要使用 SQL Server Express with Advanced Services,連接資料來源就必須包含 SQL Server Express 具名執行個體。根據預設,AdventureWorks2008R2 範例資料庫的連接字串為 "Data Source=localhost\SQLExpress; Initial Catalog=AdventureWorks2008R2"。如需詳細資訊,請參閱<SQL Server Express with Advanced Services 中的 Reporting Services>。

若要建立通往 AdventureWorks 的連接

  • 以下列程式碼取代成專案中 OpenConnection() 方法的程式碼:
Public Sub OpenConnection()
   ' Create a connection object
   m_connection = New SqlConnection()
   
   ' Create the connection string
   m_connectString = "data source=localhost;initial catalog=AdventureWorks2008R2;integrated security=SSPI"
   m_connection.ConnectionString = m_connectString
   
   ' Open the connection
   m_connection.Open()
End Sub 'OpenConnection
public void OpenConnection()
{
   // Create a connection object
   m_connection = new SqlConnection();
         
   // Create the connection string
   m_connectString = @"data source=localhost;initial catalog=AdventureWorks2008R2;integrated security=SSPI";
   m_connection.ConnectionString = m_connectString; 
         
   // Open the connection
   m_connection.Open();
}

[!附註]

您應該以對您特定組態有效的連接字串來取代這裡使用的連接字串。先前的連接字串假設您已將 AdventureWorks2008R2 資料庫安裝到 SQL Server 的本機執行個體。