C# SMO - 将表数据编写到文件中。投掷错误
在重新创建表之前,我尝试创建数据库中包含的数据的备份。我已经连接到服务器并创建文件来存储 SQL。问题是它在最后一行抛出错误。
“对象引用未设置到对象的实例”
我一整天都在这个项目中,所以可能会错过一些新的眼睛会有所帮助的东西。
这是代码:
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);
}
}
}
我正在做的事情的一些背景:
我想提取数据库中已存在的数据,然后更改实体模型。当应用程序重新启动时,EF 框架将删除并重新创建数据库,此时我将重新输入之前存在的数据。不太确定整个过程是否会成功,但这必须等到他们在 EF 中发布迁移功能为止。
希望有人能帮忙。谢谢
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.
Here's the code:
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的问题可能是 table.ToString()。尝试使用 table.Name 代替。
I think your problem is probably table.ToString(). Try using table.Name instead.
您打算如何恢复数据?我还没有首先使用 EF 代码,而且我对 SQL Server 非常熟悉,所以我肯定会选择使用 SSMS 或良好的 ol T-SQL 的解决方案如果我处于您的立场。
您所做的架构更改的性质是什么?您是否正在删除列、重新排列列或执行更复杂的操作?如果要删除列,则可以在 T-SQL 中相当简单地删除这些列。如果您要重新排列列,则必须将数据推送到临时(或标准/永久)表,进行更改,然后用脚本将更改返回。这也很容易做到。如果您扩展您的需求,也许有人可以帮助您找到一个简单的 sql 解决方案。
How are you planning on putting the data back? I have yet to use EF code first, and I am quite comfortable in SQL server, so I would definitely settle on a solution using either SSMS or good ol T-SQL if I were in your shoes.
What is the nature of the schema changes you are making? Are you dropping columns, rearranging columns, or something more complex? If you are dropping columns, those columns can be dropped in T-SQL rather trivially. If you are rearranging columns, then you will have to push the data to a temp (or standard/permanent) table, make your changes, then script the changes back. This is fairly easy to do as well. If you expand on your needs, perhaps someone can help you come to a simple sql solution.