2016-01-06 126 views
1

我正在嘗試基於變量創建DataReader。我需要根據TreeView中的哪個節點被選中來填充一系列TextBoxes,並且父節點數據與子節點數據不同。所以,我寫這個代碼:基於變量創建DataReader

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PBRConnectionString"].ConnectionString)) 
{ 
    try 
    { 
     conn.Open(); 
     if (TreeViewAccts.SelectedNode.Parent == null) 
     { 
      // At the parent level, we filter by Account Group ID 
      SqlCommand cmd = new SqlCommand(@"Select [ACCT_GRP_PK], [ACCT_GRP], 'X' as [DOS], [ACTIVE_FLG], [LOAD_BY], [LOAD_TIMESTAMP], [UPDT_BY], [UPDT_TIMESTAMP], [A_FLG_UPDT_BY], [A_FLG_UPDT_TIMESTAMP] from [ACCT_GRP_LIST] where [ACCT_GRP_PK] = @AcctID ORDER BY [ACCT_GRP] ASC", conn); 

      cmd.Parameters.AddWithValue("@AcctID", Convert.ToInt32(TreeViewAccts.SelectedValue.ToString())); 
     } 
     else 
     { 
      // At the child level, we filter by Account ID 
      SqlCommand cmd = new SqlCommand(@"Select [ACCT_PK], [ACCT_NUM], [DOS], [ACTIVE_FLG], [LOAD_BY], [LOAD_TIMESTAMP], [UPDT_BY], [UPDT_TIMESTAMP], [A_FLG_UPDT_BY], [A_FLG_UPDT_TIMESTAMP] from [ACCT_LIST] where [ACCT_PK] = @AcctID ORDER BY [ACCT_NUM] ASC", conn); 

      cmd.Parameters.AddWithValue("@AcctID", Convert.ToInt32(TreeViewAccts.SelectedValue.ToString())); 
     } 

     //Here is the trouble 
     using (SqlDataReader reader = cmd.ExecuteReader()) 
     while (reader.Read()) 
     { 
      txtID.Text = reader.GetByte(0).ToString(); 
      txtName.Text = reader.GetByte(1).ToString(); 
      txtDOS.Text = reader.GetByte(2).ToString(); 
      txtFlag.Text = reader.GetByte(3).ToString(); 
      txtLoadedBy.Text = reader.GetByte(4).ToString(); 
      txtLoadedOn.Text = reader.GetByte(5).ToString(); 
     } 

的問題是,在這條線:

using (SqlDataReader reader = cmd.ExecuteReader()) 

它告訴我,

'加利福尼亞' 不存在的當前上下文。

我假設它是因爲它是在那裏cmd定義的if/else塊之外。

我該如何做這項工作?

回答

3

您需要在if語句之外設置該命令,否則該對象指針僅在if塊中和else塊中具有作用域。然後您可以在塊中分配它。

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PBRConnectionString"].ConnectionString)) 
{ 
    try 
    { 
     conn.Open(); 
     SqlCommand cmd = null; // declare it here 
     if (TreeViewAccts.SelectedNode.Parent == null) 
     { 
      // At the parent level, we filter by Account Group ID 
      cmd = new SqlCommand(@"Select [ACCT_GRP_PK], [ACCT_GRP], 'X' as [DOS], [ACTIVE_FLG], [LOAD_BY], [LOAD_TIMESTAMP], [UPDT_BY], [UPDT_TIMESTAMP], [A_FLG_UPDT_BY], [A_FLG_UPDT_TIMESTAMP] from [ACCT_GRP_LIST] where [ACCT_GRP_PK] = @AcctID ORDER BY [ACCT_GRP] ASC", conn); 

      cmd.Parameters.AddWithValue("@AcctID", Convert.ToInt32(TreeViewAccts.SelectedValue.ToString())); 
     } 
     else 
     { 
      // At the child level, we filter by Account ID 
      cmd = new SqlCommand(@"Select [ACCT_PK], [ACCT_NUM], [DOS], [ACTIVE_FLG], [LOAD_BY], [LOAD_TIMESTAMP], [UPDT_BY], [UPDT_TIMESTAMP], [A_FLG_UPDT_BY], [A_FLG_UPDT_TIMESTAMP] from [ACCT_LIST] where [ACCT_PK] = @AcctID ORDER BY [ACCT_NUM] ASC", conn); 

      cmd.Parameters.AddWithValue("@AcctID", Convert.ToInt32(TreeViewAccts.SelectedValue.ToString())); 
     } 
2

MSDN

如果你聲明塊結構中的變量,如if語句,該變量的作用域只有等到塊的結尾。終身是直到程序結束。

的問題是,你在你的if-else塊的範圍內定義的變量cmd,所以它不是那些塊以外爲人所知。解決方法是聲明if-else塊中的cmd。像這樣:

try 
{ 
    SqlCommand cmd = null; 
    .... 
    if (TreeViewAccts.SelectedNode.Parent == null) 
    { 
     cmd = new SqlCommand(@"Select [ACCT_GRP_PK], [ACCT_GRP], 'X' as [DOS], [ACTIVE_FLG], [LOAD_BY], [LOAD_TIMESTAMP], [UPDT_BY], [UPDT_TIMESTAMP], [A_FLG_UPDT_BY], [A_FLG_UPDT_TIMESTAMP] from [ACCT_GRP_LIST] where [ACCT_GRP_PK] = @AcctID ORDER BY [ACCT_GRP] ASC", conn); 
     .... 
    } 
    else 
    { 
     // At the child level, we filter by Account ID 
     cmd = new SqlCommand(@"Select [ACCT_PK], [ACCT_NUM], [DOS], [ACTIVE_FLG], [LOAD_BY], [LOAD_TIMESTAMP], [UPDT_BY], [UPDT_TIMESTAMP], [A_FLG_UPDT_BY], [A_FLG_UPDT_TIMESTAMP] from [ACCT_LIST] where [ACCT_PK] = @AcctID ORDER BY [ACCT_NUM] ASC", conn); 
     .... 
    } 
}