[英]NullReference Exception was unhandled in foreach
MyProject.exe中发生了'System.NullReferenceException'类型的未处理异常
附加信息:对象引用未设置为对象的实例。
它说, StringCollection strCol为null,但table.Script()不为null(包括记录)。 它仅执行一次foreach。 第二次出现此异常。 这是我的代码:
foreach (var item in Sourceclb.Items)
{
Table table = database.Tables[item.ToString()];
StringCollection strCol = table.Script();//Gives exception here
var script = "";
foreach (var key in strCol)
{
script += key;
}
command.Connection = ttbl;
command.CommandText = "USE "+_hedefDb+" \n EXEC sp_sqlexec '"+scriptdondur(script)+ "'";
command.ExecuteNonQuery();
txtLog.AppendText("*TABLE COPIED* "+item.ToString()+" has been copied. \r\n");
}
我认为你需要做
foreach (var item in database.Tables)
而不是
foreach (var item in Sourceclb.Items)
并非Sourceclb.Items
所有项目Sourceclb.Items
将是表,一旦您击中不存在的项目, database.Tables[item.ToString()]
将返回null,并因此在table.Script()
处返回NullReferenceException。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.