.....
using System.Data.SqlClient;
/// <summary>
/// DbObject 的摘要说明
/// </summary>
///
namespace Shuihuren//这里是我写的 以后用的
public SqlConnection Conn
{
get
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
return conn;
}
} public void Open()
{
SqlConnection conn = this.Conn;
conn.Open();
}
public int RunSQL(String sql)
{
SqlCommand cmd = new SqlCommand(sql);
this.Open();
cmd.Connection = Conn;
int a = cmd.ExecuteNonQuery();
return a;
}
}//页面代码
.....
using Shuihuren;;... protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DBClass db = new DBClass();
string id = GridView1.DataKeys[e.RowIndex].Values.ToString();
string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text; string sql = "update product set name='"+name+"' where id="+id+" ";
int a = db.RunSQL(sql);
GridView1.EditIndex = -1;
GridViewBind();
}这样为什么说 ExecuteNonQuery 要求已打开且可用的连接。连接的当前状态为已关闭。
using System.Data.SqlClient;
/// <summary>
/// DbObject 的摘要说明
/// </summary>
///
namespace Shuihuren//这里是我写的 以后用的
public SqlConnection Conn
{
get
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
return conn;
}
} public void Open()
{
SqlConnection conn = this.Conn;
conn.Open();
}
public int RunSQL(String sql)
{
SqlCommand cmd = new SqlCommand(sql);
this.Open();
cmd.Connection = Conn;
int a = cmd.ExecuteNonQuery();
return a;
}
}//页面代码
.....
using Shuihuren;;... protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DBClass db = new DBClass();
string id = GridView1.DataKeys[e.RowIndex].Values.ToString();
string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text; string sql = "update product set name='"+name+"' where id="+id+" ";
int a = db.RunSQL(sql);
GridView1.EditIndex = -1;
GridViewBind();
}这样为什么说 ExecuteNonQuery 要求已打开且可用的连接。连接的当前状态为已关闭。
conn.close(); 这一句一定要执行
{
SqlCommand cmd = new SqlCommand(sql);
this.Open();
cmd.Connection = Conn;
int a = cmd.ExecuteNonQuery();
return a;
}
}
改为
public int RunSQL(String sql)
{
SqlCommand cmd = new SqlCommand(sql);
cmd.Connection = Conn;
comd.Connection.Open();
int a = cmd.ExecuteNonQuery();
return a;
}
}
PS:你的这段代码似乎结构上不太合理.参见SqlHelper
{
SqlConnection conn = this.Conn;
conn.Open();
}---------------
这个方法中的conn将在Open方法结束的时候被系统试着回收,这样会引起对象的关闭。你最好这样使用:public int RunSQL(String sql)
{
SqlConnection conn =
new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open() SqlCommand cmd = new SqlCommand(sql);
cmd.Connection = conn;
int a = cmd.ExecuteNonQuery();
conn.Close();
return a;
}
{
SqlConnection conn = this.Conn;
conn.Open();
} ---------------
这个方法中的conn将在Open方法结束的时候被系统试着回收,这样会引起对象的关闭。 //
根源可能是在 已经结贴了 呵呵