SMO 脚本对象和安全性

发布于 2024-07-11 20:25:26 字数 583 浏览 5 评论 0原文

我有一个客户在托管服务器上有一个 SQL 数据库; 调用数据库“myDatabase”。

托管公司。 已锁定对象资源管理器 - 我在列出的数据库中看不到 myDatabase(我看到 tempdb 和 master)。 但是,如果我“使用 myDatabase”然后“从 myTable 选择 *”,则一切正常。

由于我们无法访问对象资源管理器,因此我无法右键单击并生成脚本。 我认为我也许能够使用 SMO 来完成我想要的任务,但是当我尝试类似的操作时:

Server myServer = new Server(conn);
Database myDB = server.Databases["myDatabase"];
Table myTbl = myDB.Tables["myTable"];

它失败了 - myDB 为空(当我按预期迭代数据库集合时,我只看到 master 和 tempdb -我可以在对象资源管理器中看到数据库)。 这显然与安全性有关 - 如果我在对象资源管理器中看不到该表,它不会让我通过 SMO 访问它。 有人对允许我生成脚本的解决方法或替代方法有任何想法吗?

谢谢!

I have a customer that has a SQL database on a hosted server; call the db "myDatabase".

The hosting co. has locked down object explorer - I can't see myDatabase in the database listed (I see tempdb and master). However, if I "use myDatabase" and then "select * from myTable", all works fine.

Since we have no access to object explorer, I can't right click and generate scripts. I thought that I might be able to use SMO to accomplish what I want, but when I attempt something similar to this:

Server myServer = new Server(conn);
Database myDB = server.Databases["myDatabase"];
Table myTbl = myDB.Tables["myTable"];

It fails - myDB is null (when I iterate through the databases collection, as expected, I only see master and tempdb - the db's I can see in object explorer). It obviously has to do with security - if I can't see the table in object explorer, it won't let me access it through SMO. Anyone have any ideas of a workaround or alternate method to allow me to generate a script?

Thx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-07-18 20:25:26

我没有看过SMO代码,但是你尝试过在数据库对象上使用构造函数吗? 也许您可以直接访问它。

Database myDB = new Database(myServer, "myDatabase");

I haven't looked at the SMO code, but have you tried using the constructor on the database object? Maybe you can access it directly.

Database myDB = new Database(myServer, "myDatabase");
孤檠 2024-07-18 20:25:26

myDb.Tables 集合是否为空? 您是否使用了错误的名称来引用它?

您可以尝试的一种选择是使用 Linq2Sql 生成数据库模型。 然后,您可以使用该模型创建一个与原始数据库或多或少相同的新数据库。 查看 DataContext.CreateDatabase 方法以获取更多信息。

另一种选择是使用以下查询列出所有表:

select * from sys.tables

然后使用以下查询列出表中的所有列:

select * from sys.columns where object_id = (object id from the previous query)

这将为您提供数据库中定义的所有表和列,并且应该足以创建数据库结构。 此外,您还定义了其他对象的系统视图。

Is the myDb.Tables collection empty? Could it be that you are referencing it using the wrong name?

One option you could try is to use Linq2Sql to generate a model of the database. You can then use the model to create a new database that should be more or less identical to the original. Look up the DataContext.CreateDatabase method for more info.

Another option would be to list all tables using the following query:

select * from sys.tables

And then listing all columns in the tables using the following:

select * from sys.columns where object_id = (object id from the previous query)

This will give you all tables and columns defined in your database and should be enough to create the database structure. In addition you have system views for other objects defined as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文