0

我已经尝试解决这个问题好几个小时了,我知道我在这里有一些小错误,但我无法查明它。请帮我。所以我创建了一个名为“clsDataLayer.cs”的类,它位于 App_Code asp.net 文件夹中。然后我创建了一个名为“frmUserActivity”的表单,但是现在当我在其中发布代码并调用该类时,它显示“当前上下文中不存在“clsDataLayer””有人可以帮帮我吗?

clsDataLayer 的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
// Add your comments here
using System.Data.OleDb;
using System.Net;
using System.Data;

namespace ASP_PayRoll.App_Code
{
    public class clsDataLayer
    {
        // This function gets the user activity from the tblUserActivity
        public static dsUserActivity GetUserActivity(string Database)
        {
            // Add your comments here
            dsUserActivity DS;
            OleDbConnection sqlConn;
            OleDbDataAdapter sqlDA;

            // Add your comments here
            sqlConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + Database);

            // Add your comments here
            sqlDA = new OleDbDataAdapter("select * from tblUserActivity", sqlConn);

            // Add your comments here
            DS = new dsUserActivity();

            // Add your comments here
            sqlDA.Fill(DS.tblUserActivity);

            // Add your comments here
            return DS;
        }

        // This function saves the user activity
        public static void SaveUserActivity(string Database, string FormAccessed)
        {
            // Add your comments here
            OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + Database);
            conn.Open();
            OleDbCommand command = conn.CreateCommand();
            string strSQL;

            strSQL = "Insert into tblUserActivity (UserIP, FormAccessed) values ('" +
                GetIP4Address() + "', '" + FormAccessed + "')";

            command.CommandType = CommandType.Text;
            command.CommandText = strSQL;
            command.ExecuteNonQuery();
            conn.Close();
        }

        // This function gets the IP Address
        public static string GetIP4Address()
        {
            string IP4Address = string.Empty;

            foreach (IPAddress IPA in
                        Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != string.Empty)
            {
                return IP4Address;
            }

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            return IP4Address;
        }
    }
}

frmUserActivity.aspx.cs 的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASP_PayRoll
{
    public partial class frmUserActivity : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Declare the DataSet
                dsUserActivity myDataSet = new dsUserActivity();

                //Fill the dataset with what is returned from the function
                myDataSet = clsDataLayer.GetUserActivity(Server.MapPath("PayrollSystem_DB.mdb"));

                //Sets the DataGrip to the DataSource based on the table
                grdUserActivity.DataSource = myDataSet.Tables["tblUserActivity"];

                //Binds the DataGrid
                grdUserActivity.DataBind();
            }
        }
    }
}

提前致谢。阿克

4

2 回答 2

3

使用ASP_PayRoll.App_Code.clsDataLayer. 您的数据层类中的命名空间是ASP_PayRoll.App_Code,在您的页面中是ASP_PayRoll.

于 2013-06-06T19:48:21.400 回答
1

添加using ASP_PayRoll.App_Code;到您的 aspx 文件中。

由于您在除您的 aspx 页面之外的命名空间中声明了这一点,因此您必须使用 using 语句或整个路径 ( ASP_PayRoll.App_Code.clsDataLayer) 才能访问该类。

于 2013-06-06T19:51:30.437 回答