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 要求已打开且可用的连接。连接的当前状态为已关闭。
------解决方案--------------------public int RunSQL(String sql)
{
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
------解决方案--------------------同一个conn不可以同时打开两次的