这是错误:
非静态字段,方法或者需要对象引用 属性'ArchiCapture.Models.dbConnection.runSproc(string [], string [],string)'
它出现在调用dt = dbConnection.runSproc(paramName,paramValue,“pr_select_employee_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");
答案 0 :(得分:4)
您需要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
。