2017-07-25 36 views
0

我的應用程序用於複製表database中的表並將它們複製到另一個表中,我使用的是smoc#。我的代碼:當使用DateTime創建表時,NullReferenceExeption列(SMO,C#)

private static void createTable(Table sourcetable, string schema, Server destinationServer, 
     Database db) 
    { 
     Table copiedtable = new Table(db, sourcetable.Name, schema); 

     createColumns(sourcetable, copiedtable); 

     copiedtable.AnsiNullsStatus = sourcetable.AnsiNullsStatus; 
     copiedtable.QuotedIdentifierStatus = sourcetable.QuotedIdentifierStatus; 
     copiedtable.TextFileGroup = sourcetable.TextFileGroup; 
     copiedtable.FileGroup = sourcetable.FileGroup; 

     copiedtable.Create(); 
    } 

private static void createColumns(Table sourcetable, Table copiedtable) 
    { 

     foreach (Column source in sourcetable.Columns) 
     { 
      Column column = new Column(copiedtable, source.Name, source.DataType); 
      column.Collation = source.Collation; 
      column.Nullable = source.Nullable; 
      column.Computed = source.Computed; 
      column.ComputedText = source.ComputedText; 
      column.Default = source.Default; 

      if (source.DefaultConstraint != null) 
      { 
       string tabname = copiedtable.Name; 
       string constrname = source.DefaultConstraint.Name; 
       column.AddDefaultConstraint(tabname + "_" + constrname); 
       column.DefaultConstraint.Text = source.DefaultConstraint.Text; 
      } 

      column.IsPersisted = source.IsPersisted; 
      column.DefaultSchema = source.DefaultSchema; 
      column.RowGuidCol = source.RowGuidCol; 

      if (server.VersionMajor >= 10) 
      { 
       column.IsFileStream = source.IsFileStream; 
       column.IsSparse = source.IsSparse; 
       column.IsColumnSet = source.IsColumnSet; 
      } 

      copiedtable.Columns.Add(column); 
     } 
    } 

項目非常順利地使用北風database的作品,但是,從AdventureWorks2014 database一些表我得到在copiedtable.Create();以下內部異常:

的NullReferenceException:未將對象引用設置爲一個對象的實例。

我猜想,那AdventureWorks的datetime列可能是造成問題的原因(數據輸入,如:2008-04-30 00:00:00.000)

+2

的可能的複製(https://stackoverflow.com/問題/ 4660142 /什麼 - 是 - 一 - 的NullReferenceException和知識-DO-I-FIX-IT) –

回答

0

我已經解決了這個問題,我自己,這是很有趣。我無法在表中找到任何空值,也無法在列中找到任何空值。 然後我意識到,AdventureWorks2014數據庫使用了用戶定義的數據類型和XML模式集合。由於我沒有複製它們,所以無法訪問它們,並且創建表失敗。它是隻需要XML架構集合和用戶定義的數據類型複製到第二個數據庫:什麼是一個NullReferenceException,以及如何解決呢]

private static void createUserDefinedDataTypes(Database originalDB, Database destinationDB) 
    { 
     foreach (UserDefinedDataType dt in originalDB.UserDefinedDataTypes) 
     { 
      Schema schema = destinationDB.Schemas[dt.Schema]; 
      if (schema == null) 
      { 
       schema = new Schema(destinationDB, dt.Schema); 
       schema.Create(); 
      } 
      UserDefinedDataType t = new UserDefinedDataType(destinationDB, dt.Name); 
      t.SystemType = dt.SystemType; 
      t.Length = dt.Length; 
      t.Schema = dt.Schema; 
      try 
      { 
       t.Create(); 
      } 
      catch(Exception ex) 
      { 
       throw (ex); 
      } 

     } 

    } 
private static void createXMLSchemaCollections(Database originalDB, Database destinationDB) 
    { 
     foreach (XmlSchemaCollection col in originalDB.XmlSchemaCollections) 
     { 
      Schema schema = destinationDB.Schemas[col.Schema]; 
      if (schema == null) 
      { 
       schema = new Schema(destinationDB, col.Schema); 
       schema.Create(); 
      } 
      XmlSchemaCollection c = new XmlSchemaCollection(destinationDB, col.Name); 

      c.Text = col.Text; 
      c.Schema = col.Schema; 


      try 
      { 
       c.Create(); 
      } 
      catch(Exception ex) 
      { 
       throw (ex); 
      } 

     } 

    }