sql="****"
string ConnStr = ConfigurationSettings.AppSettings["siteSqlServer"];
DataSet ds = new DataSet();
SqlConnection MyConn = new SqlConnection(ConnStr);
try
{
if (MyConn.State.ToString() == "Close") MyConn.Open();
string bid_check = "select * from cs_sections where sectionid=" + bid;
SqlCommand comm = new SqlCommand(bid_check, MyConn);
SqlDataReader dr = comm.ExecuteReader();
if (!dr.Read())
{
sql = "&&&&";
}
dr.Close();
SqlDataAdapter da = new SqlDataAdapter(sql, MyConn);
da.Fill(ds, "table");
GV1.DataSource = ds.Tables[0].DefaultView;
GV1.DataBind();
}
catch (Exception ex)
{
Response.Write("<font color=red>错误信息:" + ex.Message + "</font>");
}
finally
{
MyConn.Close();
}GV是控件ID
string ConnStr = ConfigurationSettings.AppSettings["siteSqlServer"];
DataSet ds = new DataSet();
SqlConnection MyConn = new SqlConnection(ConnStr);
try
{
if (MyConn.State.ToString() == "Close") MyConn.Open();
string bid_check = "select * from cs_sections where sectionid=" + bid;
SqlCommand comm = new SqlCommand(bid_check, MyConn);
SqlDataReader dr = comm.ExecuteReader();
if (!dr.Read())
{
sql = "&&&&";
}
dr.Close();
SqlDataAdapter da = new SqlDataAdapter(sql, MyConn);
da.Fill(ds, "table");
GV1.DataSource = ds.Tables[0].DefaultView;
GV1.DataBind();
}
catch (Exception ex)
{
Response.Write("<font color=red>错误信息:" + ex.Message + "</font>");
}
finally
{
MyConn.Close();
}GV是控件ID
//关闭数据库
//成功返回true,否则返回false
public void Close()
{
if( null != dbConn )
{
if( ConnectionState.Closed != dbConn.State )
{
dbConn.Close();
}
dbConn.Dispose();
}
dbConn = null;
}
你的这句应该是Closed吧
if (MyConn.State.ToString() == "Close") MyConn.Open();
错误。去掉这句看看。就算是判断数据库联接状态也不是用tostring()方法的。
改成
if (MyConn.State.ToString() == "Closed") MyConn.Open();
string ConnStr = ConfigurationSettings.AppSettings["siteSqlServer"];
DataSet ds = new DataSet();
SqlConnection MyConn = new SqlConnection(ConnStr);
try
{
if (MyConn.State.ToString() == "Close") MyConn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, MyConn);
da.Fill(ds, "table");
GV1.DataSource = ds.Tables[0].DefaultView;
GV1.DataBind();
}
catch (Exception ex)
{
Response.Write("<font color=red>错误信息:" + ex.Message + "</font>");
}
finally
{
MyConn.Close();
}还有我测过这样的正确编译的
if (MyConn.State != ConnectionState.Opened)
Ivy_zheng(最后一只恐龙) 正解
if (MyConn.State.ToString() == "Close") MyConn.Open();
改成
if (MyConn.State.ToString() == "Closed") MyConn.Open();