1. 程式人生 > >非靜態的欄位,方法或屬性要求物件引用如何解決?

非靜態的欄位,方法或屬性要求物件引用如何解決?

寫了一個公共類DBhelper.cs,但是在引用的時候出現““ 非靜態的欄位、方法或屬性“DBhelper.ExecuteNonQuery(STRING,System.Data.SqlClient.SqlParameter[])”要求物件引用””,應該怎麼樣解決呢?

附DBhelper.cs程式碼如下:
 public class DBhelper
    {
        private SqlConnection connection;
        private SqlCommand command;
        private SqlDataAdapter adapter;
        public DBhelper()
        {
            this.connection = new SqlConnection(ConfigurationManager.ConnectionStrings["db_EShopConnectionString"].ConnectionString);
            this.command = new SqlCommand();
            this.command.Connection = this.connection;
            this.adapter = new SqlDataAdapter(command);
        }

        public int ExecuteNonQuery(string sql)
        {
            return this.ExecuteNonQuery(sql, new SqlParameter[0]);
        }

        public int ExecuteNonQuery(string sql, SqlParameter[] param)
        {
            this.command.CommandText = sql;
            this.command.Parameters.Clear();
            for (int i = 0; i < param.Length; i++)
            {
                if (param[i] != null)
                {
                    this.command.Parameters.Add(param[i]);

                }
                int result = 0;
                try
                {
                    this.connection.Open();
                    result = this.command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    this.connection.Close();
                }
                return result;
            }
            return 0;
        }

        public DataTable Select(string sql, SqlParameter[] param)
        {
            this.command.CommandText = sql;
            this.command.Parameters.Clear();
            for (int i = 0; i < param.Length; i++)
            {
                if (param[i] != null)
                {
                    this.command.Parameters.Add(param[i]);

                }
                DataTable dtData = new DataTable();
                try
                {
                    this.adapter.Fill(dtData);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return dtData;
            }
            return null;
        }

        public DataTable Select(string sql)
        {
            return this.Select(sql, new SqlParameter[0]);
        }

        public static SqlParameter[] AddParameter(SqlParameter[] paramArray, SqlParameter param)
        {
            Array.Resize<SqlParameter>(ref paramArray, paramArray.Length + 1);
            return paramArray;
        }
    }