c# - 这段代码中的范围有什么问题?

标签 c# visual-studio-2008 scope

以下代码中的作用域有什么问题?

namespace t2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ////// string connectionString = "Data Source=A-63A9D4D7E7834\\SQLEXPRESS;Initial Catalog=Test1;Integrated Security=True";
            //connection string i taken from the file class1.cs static class.

            try
            {
                string commandString = "Select * from Table_1";

                SqlConnection conn = new SqlConnection(Class1.connection);
                SqlCommand command = new SqlCommand(commandString, conn);
                conn.Open();

                // Start a local transaction.
                SqlTransaction sqlTran = conn.BeginTransaction();
                command.Transaction = sqlTran;

                try
                {
                    SqlDataReader reader = command.ExecuteReader();
                    GridView1.DataSource = reader;
                    GridView1.DataBind();
                }//try ends here.
                catch (SqlException ex)
                {
                    //Console.WriteLine(ex.Message);
                    try
                    {
                        // Attempt to roll back the transaction.
                        sqlTran.Rollback();
                    }
                    catch (Exception exRollback)
                    {
                        // Throws an InvalidOperationException if the connection
                        // is closed or the transaction has already been rolled
                        // back on the server.
                        Response.Write(exRollback.Message.ToString());
                    }
                    //    <snipped> build error message
                }//sql catch ends here
                catch (Exception all)
                {
                    try
                    {
                        // Attempt to roll back the transaction.
                        sqlTran.Rollback();
                    }
                    catch (Exception exRollback)
                    {
                        // Throws an InvalidOperationException if the connection
                        // is closed or the transaction has already been rolled
                        // back on the server.
                        Response.Write(exRollback.Message.ToString());
                    }

                    Response.Write(all.Message.ToString());
                }//catch all ends
            }//main try
            finally
            {
                // Commit the transaction.
                sqlTran.Commit();
                reader.Close();
                reader.Dispose();
                conn.Close();
                conn.Dispose();
             }//finally ends
        }//page load method ends

显示的错误是:

Error   1   The name 'sqlTran' does not exist in the current context    D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 162 17  t2
Error   2   The name 'reader' does not exist in the current context D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 166 17  t2
Error   3   The name 'reader' does not exist in the current context D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 167 17  t2
Error   4   The name 'conn' does not exist in the current context   D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 169 17  t2

Error   5   The name 'conn' does not exist in the current context   D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 170 17  t2

最佳答案

这些变量在代码的 try 部分声明。在 catch {} 部分中,这些变量不存在。要解决这个问题,您需要在 try block 之外声明变量(将它们设置为 null),然后您可以在 try block 中实例化它们。请务必检查 catch block 中的空值。

关于c# - 这段代码中的范围有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3084587/

相关文章:

visual-studio-2008 - 德语Visual Studio 2008/ASP.NET中的英语错误消息

c++如何将类声明为文件的本地类

Java 输出变量作用域问题

c# - Mock 上的设置未返回预期值

windows - 开始 64 位 Windows 应用程序开发

c# - 如何自定义 MSI 安装程序中的错误屏幕?

javascript - 在 Javascript 中为循环声明声明 var

c# - 无法将类型 'Polly.CircuitBreaker.AsyncCircuitBreaker' 隐式转换为 'Polly.Policy'

c# - 使用 html-agility-pack 查找所有具有数据属性的元素

c# - 如何为 JSON 创建通用 DataContract 类