在响应中获得错误

时间:2013-01-16 06:19:09

标签: c# asp.net .net dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web;

namespace Database_Updation
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn1"].ToString());

            SqlCommand cmd = new SqlCommand("SELECT [GUID] FROM [Source].[dbo].[Source_User]", cn);
            cn.Open();
            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            rdr.Read();
            Response.Write(rdr[0].ToString()); //read a value

        }
    }
}

我收到错误The name 'Response' does not exist in the current context

3 个答案:

答案 0 :(得分:2)

尝试使用

Console.Write(rdr[0].ToString()); //Write a value 

而不是

Response.Write(rdr[0].ToString()); //Write a value

希望它有所帮助

答案 1 :(得分:1)

从你的代码来看它是控制台应用程序,而不是使用响应,还检查数据站的数据

if(rdr.Read())
  Console.Writeln(rdr[0].ToString());

响应是Web应用程序中使用的对象,如果你愿意,你将在asp.net web应用程序中获取此对象创建web应用程序而不是控制台应用程序...你需要阅读控制台和web asp.net之间的区别应用

答案 2 :(得分:1)

第一次检查参考System.Web.dll是否已添加 和 尝试 System.Web.HttpContext.Current.Response.Write(RDR [0]的ToString());