数据库服务器出现连接数过多,导致网站打不开
我也不清楚那个地方问题,最多的写法如下:
代码如下:
//引入数据库操作类
Admin_ClassDb classdb = new Admin_ClassDb();
//定义所需sql语句
string sql1 = "select top 4 id,pic,title from brandfix where Recommend='是' order by ID desc";
//取得结果值
DataList1.DataSource = classdb.GetTables(sql1).DefaultView;
DataList1.DataBind();
函数:public DataTable GetTables(string sql) //返回ds.Tables["defaulttable"]
{ //从web.config中取得数据库连接字符串
string SqlConnectionStr=System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionStr"];
//创建数据库连接对象
SqlConnection Sqlconn=new SqlConnection(SqlConnectionStr);
//创建DataAdapter对像
SqlDataAdapter da=new SqlDataAdapter(sql,Sqlconn);
//创建数据集
DataSet ds=new DataSet();
//填充数据集
da.Fill(ds,"defaulttable");
return ds.Tables["defaulttable"];
}这个需要释放吗
我也不清楚那个地方问题,最多的写法如下:
代码如下:
//引入数据库操作类
Admin_ClassDb classdb = new Admin_ClassDb();
//定义所需sql语句
string sql1 = "select top 4 id,pic,title from brandfix where Recommend='是' order by ID desc";
//取得结果值
DataList1.DataSource = classdb.GetTables(sql1).DefaultView;
DataList1.DataBind();
函数:public DataTable GetTables(string sql) //返回ds.Tables["defaulttable"]
{ //从web.config中取得数据库连接字符串
string SqlConnectionStr=System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionStr"];
//创建数据库连接对象
SqlConnection Sqlconn=new SqlConnection(SqlConnectionStr);
//创建DataAdapter对像
SqlDataAdapter da=new SqlDataAdapter(sql,Sqlconn);
//创建数据集
DataSet ds=new DataSet();
//填充数据集
da.Fill(ds,"defaulttable");
return ds.Tables["defaulttable"];
}这个需要释放吗
解决方案 »
- 大伙发一发都是怎么设计DB层的?寻求一种可以写最少的代码实现MSSQL和ORACLE之间转换?谢谢各位
- Label1.Visible = false未将对象的引用设置到对象的实例
- SONY 大容量u 盘,价格超级低,想买的朋友可以进来看看
- 如何让页面居中。
- 郁闷的问题:老是提示 session已失效。谢谢。
- sys.webForms.PageRequestManagerParserErrorException
- SQL2000数据库连接问题……紧急求教……请赐教!!!
- 表头如何合并,弄不起来啊,求帮忙
- FSO生成.html问题
- 网页刷新问题?
- 关于 ASP.NET 1.1 与 ASP.NET 2.0 的区别?
- web服务器和SQL服务器分开对CC,DDOS攻击有作用吗
// System.Data.SqlClient library
#region Building the connection string
string Server = "localhost";
string Username = "my_username";
string Password = "my_password";
string Database = "my_database";
string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;
#endregion
#region Try to establish a connection to the database
SqlConnection SQLConnection = new SqlConnection();
try
{
SQLConnection.ConnectionString = ConnectionString;
SQLConnection.Open();
// You can get the server version
// SQLConnection.ServerVersion
}
catch (Exception Ex)
{
// Try to close the connection
if (SQLConnection != null)
SQLConnection.Dispose();
// Create a (useful) error message
string ErrorMessage = "A error occurred while trying to connect to the server.";
ErrorMessage += Environment.NewLine;
ErrorMessage += Environment.NewLine;
ErrorMessage += Ex.Message;
// Show error message (this = the parent Form object)
MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// Stop here
return;
}
#endregion
#region Execute a SQL query
string SQLStatement = "SELECT * FROM ExampleTable";
// Create a SqlDataAdapter to get the results as DataTable
SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(SQLStatement, SQLConnection);
// Create a new DataTable
DataTable dtResult = new DataTable();
// Fill the DataTable with the result of the SQL statement
SQLDataAdapter.Fill(dtResult);
// Loop through all entries
foreach (DataRow drRow in dtResult.Rows)
{
// Show a message box with the content of
// the "Name" column
MessageBox.Show(drRow["Name"].ToString());
}
// We don't need the data adapter any more
SQLDataAdapter.Dispose();
#endregion
#region Close the database link
SQLConnection.Close();
SQLConnection.Dispose();
#endregion
public static DataTable GetTables(string sql) //返回ds.Tables["defaulttable"]
{
//从web.config中取得数据库连接字符串
string SqlConnectionStr=System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionStr"];
//创建数据库连接对象
SqlConnection Sqlconn=new SqlConnection(SqlConnectionStr);
//创建DataAdapter对像
SqlDataAdapter da=new SqlDataAdapter(sql,Sqlconn);
//创建数据集
DataSet ds=new DataSet();
//填充数据集
da.Fill(ds,"defaulttable");
Sqlconn.Close();//关闭
return ds.Tables["defaulttable"];
}
//自己处理异常去 不写了
是静态方法返回一个sqlconnection实例,这是楼主写的方法
public DataTable GetTables
,这个方法必定属于一个非静态类,所以要实例化类,得到对象后根据对象调用这个方法。
{
public Myclass()
{
connstring = ConfigurationManager.AppSettings["constr"];
} protected SqlConnection conn;
protected string connstring;
protected void Open()
{
if (conn == null)
{
conn = new SqlConnection(connstring);
}
if (conn.State.Equals(ConnectionState.Closed))
{
conn.Open();
}
} public void Close()
{
if (conn != null)
conn.Close();
}
public void Sql_Dml(string sql)
{
Open();
SqlCommand comm = new SqlCommand(sql, conn);
comm.ExecuteNonQuery();
Close();
} public int Sql_Dql(string sql)
{
Open();
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader dr = comm.ExecuteReader();
if (dr.HasRows)
{
return 1;
}
else
{
return 0;
}
Close();
}
public DataTable Sql_SelectTable(string sql)
{
Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
Close();
return ds.Tables[0];
}
用try catch
加上finally来处理.数据库服务器出现连接数过多,导致网站打不开
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
先查查数据库在执行什么语句.
这个方法本身是没有任何问题的!!!
DataAdapter 会自动帮你管理 Connection 的 Open/Close2.
其他方法呢?3.
Connection 不要用 static,Connection 的开销在于 Open 操作而不在于 Connection 对象本身,本质上他应该仅仅是一个指针而已
{
}
实现,通过要关闭,最好做成S数据库操作类,可参考sqlhelp,petshop等