我正在尝试根据变量创建DataReader
。我需要根据选择TextBoxes
中的哪个节点填充一系列TreeView
,并且父节点数据与子节点数据不同。所以,我写了这段代码:
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
的{{1}} / else
块之外。
我该如何做到这一点?
答案 0 :(得分: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()));
}
答案 1 :(得分:2)
来自MSDN:
如果在块构造(如If语句)中声明变量,则该变量的作用域仅在块结束之前。生命周期一直持续到程序结束。
问题是您在cmd
块的范围内定义了if-else
变量,因此在这些块之外不知道它。解决方案是在cmd
块中声明if-else
。像这样:
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);
....
}
}