gpt4 book ai didi

c# - 基于变量创建 DataReader

转载 作者:太空宇宙 更新时间:2023-11-03 19:55:02 25 4
gpt4 key购买 nike

我正在尝试创建一个基于变量的DataReader。我需要根据选择了 TreeView 中的哪个节点来填充一系列 TextBoxes,并且父节点数据与子节点数据不同。所以,我写了这段代码:

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' doesn't exist in the current context.

我假设这是因为它在定义 cmdif/else block 之外。

我怎样才能让它工作?

最佳答案

您需要在 if 语句之外设置命令,否则该对象指针仅在该 if block 和 else block 中具有作用域。然后您可以在 block 中分配它。

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()));
}

关于c# - 基于变量创建 DataReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34638407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com