C#SMO - 脚本表中的数据文件。投掷错误 [英] C# SMO - Scripting Table Data to File. Throwing Error

查看:147
本文介绍了C#SMO - 脚本表中的数据文件。投掷错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建我的数据库中包含之前,我重新创建表中的数据进行备份。我有那么远,连接到服务器并创建存储SQL文件。现在的问题是它的最后一行抛出一个错误。

I'm trying to create a backup of the data contained within my database before I re-create the tables. I've got so far as to connect to the server and create the file to store the SQL. The problem is its throwing an error on the last line.

对象引用不设置到对象的实例

"Object reference not set to an instance of an object"

我已经在这个项目中的所有的日子,所以可能会失去了一些东西,一个新鲜的一双眼睛将与帮助。

I've been at this project all day so might be missing something that a fresh pair of eyes would help with.

下面的代码:

public void scriptTables()
    {
        string folder = HttpContext.Current.Server.MapPath("/Scripts/SQLScripts/");

        Server myServer = new Server(".\\SQLEXPRESS");
        Database CMSDB = myServer.Databases["CMSDB"];

        Scripter script = new Scripter(myServer);
        ScriptingOptions so = new ScriptingOptions();
        so.ScriptData = true;
        so.ScriptSchema = true;
        so.ScriptDrops = false;

        foreach (Table table in CMSDB.Tables)
        {
            string tables = table.ToString();
            string filename = folder + table + ".sql";
            FileStream fs = File.Create(filename);
            so.FileName = filename;

            CMSDB.Tables[tables].EnumScript(so);
        }
    }
}



一些背景什么我' ]丢:

Some background to what I'm doing:

我要拉已经存在于数据库中的数据,然后更改实体模型。当应用程序重新启动EF框架将删除并重新创建在这一点,我会重新进入,这在以前没有数据的数据库。也不太清楚,如果整个过程将制定出但这将不得不做,直到他们释放EF迁移功能。

I want to pull the data that already exists in the DB and then change the entity models. When the application restarts the EF framework will drop and re-create the database at which point I'll re-enter the data that was previously there. Not too sure if the whole process will work out but this will have to do until they release the migration feature in EF.

希望有人可以提供帮助。谢谢

Hope someone can help. Thanks

推荐答案

我觉得你的问题可能是table.ToString()。尝试使用table.Name代替。

I think your problem is probably table.ToString(). Try using table.Name instead.

这篇关于C#SMO - 脚本表中的数据文件。投掷错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆