使用 DateTime Column 创建表时出现 NullReferenceException(SMO, C#)

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

提问人:jElliot 提问时间:7/25/2017 最后编辑:CœurjElliot 更新时间:12/26/2018 访问量:92

问:

我的应用程序用于从一个表复制并将它们复制到另一个表,我使用的是 C#。我的代码:databasesmo

 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 完美配合,但是,使用 AdventureWorks2014 中的一些表,我在以下位置收到以下内部异常:databasedatabasecopiedtable.Create();

NullReferenceException:对象引用未设置为对象的实例。

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

C# sql-server 数据库 nullreferenceexception SMO

答:

0赞 jElliot 7/25/2017 #1

我自己解决了这个问题,这很有趣。我在表本身和列中都找不到任何空值。 然后我意识到,AdventureWorks2014 DB 使用了用户定义的数据类型和 XML 架构集合。由于我没有复制它们,因此无法访问它们,并且表的创建失败。只需将 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);
            }

        }

    }