通过SMO获取链接服务器数据库表和视图

时间:2015-03-09 16:06:35

标签: c# sql-server linked-server smo

如何通过SMO获取链接服务器上的SQL Server数据库?

Server server = GetServer("server");
Database db = server.Databases["db"];
LinkedServer ls = server.LinkedServers["ls"];

上面的第二行返回一个常规数据库。第三行返回特定的链接服务器,该服务器提供对链接服务器连接的访问​​,但不提供对其数据的访问。我怎样才能得到类似的东西:

Database db1 = server.LinkedServers["ls"].Databases["db"];

?我需要这个的原因是我将遍历链接数据库中的不同对象,例如表或视图。

更新

为了进一步澄清,我目前有这段代码:

public void GenerateViews(string objectName = null)
{
    Server server = new Server("server");
    //Database a = server.Databases["a"];
    Database b = server.Databases["b"];

    b.Tables.OfType<Table>().ToList().ForEach(o => ProcessSqlObject(o));
    b.Views.OfType<View>().ToList().ForEach(o => ProcessSqlObject(o));
}

//takes all tables and views in database b that have a custom extended property "CreateView", and create a view for it in database a
private void ProcessSqlObject(dynamic o)    //o MUST be an SMO table or view (since they don't implement a common interface, I'm using a dynamic)
{
    Database ct = (Database)o.Parent;
    Database a = ct.Parent.Databases["a"];

    const string viewPrefix = "V_CTC_";   
    const string SourceIDColumnName = "SourceID";    
    string objectName = (string)o.Name;   //name of table or view
    objectName = objectName.StartsWith("V_", StringComparison.InvariantCultureIgnoreCase) ? objectName.Substring(2) : objectName;
    string viewName = viewPrefix + objectName;  //remove V_ from view, so that we don't have "V_V_".

    ExtendedProperty ep = (ExtendedProperty)o.ExtendedProperties["CreateView"];
    bool AlreadyExists = a.Views.OfType<View>().Any(v => v.Name == viewName);

    if (ep != null && ep.Value.ToString() == "1")  //there IS an extended property, and its value is 1, meaning, we want a view
    {
        if (!AlreadyExists) //we don't already have the view    
        {

            //ProcessSqlObject(t, viewName, SourceIDColumnName, ct, a);

            StringBuilder ws = new StringBuilder();
            ws.AppendLine("SELECT");
            ws.AppendLine("\t2 [" + SourceIDColumnName + "]");

            ((ColumnCollection)o.Columns).OfType<Column>().ToList().ForEach(c =>
            {
                ws.AppendLine("\t, [" + c.Name + "]");
            });

            string linkedServer = "[ls].";
            ws.AppendLine("FROM " + linkedServer + "[" + ct.Name + "].[dbo].[" + o.Name + "] WITH(NOLOCK)");

            string rt = ws.ToString();
            rt = rt.Replace("wholesale", "retail");
            rt = rt.Replace("2 [" + SourceIDColumnName + "]", "3 [" + SourceIDColumnName + "]");
            StringBuilder sql = new StringBuilder();

            sql.AppendLine("CREATE VIEW " + viewName + " AS");
            sql.AppendLine();
            sql.AppendLine(ws.ToString());
            //sql.AppendLine();
            sql.AppendLine("UNION ALL");
            sql.AppendLine();
            sql.AppendLine(rt);

            Console.WriteLine(sql);

            a.ExecuteNonQuery(sql.ToString());
        }

    }
    else    //we DON't want the view
    {

        a.Views.OfType<View>().Single(v => v.Name == viewName).Drop();
        a.Refresh();

        }
    }
}

我目前正在向给定数据库中的所有表和视图的第二个函数传递。这不使用链接服务器。我希望能够为链接服务器做同样的事情,而不必重写代码。

感谢。

2 个答案:

答案 0 :(得分:1)

您不需要连接到服务器来获取表和视图(如果您只需要他们的名字)。 LinkedServer类为此提供EnumTables method

答案 1 :(得分:1)

LinkedServer class有一个DataSource属性,您应该可以将其用作远程服务器的名称。如果将其传递给GetServer()函数,则应该返回SMO服务器对象。