C# ASP.NET网站可以';在应用程序代码文件夹中看不到.cs文件
所以我有一个ASP.NET网站(不是web应用程序),我用C#在VS2010中创建。它在我的机器上运行良好,但当我将其上载到其承载的站点时,它将不会编译,并给出:“CS0246:找不到类型或命名空间名称‘DataAccess’(是否缺少using指令或程序集引用?)” 我一直在使用VS中的复制网站功能,在我想将自己的类放入App_Code文件夹并使用它之前,我没有遇到任何问题。我在其他答案中读到了关于将.cs属性更改为“Compile”而不是“Content”的内容,但是在文件的属性中没有这样的选项。。。仅文件名、完整路径和自定义工具。以下是.cs文件中的代码:C# ASP.NET网站可以';在应用程序代码文件夹中看不到.cs文件,c#,asp.net,app-code,C#,Asp.net,App Code,所以我有一个ASP.NET网站(不是web应用程序),我用C#在VS2010中创建。它在我的机器上运行良好,但当我将其上载到其承载的站点时,它将不会编译,并给出:“CS0246:找不到类型或命名空间名称‘DataAccess’(是否缺少using指令或程序集引用?)” 我一直在使用VS中的复制网站功能,在我想将自己的类放入App_Code文件夹并使用它之前,我没有遇到任何问题。我在其他答案中读到了关于将.cs属性更改为“Compile”而不是“Content”的内容,但是在文件的属性中没有这样的
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
使用系统;
使用系统数据;
使用System.Data.SqlClient;
使用System.Web.Configuration;
///
///提供对SQL Server数据库的访问。
///
///
公共类数据访问
{
//变量和公共属性***********************************
私有字符串connectionString=“”;
私有int记录计数=-1;
///
///属性:获取检索或更改的记录数
///
公共整数计数
{
得到
{
返回记录计数;
}
}
//类构造函数在初始化对象时执行***********
///
///初始化DataAccess需要web.config文件中的连接字符串名称
///
///web.config连接字符串的名称
公共数据访问(字符串连接名)
{
if(WebConfiguration Manager.ConnectionString[ConnectionName]==null){
抛出新异常(“找不到名为“”的连接字符串”+
web.config中的ConnectionName+“”);
}
//从web.config获取连接字符串。
connectionString=WebConfiguration Manager.connectionString[ConnectionName].connectionString;
}
///
///执行SELECT语句并在dataTable中返回结果
///
///选择SQL语句
///
公共数据表FillDataTable(字符串SQL)
{
SqlConnection _objConn=新的SqlConnection(connectionString);
SqlDataAdapter objAdapter=新的SqlDataAdapter(SQL,_objConn);
DataTable dt=新的DataTable();
试一试{
对象自适应填充(dt);
}
catch(SqlException-ex){
抛出新异常(“SQL中的错误:+SQL,ex”);
}
捕获(例外情况除外){
抛出ex;//冒泡异常到父类
}
最后{
_objConn.Close();
}
recordCount=dt.Rows.Count;
返回dt;
}
///
///执行“非查询”SQL语句(插入、更新、删除)
///
///插入、更新或删除
///受影响的记录数
public int ExecuteNonQuery(字符串SQL)
{
SqlConnection _objConn=新的SqlConnection(connectionString);
试一试{
_objConn.Open();
SqlCommand objCmd=新的SqlCommand(SQL,_objConn);
recordCount=objCmd.ExecuteNonQuery();
}
catch(SqlException-ex){
抛出新异常(“SQL中的错误:+SQL,ex”);
}
捕获(例外情况除外){
抛出新异常(例如Message);//将异常重新提交到父类
}
最后{u objConn.Close();}
返回记录计数;
}
public int ExecuteScalar(字符串SQL)
{
SqlConnection _objConn=新的SqlConnection(connectionString);
int intID;
试一试{
_objConn.Open();
SqlCommand objCmd=新的SqlCommand(SQL,_objConn);
intID=Convert.ToInt32(objCmd.ExecuteScalar());
}
catch(SqlException-ex){
抛出新异常(“SQL中的错误:+SQL,ex”);
}
捕获(例外情况除外){
抛出新异常(例如Message);//将异常重新提交到父类
}
最后{u objConn.Close();}
返回intID;
}
}//末级
从我的页面:
<%@ 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>
受保护的无效页面加载(对象发送方、事件参数e)
{
//初始化数据访问类
DataAccess myDA=新的DataAccess(“A05客户”);
//填充dataTable并绑定到GridView控件
string strSQL=“从tblCustomers中选择*”;
gvCustomers.DataSource=myDA.FillDataTable(strSQL);
gvCustomers.DataBind();
}
网格视图
网格视图
谢谢你的帮助 您可能需要在部署应用程序之前编译它。目前ASP.NET无法了解未附加到.aspx/.ascx文件的.cs文件。我建议查看Web部署项目或使用VisualStudio2010“发布”选项。Scott Guthrie提供了比我更好的摘要。您是否将app_code文件夹作为web应用程序的根文件夹(不仅仅是文件夹或虚拟文件夹)。Asp.NET似乎看不到应用程序代码文件夹
Conent和compile选项仅在WebApplication project中可用。默认情况下,网站不编译.cs文件。你需要做一些不同的事情
我在创建帐户之前问了这个问题,否则我会编辑它 在以下情况下,我出现了相同的错误: