15

所以我有一个 ASP.NET 网站(不是 Web 应用程序),我在 VS2010 中使用 C# 制作。它在我的机器上运行良好,但是当我将它上传到它所在的站点时,它不会编译,给出:“CS0246:找不到类型或命名空间名称'DataAccess'(您是否缺少 using 指令或装配参考?)”

我一直在使用 VS 中的复制网站功能,直到我想将自己的类放在 App_Code 文件夹中并使用它之前都没有问题。我在其他答案中阅读了有关将 .cs 属性更改为“编译”而不是“内容”的信息,但在文件的属性中没有适合我的选项......只有文件名、完整路径和自定义工具。这是 .cs 文件中的代码:

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>

谢谢你的帮助!

4

4 回答 4

21

默认情况下,网站不编译 .cs 文件。您将需要做一些不同的事情。

  1. 在该类文件的属性中将每个类文件设置为“编译”。您可以通过单击类文件、在属性浏览器窗口中查看属性并将 Build Action 下拉列表更改为“Compile”来查看此选项。

  2. 如果上述方法没有帮助,请从 app_code 中删除该类并将其放入根文件夹中。

于 2010-10-27T22:38:41.450 回答
6

您是否有 app_code 文件夹,它是 Web 应用程序的根文件夹(不仅是文件夹或虚拟文件夹)。似乎 Asp.NET 看不到 app_code 文件夹。

内容和编译选项仅在 WebApplication 项目中可用。

于 2010-10-27T22:28:58.477 回答
1

我在开始帐户之前问了这个问题,否则我会编辑它。

之后我有同样的错误:

  1. 检查我有一个网站,而不是 Web 应用程序,因此没有 .cs 文件的编译/内容属性。

  2. 检查 App_Code 文件夹的位置。我尝试将 GridView.aspx 放在根目录下以及它自己的文件夹中,同样的错误。\ \App_Code DataAccess.cs \App_Data \GridView GridView.aspx Web.config

  3. 将类从 App_Code 移动到根目录。同样的错误。

  4. 这最后一个我认为会工作,但没有。使用发布网站功能上传预编译代码。我可以看到它现在是 bin 文件夹中的 DataAccess.dll,并且根目录中有一个 PrecompiledApp.config 文件。同样的错误。

更新:已解决:服务器上的文件夹结构与本地文件夹结构类似,我将所有内容都放在子文件夹中... App_Code 和 App_Data 必须位于服务器的根目录中。

于 2010-10-28T02:18:44.197 回答
0

您可能需要在部署应用程序之前对其进行编译。目前 ASP.NET 无法知道未附加到 .aspx/.ascx 文件的 .cs 文件。我建议查看 Web 部署项目或使用 Visual Studio 2010“发布”选项。Scott Guthrie 给出了比我在这里更好的总结。

于 2010-10-27T22:13:08.293 回答