如何从数据库中获取字符串数据时修复system.outofmemoryexception异常? [英] how to fix system.outofmemoryexception exception when get string data from database?

查看:72
本文介绍了如何从数据库中获取字符串数据时修复system.outofmemoryexception异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码时出现此错误:



数据库db = sv.Databases [oldDatabase];



数据库newDatbase =新数据库(sv,newDatabase);

newDatbase.Create();



ScriptingOptions options = new ScriptingOptions();

StringBuilder sb = new StringBuilder();

options.ScriptData = true;

options.ScriptDrops = false;

options.ScriptSchema = true;

options.EnforceScriptingOptions = true;

options.Indexes = true;

options.IncludeHeaders = true;

options.WithDependencies = true;



TableCollection tables = db.Tables;



//conn.Open();

foreach(表中的Microsoft.SqlServer.Management.Smo.Table mytable) s)

{

foreach(db.Tables中的字符串行[mytable.Name] .EnumScript(选项))

{

sb.Append(行+\\\\ n);

}

}



string [] splitter = new string [] {\\\\\\\\\\ n};

string [] commandTexts = sb.ToString()。 Split(splitter,StringSplitOptions.RemoveEmptyEntries);

int indexx = 0;

foreach(commandTexts中的字符串命令)

{



indexx = command.IndexOf(SET);

string command2 = command.Insert(indexx,use+ newDatabase +\ n );

SqlCommand comm = new SqlCommand(command2,conn);

comm.ExecuteNonQuery();

}

//返回true;

string scriptresult1 = commandTexts [0] .ToString();

int index = scriptresult1.IndexOf(SET) ;

string scriptresult12 = scriptresult1.Insert(index,use+ newDatabase +\ n);



int res = sv.ConnectionContext.ExecuteNonQuery(scriptresult12);



返回scriptresult12;

}

sp时发生错误(字符串构建器)存储更多数据,以便如何解决??? div class =h2_lin>解决方案

所以...让我看看我是否能理解这一点。

您正在访问未知大小的数据库,并获取SQL以创建数据库中每个表的脚本,包括所有数据,标题,依赖项和索引?然后你将它构建成一个单独的StringBuilder?



当你内存不足时你会感到惊讶吗?

你做的意识到.NET中的任何单个对象都限制为2GB,不是吗?

首先检查发生了什么:使用调试器来查看StringBuilder在进行循环时有多大嵌套循环...


i get this error when i run this code :

Database db = sv.Databases["oldDatabase"];

Database newDatbase = new Database(sv, "newDatabase");
newDatbase.Create();

ScriptingOptions options = new ScriptingOptions();
StringBuilder sb = new StringBuilder();
options.ScriptData = true;
options.ScriptDrops = false;
options.ScriptSchema = true;
options.EnforceScriptingOptions = true;
options.Indexes = true;
options.IncludeHeaders = true;
options.WithDependencies = true;

TableCollection tables = db.Tables;

//conn.Open();
foreach (Microsoft.SqlServer.Management.Smo.Table mytable in tables)
{
foreach (string line in db.Tables[mytable.Name ].EnumScript(options))
{
sb.Append(line + "\r\n");
}
}

string[] splitter = new string[] { "\r\nGO\r\n" };
string[] commandTexts = sb.ToString().Split(splitter, StringSplitOptions.RemoveEmptyEntries);
int indexx = 0;
foreach (string command in commandTexts)
{

indexx = command.IndexOf("SET ");
string command2 = command.Insert(indexx, "use " + newDatabase + "\n");
SqlCommand comm = new SqlCommand(command2, conn);
comm.ExecuteNonQuery();
}
//return true ;
string scriptresult1 = commandTexts[0].ToString();
int index = scriptresult1.IndexOf("SET ");
string scriptresult12 = scriptresult1.Insert(index, "use " + newDatabase + "\n");

int res=sv.ConnectionContext.ExecuteNonQuery(scriptresult12);

return scriptresult12;
}
the error happen when sp(string builder) store more data so how can i fix it ??

解决方案

So...let me see if I can understand this.
You are accessing a database of unknown size, and getting SQL to create a script of each and every table in the DB, complete with all data, headings, dependencies, and indexes? And then you are building this into one, single StringBuilder?

And then you are surprised when you run out of memory?
You do realize that any single object in .NET is limited to 2GB, don't you?
Start by checking what is happening: use the debugger to look at how big the StringBuilder is getting as you go round that nested loop...


这篇关于如何从数据库中获取字符串数据时修复system.outofmemoryexception异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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