连接对象定义填充表格模型的数据源。
连接表示形式
连接对象的规范遵循 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 中提供。 示例代码仅作为对此处介绍的逻辑概念的支持提供,不应在生产环境中使用。