這裏的時候,收到錯誤的錯誤:調用公共類
An object reference is required for a non-static field, method, or property 'ArchiCapture.Models.dbConnection.runSproc(string[], string[], string)'
它出現在呼叫DT = dbConnection.runSproc(PARAMNAME,paramValue, 「pr_select_employee_by_id」);
我正在嘗試開發一個名爲SPROCs的類。這是我的班。
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");
[非靜態字段,方法或屬性需要對象引用](http://stackoverflow.com/questions/7016437/an-object-reference-is-required-for-the -non-static-field-method-or-property) – 2012-03-14 01:06:09
請不要命名以公開樣本小寫字母開頭的類。遵循[C#編碼準則](http://msdn.microsoft.com/zh-cn/library/ff926074.aspx)可使代碼更易於閱讀。 – 2012-03-14 01:08:00