使用 DateTime 列(SMO、C#)创建 table 时出现 NullReferenceException

NullReferenceException when creating a table with DateTime Column(SMO, C#)

我的应用程序用于从一个 database 复制表格并将它们复制到另一个,我正在使用 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);
        }
    }

该项目与 North wind database 配合得很好,但是,使用 AdventureWorks2014 database 中的一些表格,我在 copiedtable.Create(); 处得到以下内部异常:

NullReferenceException: Object reference not set to an instance of an object.

我怀疑 AdventureWorks datetime 列可能导致了问题(数据输入如下:2008-04-30 00:00:00.000)

这个问题我自己解决了,挺有意思的。我在 Table 本身和它的列中都找不到任何空值。 然后我意识到,AdventureWorks2014 数据库使用了用户定义的数据类型和 XML 架构集合。由于我没有复制它们,因此无法访问它们并且创建 table 失败。只需要将 XML 模式集合和用户定义的数据类型复制到第二个数据库:

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);
            }

        }

    }