連接物件會定義填入表格式模型之數據源。
連接表示法
連接物件的規格遵循 OLE DB 提供者的規則。
AMO 中的連線
使用 AMO 來管理表格式模型資料庫時, DataSource AMO 中的物件會比對一對一與連接邏輯物件。
下列代碼段示範如何在表格式模型中建立 AMO 數據源或 Connection 物件。
//Create an OLEDB connection string
StringBuilder SqlCnxStr = new StringBuilder();
SqlCnxStr.Append(String.Format("Data Source={0};" ,SQLServer.Text));
SqlCnxStr.Append(String.Format("Initial Catalog={0};", SQLDatabase.Text));
SqlCnxStr.Append(String.Format("Persist Security Info={0};", false));
SqlCnxStr.Append(String.Format("Integrated Security={0};", "SSPI"));
SqlCnxStr.Append(String.Format("Provider={0}", "SQLNCLI11"));
String DatasourceCnxString = SqlCnxStr.ToString();
//Verify connection string and connectivity
System.Data.OleDb.OleDbConnection OleDbCnx = new System.Data.OleDb.OleDbConnection(DatasourceCnxString);
try
{
OleDbCnx.Open();
}
catch ()
{
throw;
}
String newDataSourceName = (string.IsNullOrEmpty(DataSourceName.Text) || string.IsNullOrWhiteSpace(DataSourceName.Text)) ? SQLDatabase.Text : DataSourceName.Text;
AMO.DataSource newDatasource = newDatabase.DataSources.Add(newDataSourceName, newDataSourceName);
newDatasource.ConnectionString = DatasourceCnxString.Text;
if (impersonateServiceAccount.Checked)
newDatasource.ImpersonationInfo = new AMO.ImpersonationInfo(AMO.ImpersonationMode.ImpersonateServiceAccount);
else
{
if (String.IsNullOrEmpty(impersonateAccountUserName.Text))
{
MessageBox.Show(String.Format("Account User Name cannot be blank when using 'ImpersonateAccount' mode"), "AMO to Tabular message", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
newDatasource.ImpersonationInfo = new AMO.ImpersonationInfo(AMO.ImpersonationMode.ImpersonateAccount, impersonateAccountUserName.Text, impersonateAccountPassword.Text);
}
newDatasource.Update();
表格式 AMO 2012 範例
若要深入瞭解如何使用 AMO 來建立及作連接表示法,請參閱表格式 AMO 2012 範例中的原始程式碼;請特別簽入下列原始程序檔:Database.cs。 此範例可在 Codeplex 取得。 範例程式代碼只提供這裡所說明邏輯概念的支援,而且不應該在生產環境中使用。