【问题标题】:NullReferenceException when creating a table with DateTime Column(SMO, C#)使用 DateTime 列创建表时出现 NullReferenceException(SMO,C#)
【发布时间】:2017-07-25 09:44:16
【问题描述】:

我的应用程序用于从一个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);
        }
    }

该项目与北风 database 完美配合,但是,对于 AdventureWorks2014 database 的一些表,我在 copiedtable.Create(); 得到以下内部异常:

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

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

【问题讨论】:

标签: c# sql-server database nullreferenceexception smo


【解决方案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);
            }

        }

    }

【讨论】:

    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2019-04-24
    • 2019-06-20
    相关资源
    最近更新 更多