-->

Using SMO to script PARTIAL data content (only row

2019-06-02 06:39发布

问题:

I use SMO to fill a SQL Compact database with the data of a SQL server database.

Here is the code I actually use:

foreach(Table l_tblCurrent in l_dbDatabase.Tables)
{
  if(l_tblCurrent.IsSystemObject) continue;

  ScriptingOptions l_scOptions = new ScriptingOptions();

  l_scOptions.NoIdentities        = true;
  l_scOptions.NoCollation         = true;
  l_scOptions.NoCommandTerminator = true;
  l_scOptions.NoFileGroup         = true;
  l_scOptions.ScriptSchema        = true;
  l_scOptions.ScriptData          = true;

  foreach(string l_strCurrent in l_tblCurrent.EnumScript(l_scOptions))
  {
    l_sccDBFCommand.CommandText = l_strCurrent.Replace("[dbo].", "");
    l_sccDBFCommand.ExecuteNonQuery();
  }
}

It works perfectly, but for several tables, I don't want to copy all the rows. I want to be able to select only rows matching a WHERE clause to be copied.

Is it possible ?

回答1:

Doesn't look like SMO supports WHERE clause or any other mechanism to limit the number of records. My suggested workaround is to create a new table containing the subset of records, script it, then drop it. Everything can be done programmatically.