通过 C# 和 SMO 检查并删除现有表
我正在尝试按名称查找 SQL 表,如果存在则删除它。 这一切都需要使用 SMO 在 C# 中完成。
更复杂的是,该表还有一个“dbo”以外的模式。
最终该表将通过 SMO 重新创建(我可以正常工作),但我必须确保它不存在,然后才能重新创建它。
我看到的所有示例似乎都是在相同的上下文中创建然后删除表。 就我而言,该表将在上一个会话中创建并填充。
I am trying to look for a SQL table by name and if it exists drop it. This all needs to be done in C# using SMO.
To complicate it a bit more the table also has a schema other then "dbo".
Ultimatly the table will be recreated via SMO (I have this working) but I have to make sure it is not there before I can recreate it.
All of the examples that I have seen seem to be creating and then dropping the table all in the same context. In my case the table will have been created and populated in a previous session.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为最好的方法是:
I think the best approach would be:
难道您不能将 DROP TABLE 语句包装在 try/catch 块中,并丢弃发生的任何错误吗?
不管怎样,判断表是否存在的sql是:
Couldn't you just wrap your DROP TABLE statement in a try/catch block, and discard any errors that occur?
Anyway, the sql to determine if a table exists is:
第一个问题是,为什么不能用 DDL 删除并重新创建?
并回答你的问题:
First question is, why can you not drop and recreate with DDL?
And in answer to your question: