15

つまり、VS2010でC#を使用して作成しているASP.NET Webサイト(Webアプリケーションではない)があります。私のマシンでは正常に動作しますが、ホストされているサイトにアップロードすると、コンパイルされず、次のようになります。「CS0246:タイプまたは名前空間の名前'DataAccess'が見つかりませんでした(usingディレクティブがありませんか?アセンブリリファレンス?)」

私はVSでWebサイトのコピー機能を使用してきましたが、自分のクラスを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

デフォルトでは、Webサイトは.csファイルをコンパイルしません。あなたはいくつかの異なることをする必要があるでしょう。

  1. そのクラスファイルのプロパティで、各クラスファイルを「コンパイル」するように設定します。このオプションを表示するには、クラスファイルをクリックし、プロパティエクスプローラウィンドウでプロパティを表示し、[ビルドアクション]ドロップダウンリストを[コンパイル]に変更します。

  2. 上記で問題が解決しない場合は、app_codeからクラスを削除し、ルートフォルダーにドロップします。

于 2010-10-27T22:38:41.450 に答える
6

app_codeフォルダーがWebアプリケーションのルートフォルダー(フォルダーまたは仮想フォルダーだけでなく)にありますか。Asp.NETはapp_codeフォルダーを認識できないようです。

Conentおよびコンパイルオプションは、WebApplicationプロジェクトでのみ使用できます。

于 2010-10-27T22:28:58.477 に答える
1

アカウントを開始する前にこの質問をしました。それ以外の場合は編集します。

次の後に同じエラーが発生します:

  1. WebアプリケーションではなくWebサイトがあることを確認します。したがって、.csファイルのコンパイル/コンテンツプロパティがありません。

  2. App_Codeフォルダの位置を確認しています。GridView.aspxをルートの下と独自のフォルダーに配置しようとしましたが、同じエラーが発生しました。\ \ App_Code DataAccess.cs \ App_Data \ GridView GridView.aspx Web.config

  3. クラスをApp_Codeからrootに移動します。同じエラー。

  4. この最後はうまくいくと思いましたが、うまくいきませんでした。Webサイトの公開機能を使用して、コンパイル済みのコードをアップロードしました。これで、binフォルダーにDataAccess.dllがあり、ルートにPrecompiledApp.configファイルがあることがわかりました。同じエラー。

更新:解決しました:サーバー上のフォルダー構造はローカルのものと似てましたが、すべてがサブフォルダーにありました...App_CodeとApp_Dataはサーバーのルートにある必要があります。

于 2010-10-28T02:18:44.197 に答える
0

アプリケーションをデプロイする前に、おそらくアプリケーションをコンパイルする必要があります。現時点では、ASP.NETには、.aspx/.ascxファイルに添付されていない.csファイルを知る方法がありません。Web配置プロジェクトを確認するか、VisualStudio2010の[公開]オプションを使用することをお勧めします。スコットガスリーは、私がここでこれまでにできたよりも良い要約を与えます。

于 2010-10-27T22:13:08.293 に答える