0

MyProject.exe 中发生“System.NullReferenceException”类型的未处理异常
附加信息:对象引用未设置为对象的实例。

它说, StringCollection strCol 为空,但table.Script()不为空(它包括记录)。它只执行一次 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");
}
4

1 回答 1

0

我认为你需要做

foreach (var item in database.Tables)

而不是

foreach (var item in Sourceclb.Items)

并非所有项目Sourceclb.Items都是表格,一旦你点击一个不是的项目,database.Tables[item.ToString()]就会返回 null,随之而来的 NullReferenceException 在table.Script()

于 2013-12-02T15:04:58.567 回答