在.net中生成脚本插入

时间:2010-10-15 04:35:09

标签: .net sql-server-2005 insert conditional database-scripts

尝试生成具有特定条件的脚本插件时出现问题。

到目前为止,我已经尝试了这一步。

  1. 添加引用Microsoft.SqlServer.ConnectionInfo和Microsoft.SqlServer.Smo

  2. 在代码Microsoft.SqlServer.Management.Smo

  3. 中添加引用
  4. 将此内容添加到脚本中。

      var srv = new Server(@"localhost\SQLEXPRESS");
      var db = srv.Databases["Northwind"];
      foreach (Microsoft.SqlServer.Management.Smo.Table tab in db.Tables)
      {
          foreach (string s in tab.Script())
          {
              File.AppendAllText("sql_insert_conditioanl.sql", s);
          }
      }
    
  5. 脚本“sql_insert_conditioanl.sql”仅包含创建数据库“Northwind”中存在的数据库和创建表。

    所以我研究这个问题。尝试使用参数“ScriptingOptions”ref link的参数更改方法“Script”。我仍然对使用参数脚本选项感到困惑。

    然后还尝试使用protected方法。但由于它受到保护,我无法测试方法(如:Method“ScriptAlter”)。

    方面

1 个答案:

答案 0 :(得分:0)

您需要为所有数据库对象生成脚本 - 例如索引,存储过程,视图等。有关入门的信息,请参阅此article(及其相关文章)。