调用公共类时收到错误

本文关键字:错误 调用 | 更新日期: 2023-09-27 18:27:47

以下是错误:

非静态字段、方法或属性'ArchiCapture.Models.dbConnection.runSproc(string[],string[],string)'

它出现在调用dt=dbConnection.runSproc(paramName,paramValue,"pr_select_employ_by_id")时;

我正在尝试开发一个称为SPROC的类。这是我的课。

namespace ArchiCapture.Models
{
    public class dbConnection
    {
    public DataTable runSproc(string[] paramName, string[] paramValue, string sproc)
    {
            SqlConnection conn = null;
            DataTable dt = new DataTable();
            SqlDataReader reader = null;
            try
            {
                string connStr = ConfigurationManager.ConnectionStrings["AC2012"].ConnectionString;
                using (conn = new SqlConnection(connStr))
                {
                    SqlCommand cmd = new SqlCommand(sproc, conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    for (int x = 0; x < paramName.Count(); x++)
                    {
                        cmd.Parameters.Add(new SqlParameter(paramName[x], paramValue[x]));
                    }
                    conn.Open();
                    reader = cmd.ExecuteReader();
                    dt.Load(reader);
                    return dt;
                }
            }
            catch (SqlException e)
            {
                throw (e);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        finally
        {
            if (conn != null)
            {
                conn.Close();
            }
            if (reader != null)
            {
                reader.Close();
            }
        } 
    }
}
}

我是这样称呼它的…

                    DataTable dt = new DataTable();
                string[] paramName = new string[1] { "@employee_id" };
                string[] paramValue = new string[1] { searchEngine.Value };

                dt = dbConnection.runSproc(paramName, paramValue, "pr_select_employee_by_id");

调用公共类时收到错误

您想要一个static方法:

public static DataTable runSproc(string[] paramName, string[] paramValue, string sproc)

或者,正如错误消息所述,在调用实例方法之前,必须实例化类的新实例:

dbConnection conn = new dbConnection();
dt = conn.runSproc(paramName, paramValue, "pr_select_employee_by_id");

此外,类名通常是PascalCased。考虑DbConnection而不是dbConnection