ASP.NET网站无法看到APP_ code文件夹cs文件 [英] ASP.NET web site can't see .cs file in App_Code folder

查看:182
本文介绍了ASP.NET网站无法看到APP_ code文件夹cs文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个ASP.NET网站(不是Web应用程序)我做在VS2010用C#。它运行良好我的机器上,但是当我把它上传到其托管的网站,它不会编译,赠送:CS0246:类型或命名空间名称数据访问找不到(是否缺少using指令或程序集引用?)

So I have an ASP.NET web site (not web application) I'm making in VS2010 with C#. It runs fine on my machine, but when I upload it to the site it's hosted on, it won't compile, giving: "CS0246: The type or namespace name 'DataAccess' could not be found (are you missing a using directive or an assembly reference?)"

我一直使用VS复制网站功能,并没有任何问题,直到我想把我自己的类中APP_ code文件夹,并使用它。我读了有关更改的.cs属性编译,而不是内容其他的答案,但在文件的属性有没有这样的选择对我来说...唯一的文件名,完整路径和自定义工具。下面是来自cs文件的code:

I've been using the copy web site feature in VS and had no problems until I wanted to put my own class in the App_Code folder and use it. I read in other answers about changing the .cs properties to "Compile" instead of "Content", but there's no such option for me in the properties of the file... only File Name, Full Path, and Custom Tool. Here's the code from the .cs file:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

/// <summary>
/// Provides access to SQL Server database. 
/// </summary>
/// 
public class DataAccess
{
    //Variables & public properties ***********************************
    private string connectionString = "";
    private int recordCount = -1;

    /// <summary>
    /// Property: gets count of records retrieved or changed 
    /// </summary>
    public int Count
    {
        get
        {
            return recordCount;
        }
    }

    //Class constructor is executed when object is initialized ***********
    /// <summary>
    /// Connection string name in web.config file is required to initialize DataAccess
    /// </summary>
    /// <param name="ConnectionName">Name of web.config connection string</param>
    public DataAccess(string ConnectionName)
    {
        if (WebConfigurationManager.ConnectionStrings[ConnectionName] == null) {
            throw new Exception("Cannot find connection string named '" +
               ConnectionName + "' in web.config");
        }
        //Get connection string from web.config.
        connectionString = WebConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString;
    }
    /// <summary>
    /// Executes SELECT statement and returns results in dataTable
    /// </summary>
    /// <param name="SQL">Select SQL statement</param>
    /// <returns></returns>
    public DataTable FillDataTable(string SQL)
    {
        SqlConnection _objConn = new SqlConnection(connectionString);
        SqlDataAdapter objAdapter = new SqlDataAdapter(SQL, _objConn);
        DataTable dt = new DataTable();
        try {
            objAdapter.Fill(dt);
        }
        catch (SqlException ex) {
            throw new Exception("Error in SQL:" + SQL, ex);
        }
        catch (Exception ex) {
            throw ex; //Bubbling exception up to parent class
        }
        finally {
            _objConn.Close();
        }

        recordCount = dt.Rows.Count;
        return dt;
    }

    /// <summary>
    /// Executes "non-query" SQL statements (insert, update, delete)
    /// </summary>
    /// <param name="SQL">insert, update or delete</param>
    /// <returns>Number of records affected</returns>
    public int ExecuteNonQuery(string SQL)
    {
        SqlConnection _objConn = new SqlConnection(connectionString);
        try {
            _objConn.Open();
            SqlCommand objCmd = new SqlCommand(SQL, _objConn);
            recordCount = objCmd.ExecuteNonQuery();
        }
        catch (SqlException ex) {
            throw new Exception("Error in SQL:" + SQL, ex);
        }
        catch (Exception ex) {
            throw new Exception(ex.Message); //Rethrowing exception up to parent class
        }
        finally { _objConn.Close(); }

        return recordCount;
    }

    public int ExecuteScalar(String SQL)
    {
        SqlConnection _objConn = new SqlConnection(connectionString);
        int intID;
        try {
            _objConn.Open();
            SqlCommand objCmd = new SqlCommand(SQL, _objConn);
            intID = Convert.ToInt32(objCmd.ExecuteScalar());
        }
        catch (SqlException ex) {
            throw new Exception("Error in SQL:" + SQL, ex);
        }
        catch (Exception ex) {
            throw new Exception(ex.Message); //Rethrowing exception up to parent class
        }
        finally { _objConn.Close(); }
        return intID;
    }

}//end class

从我的页面:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

 protected void Page_Load(object sender, EventArgs e)
 {
  //Initialize dataAccess class
  DataAccess myDA = new DataAccess("A05Customers");

  //Populate dataTable and bind to GridView Control
  string strSQL = "Select * from tblCustomers";

  gvCustomers.DataSource = myDA.FillDataTable(strSQL);
  gvCustomers.DataBind();
 }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center" style="font-family: Verdana">
  <h2>GridView</h2>
  <hr style="color: #0000FF" width="800" />
  <br />
  <asp:GridView ID="gvCustomers" runat="server" BackColor="#FFFFCC" BorderColor="#336699" BorderStyle="Inset" BorderWidth="5px" CellPadding="2" CellSpacing="4" />
  <br />
 </div>
    </form>
</body>
</html>

感谢您的帮助!

推荐答案

默认情况下的网站不进行编译的.cs文件。你需要做一些不同的事情。

By default websites don't compile .cs files. You will need to do a few different things.


  1. 设置每个类文件中的该类文件的属性为编译。您可以通过单击类文件,查看属性中的性能资源管理器窗口,并更改生成操作下拉列表中看到这个选项编译。

  1. Set each class file to "compile" in the properties for that class file. You can see this option by clicking the class file, viewing the properties in the properties explorer window, and changing the Build Action dropdown list to "Compile."

如果上面没有帮助,从APP_ code删除类,并在根文件夹中删除它。

If the above doesn't help, remove the class from the app_code and drop it in the root folder.

这篇关于ASP.NET网站无法看到APP_ code文件夹cs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆