非静态的字段,方法或属性要求对象引用如何解决?
2010-08-21 20:36
796 查看
写了一个公共类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;
}
}
附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;
}
}
相关文章推荐
- CS0120: 非静态的字段、方法或属性“System.Web.UI.Page.Request.get”要求对象引用的解决办法
- 错误“非静态的字段、方法或属性“System.Web.UI.Page.Response.get”要求对象引用”的解决办法
- 非静态的字段、方法或属性,要求对象引用
- 非静态的字段、方法或属性“……”要求对象引用
- 非静态的字段、方法或属性“System.Web.UI.Page.Session.get”要求对象引用
- 错误 10 非静态的字段、方法或属性“Test10.Program.a”要求对象引用
- 非静态的字段、方法或属性要求对象引用
- 这个问题不简单:非静态的字段、方法或属性“System.Web.UI.Page.Request.get”要求对象引用
- 非静态的字段、方法或属性“System.Web.UI.Page.ClientScript...”要求对象引用 (封装注册脚本)
- 非静态的字段、方法或属性“System.Web.UI.Page.ClientScript.get”要求对象引用
- 非静态的字段、方法或属性“System.Web.UI.Page.ClientScript.get”要求对象引用
- VS提示“非静态的字段、方法或属性要求对象引用”
- 错误:非静态的字段、方法或属性“System.Web.UI.Page.Session.get”要求对象引用
- 非静态的字段,方法或属性。。。。。。要求对象引用
- 非静态的字段、方法或属性……要求对象引用【學習】要求對象引用?
- 非静态的字段、方法或属性“System.Web.UI.Page.ClientScript.get”要求对象引用
- C#中 非静态字段、方法或属性“XXXX”要求对象引用-----解决方案
- 非静态的字段、方法或属性 要求对象引用
- .net 非静态的字段、方法或属性 要求对象引用
- 错误“非静态的字段、方法或属性“System.Web.UI.Page.Server.get”要求对象引用”