【问题标题】:The name "ABC..." does not exist in the current context. c# SQL Console program当前上下文中不存在名称“ABC ...”。 c# SQL控制台程序
【发布时间】:2016-03-25 08:51:39
【问题描述】:

我是新手。我正在制作一个 c# 控制台程序,该程序通过 datareader 读取 sql 数据库,如果有任何行与查询匹配,则显示一条语句,并通过 sql update 查询更新这些行。但我不知道如何解决这个错误。这是我的代码。帮助表示赞赏。谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;

namespace emailsend
{
    class Program
    {
        #region Connection String
        static private SqlConnection connection = new SqlConnection("Data Source=server;Initial Catalog=dbTrainning;User ID=user;Password=123");
        #endregion        

        static void Main(string[] args)
        {
            #region SQL Select Unsent Query
            string Sql = "SELECT * FROM People where Visits is Null;";
            SqlCommand cmd = new SqlCommand(Sql, connection);
            #endregion

            #region Open DB Connection
            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            } 
            #endregion
            SqlDataReader DR = cmd.ExecuteReader();

            #region Try Catch Block
            try
            {             
                while (DR.Read())
                {
                    #region Fetching DB data
                    DateTime TimeStamp = (DateTime)DR["ExceptionDate"];
                    string VNumber = (string)DR["VisitNumber"];
                    Console.Write("Total Visits = " +VNumber "\n");
                    #endregion
                    DR.Close();
                }

                cmd = new SqlCommand("UPDATE People SET Visits = '0' WHERE VisitNumber = '" + VNumber + "'", connection);   //The name 'VNumber' does not exist in the current context
                cmd.ExecuteNonQuery();
            }
            catch (SqlException exception)
            {
                Console.Write(exception);
            }
            finally
            {
                connection.Close();
            } 
            #endregion
        }
    }
}

【问题讨论】:

  • 如果您想访问在{ } 中声明的变量以访问您需要声明的那些变量,那么这里是使用调试器以及理解关键术语SCOPE 的地方在loop or try or if...等之外初始化variable(s)

标签: c# sql sql-server-2008 console


【解决方案1】:

这是因为您在while 循环中创建了VNumber,并且它在外部不可见。试试看:

try
{   
    string VNumber = null;      //outside the loop
    while (DR.Read())
    {
        #region Fetching DB data
        DateTime TimeStamp = (DateTime)DR["ExceptionDate"];
        VNumber = (string)DR["VisitNumber"];
        Console.Write("Total Visits = " +VNumber "\n");
        #endregion
        DR.Close();
    }

    cmd = new SqlCommand("UPDATE People SET Visits = '0' WHERE VisitNumber = '" + VNumber + "'", connection);   //The name 'VNumber' does not exist in the current context
    cmd.ExecuteNonQuery();
}

【讨论】:

  • 解决了小问题。我现在看到我引用的变量是在循环内创建和初始化的。它应该在类/方法的开头被初始化为全局变量
猜你喜欢
  • 1970-01-01
  • 2014-12-09
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多