连接对象定义填充表格模型的数据的源。
连接表示形式
连接对象定义填充表格模型的数据的源。 模型可通过连接对象访问来自 OLE DB 访问接口的数据;因此,指定连接对象遵从 OLE DB 访问接口的规则。
AMO 中的连接
在使用 AMO 管理表格模型数据库时,AMO 中的 DataSource 对象与表格模型中的连接逻辑对象一对一匹配。
下面的代码段演示如何在表格模型中创建 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();
AMO2Tabular 示例
为了更好地理解如何使用 AMO 创建和操作连接表示形式,请参阅 AMO 到表格示例中的源代码;具体来讲,请查看以下源文件:Datasource.cs。 该示例在 Codeplex 上提供。 有关该代码的重要说明:提供该代码只是为了支持本文介绍的逻辑概念,不应用于生产环境中;也不应用于除教学之外的其他用途。