2012-11-05 20 views
2

我想要从if语句中的数据库调用中获取数据,然后我希望能够在页面后面的if语句之外使用该数据。C# - 我怎样才能声明一个变量,将得到填充数据库调用内部的if语句?

那么,为什么不工作?我能做些什么来完成这项工作?我刚刚收到错误消息“编译器错误消息:CS0103:名称'selectedData'在当前上下文中不存在”。我试图在if语句之外声明一个“selectedData”变量,以便它可用,但我似乎无法弄清楚如何正确执行该操作。

@{ 

    var testVariable = "blah"; 

    //set cache key and query based their being a craft name 
    if(testVariable.Length > 0){ 

     var db = Database.Open("Connection"); 
     var selectedData = db.Query("SELECT * FROM Products");    
    } 

} 


<div> 
    @foreach (var row in selectedData){ 
     @row.ContentTitle <br /> 
     @row.ContentShortDescription <br /> 
    } 
</div> 

回答

2

这是一个范围问题。你需要在if范围之外声明你的变量。

@{ 

    var testVariable = "blah"; 
    Type selectedData; 

    //set cache key and query based their being a craft name 
    if(testVariable.Length > 0){ 

     var db = Database.Open("Connection"); 
     selectedData = db.Query("SELECT * FROM Products");    
    } 

} 


<div> 
    @foreach (var row in selectedData){ 
     @row.ContentTitle <br /> 
     @row.ContentShortDescription <br /> 
    } 
</div> 

我不熟悉这里的类型,所以我用Type。在那里替换适当的类型。请注意,该代码片段在进入循环之前也不能确保selectedData被正确设置。你也应该处理。

+0

我想这确实是我的问题所在。我试着添加var selectedDate =“”;在那里你添加了行,但然后我得到以下错误“编译器错误消息:CS0029:不能隐式地将类型'System.Collections.Generic.IEnumerable '转换为'string'”。所以我想我不能使用“var”作为类型,但是它会是什么类型? – user1801249

+0

那么这是我在帖子中提到的..使用db.Query返回的类型。我不知道那是什么。看看文档。 –

+0

尝试用'IEnumerable selectedData = null;'。 – GmG

0

这是一个关于范围的问题。

基本上,在C#中,当您打开一组新的{}时,您声明了一个新的作用域。在该范围内创建变量,退出时会被销毁。这是一个相当简单的解释,不完全准确,但它很容易理解。

{ 
    var testVariable = "blah"; 

    //set cache key and query based their being a craft name 
    if(testVariable.Length > 0) 
    { 

     var db = Database.Open("Connection"); 
     // Create a new variable. 
     var selectedData = db.Query("SELECT * FROM Products");   
    } 
    // Variable doesn't exist anymore. 
} 

要解决这个问题:

{ 
    var testVariable = "blah"; 
    // Create variable outside the if scope 
    var selectedData = null; // Won't compile, compiler cannot find valid variable type. 

    //set cache key and query based their being a craft name 
    if(testVariable.Length > 0) 
    { 

     var db = Database.Open("Connection"); 
     // Assign a value to a variable 
     selectedData = db.Query("SELECT * FROM Products");   
    } 
    // Variable still exist! 
} 
// Here, variable would cease to exist. :(

但在这里,代码将无法编译,因为编译不知道是什么类型selectedData,因为我在其创建为它分配null。所以,让我们说selectedDataData类型:

{ 
    var testVariable = "blah"; 
    // Create variable outside the if scope 
    Data selectedData = null; // Now it compiles. :) 

    //set cache key and query based their being a craft name 
    if(testVariable.Length > 0) 
    { 

     var db = Database.Open("Connection"); 
     // Assign a value to a variable 
     selectedData = db.Query("SELECT * FROM Products");   
    } 
    // Variable still exist! 
} 
// Here, variable would cease to exist. :(

之后,你可以做if (selectedData != null)知道,如果数据被正确查询。

+1

您应该注意,这实际上不会编译。 –

+0

@JordanKaye:最有可能的。我仍然很难理解为什么人们在各处发送'var'。它只是更难跟踪变量类型。 – LightStriker

+0

这将是错误CS0818 - “隐式类型局部变量必须初始化” –

相关问题