-1
***

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     
}`

4

1 回答 1

0

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

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

您看到此错误的原因是因为ExecuteNonQuery是一个实例方法 - 这意味着您需要一个实例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返回一个整数,其中包含受影响的行数;没有理由让它成为一个对象(至少我能想到)。

于 2014-01-11T08:19:36.150 回答