private void Open()
{
try
{
if (con.State == ConnectionState.Open)
{
con.Close
}
con.Open();
}
catch (Exception)
{
System.Web.HttpContext.Current.Response.Write("数据库连接失败");
System.Web.HttpContext.Current.Response.End();
}
}
我在访问数据库的时候 经常都会出现数据库失败的错误,数据库链接我就是用的上面的函数,每次运行应该都会先关闭数据库再重新链接,怎么还会出这个错误啊?
{
try
{
if (con.State == ConnectionState.Open)
{
con.Close
}
con.Open();
}
catch (Exception)
{
System.Web.HttpContext.Current.Response.Write("数据库连接失败");
System.Web.HttpContext.Current.Response.End();
}
}
我在访问数据库的时候 经常都会出现数据库失败的错误,数据库链接我就是用的上面的函数,每次运行应该都会先关闭数据库再重新链接,怎么还会出这个错误啊?
这里是什么啊?
con.Close();
private void Open()
{
try
{
if (con.State == ConnectionState.Open)
{
con.Close()
}
con.Open();
}
catch (Exception)
{
System.Web.HttpContext.Current.Response.Write("数据库连接失败");
System.Web.HttpContext.Current.Response.End();
}
} 循环是循环datatablepublic DataTable GetDataTable(string sqlstr)
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable datatable = new DataTable();
Open();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.Connection=con;
da.SelectCommand = comm;
da.Fill(datatable); return datatable;
}
public virtual bool Open()
{
if (string.IsNullOrEmpty(this._dbConnection.ConnectionString))
{
throw new ArgumentNullException("ConnectionString");
}
if ((this._dbConnection != null) && (this._dbState == DataConnectionState.Opening))
{
this.Close();
}
try
{
this._dbConnection.Open();
}
catch (Exception ex)
{
if (this.LogEntry == null)
{
throw ex;
}
this.LogEntry.RecordConnectionOpenError(ex);
}
return true;
}
DataAdapter在填充数据时会自动打开连接,取得数据后,会关闭连接。
{
连接代码;.......
}
用这种一切OK
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
sda.Fill(datatable);
return datatable;