非静态字段、方法或属性“Competition.DataLayer”需要对象引用

Posted

技术标签:

【中文标题】非静态字段、方法或属性“Competition.DataLayer”需要对象引用【英文标题】:An object reference is required for the non-static field, method, or property 'Competition.DataLayer 【发布时间】:2014-01-30 08:30:48 【问题描述】:
***

namespace Competition.DataLayer 


    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.htmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public class DataManager
    

        public  DataManager()
             
        

        private SqlConnection OpenConnection()
        
            SqlConnection oCon = new SqlConnection("competition_str");
            oCon.Open();
            return oCon;
        

        private  void CloseConnection(SqlConnection oCon)
        
            if (oCon != null)
            
                oCon.Close();
                oCon.Dispose();
            
        

        public object ExecuteNonQuery(string sSQLText, SqlParameter [] oParam)
        
            SqlConnection oCn = OpenConnection();
            SqlCommand cmd = new SqlCommand();//sSQLText, oCn);
            cmd.Connection = oCn;
            cmd.CommandText = sSQLText;
            if (oParam != null && oParam.Length > 0)
             
                foreach(SqlParameter oOneParameter in oParam)
                
                    cmd.Parameters.Add(oOneParameter);
                
            
            object oRetVal = cmd.ExecuteNonQuery();
            CloseConnection(oCn);
            return oRetVal;
        

     

业务逻辑层代码-

namespace Competition.BusinessLogicLayer 
    
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using Competition.DataLayer;

    #region "variables"
    public class Candidate
       

        //#region "variables"
        private string sName;
        private string sAddress;
        private string sState;
        private string sCity;
        private string sFName;
        private float fWeight;
        private float fHight;
        private string sQualification;
        private string sAssoclubName;
        private string sSeniorName;
        private string sNoc;
        private string sComments;
        private DateTime dtDob;
        private int iAddedBYId;

        public Candidate()
        
        

        public string Name
        
            get  return sName; 
            set  sName = value; 
        

        public string Address
        
            get  return sAddress; 
            set  sAddress = value; 
        

        public string State
        
            get  return sState; 
            set  sState = value; 
        

        public string City
        
            get  return sCity; 
            set  sCity = value; 
        

        public string FName
        
            get  return sFName; 
            set  sFName = value; 
        

        public float Weight
        
            get  return fWeight; 
            set  fWeight = value; 
        

        public float Height
        
            get  return fHight; 
            set  fHight = value; 
        

        public string Qualification
        
            get  return sQualification; 
            set  sQualification = value; 
        

        public string AssoclubName
        
            get  return sAssoclubName; 
            set  sAssoclubName = value; 
        

        public string SeniorName
        
            get  return sSeniorName; 
            set  sSeniorName = value; 
        

        public string IsNoc
        
            get  return sNoc; 
            set  sNoc = value; 
        

        public string Comments
        
            get  return sComments; 
            set  sComments = value; 
        

        public DateTime Dob
        
            get  return dtDob; 
            set  dtDob = value; 
        

        public int AddedBYId
        
            get  return iAddedBYId; 
            set  iAddedBYId = value; 
        

        #region "Function"

        public int InsertCandidate()
        
            SqlParameter[] oParam =
            
               new SqlParameter("@prm_name",sName),
               new SqlParameter("@prm_dob",dtDob),
               new SqlParameter("@prm_fname",sFName),
               new SqlParameter("@prm_state",sState),
               new SqlParameter("@prm_city",sCity),      

            new SqlParameter("@prm_address",sAddress),
            new SqlParameter("@prm_weight",fWeight),
            new SqlParameter("@prm_height",fHight),
            new SqlParameter("@prm_qualification",sQualification),
            new SqlParameter("@prm_associateclubname",sAssoclubName),

            new SqlParameter("@prm_seniorname",sSeniorName),
             new SqlParameter("@prm_noc ",sNoc),
              new SqlParameter(" @prm_comments",sComments),
               new SqlParameter("@prm_addedbyid",iAddedBYId)

            ;
            int iSuccessful = DataManager.ExecuteNonQuery("p_cand_insert", oParam);

        
        #endregion

    
       #endregion     
`

【问题讨论】:

我猜错误在这一行:nt iSuccessful = DataManager.....。错误消息告诉您问题所在 - 您正尝试访问 DataManager 类的实例方法,就好像它是 static。创建类的实例,然后调用方法:DataMangaer dm = new DataManager(); int iSuccessful = dm.ExeucteNonQuery(.... 【参考方案1】:

假设给出错误的行是这样的:

int iSuccessful = DataManager.ExecuteNonQuery("p_cand_insert", oParam);

您看到此错误的原因是因为 ExecuteNonQuery 是一个 instance 方法 - 这意味着您需要一个 DataManager 的实例来调用该方法。

你可以这样做:

DataManager dm = new DataManager();
int iSuccessful = dm.ExecuteNonQuery("p_cand_insert", oParam);

或者,您可以将方法标记为static

public static object ExecuteNonQuery(string sSQLText, SqlParameter [] oParam)

然后这样称呼它:

int iSuccessful = DataManager.ExecuteNonQuery("p_can_insert", oParam);

但是,请注意您的方法返回一个object,而您正试图将其分配给int。我会更改方法以返回int(您现在拥有它的方式可能会引发错误):

public int ExecuteNonQuery(string sSQLText, SqlParameter [] oParam)


    SqlConnection oCn = OpenConnection();
    SqlCommand cmd = new SqlCommand();//sSQLText, oCn);
    cmd.Connection = oCn;
    cmd.CommandText = sSQLText;

    if (oParam != null && oParam.Length > 0)
     
        foreach (SqlParameter oOneParameter in oParam)
        
            cmd.Parameters.Add(oOneParameter);
        
    

    int retVal = cmd.ExecuteNonQuery();
    CloseConnection(oCn);

    return retVal;

SqlCommand.ExecuteNonQuery 返回一个整数,其中包含受影响的行数;没有理由让它成为一个对象(至少我能想到)。

【讨论】:

以上是关于非静态字段、方法或属性“Competition.DataLayer”需要对象引用的主要内容,如果未能解决你的问题,请参考以下文章

字段初始值设定项无法引用非静态字段、方法或属性

字段初始值无法引用非静态字段、方法或属性

C#非静态的字段、方法或属性要求对象引用

C# 字段初始值设定项无法引用非静态字段、方法或属性

C# 字段初始值设定项无法引用非静态字段、方法或属性

C# 字段初始值无法引用非静态字段、方法或属性