using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class QQquery : System.Web.UI.Page { DataSet finaldata = new DataSet(); protected void Page_Load(object sender, EventArgs e) { Label1.Text = "over"; } //数据集转字符串数组,用于遍历 public string[] ds2string(DataSet _ds) { int n = _ds.Tables[0].Rows.Count; string[] s = new string[n]; for (int i = 0; i < n; i++) { s[i] = _ds.Tables[0].Rows[i][0].ToString(); } return s; } //获取全部数据库的名字 public DataSet getAllDataBase() { SqlConnection coon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString); string sqlstr = "select name from sys.databases where name like "GroupData%""; SqlDataAdapter da = new SqlDataAdapter(sqlstr, coon); DataSet ds = new DataSet(); da.Fill(ds); return ds; } //获取数据库下的全部表 public DataSet getTable(string _DataBaseName) { SqlConnection coon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString); string sqlstr = "SELECT name FROM " + _DataBaseName + "..SysObjects Where XType="U" and name!="dtproperties""; SqlDataAdapter da = new SqlDataAdapter(sqlstr, coon); DataSet ds = new DataSet(); da.Fill(ds); return ds; } //获取数据库中表的数据 public DataSet QueryData(string _DataBaseName, string _TableName, string _QueryNum) { SqlConnection coon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString); string sqlstr = "select * from " + _DataBaseName + ".." + _TableName + " where QQNum ="" + _QueryNum + """; SqlDataAdapter da = new SqlDataAdapter(sqlstr, coon); DataSet ds = new DataSet(); da.Fill(ds); return ds; } //查询按钮 protected void Button_query_Click1(object sender, EventArgs e) { //输入要查询的qq号码 string qq = TextBox_QQ.Text; //遍历数据库 foreach (string database in ds2string(getAllDataBase())) { //遍历数据库中的表 foreach (string table in ds2string(getTable(database))) { //查询数据,结果添加到数据集finaldata中 finaldata.Merge(QueryData(database, table, qq)); } } //查询完毕,绑定数据 GridView1.DataSource = finaldata; GridView1.DataBind(); } }
若干个MDF文件附加的数据库:
数据库中的表:
表的结构
现在的问题是,数据量太大,在本人的2G内存的机器上,一查询就卡死,然后就超时了
本人只能一个表一个表的去查,这样的话不会卡死
这次泄漏的数据库一共大致90G
作为一个新手纯属技术研究,望高手指点怎么样提高效率
解决方案
40
QQNum有索引吗?有索引和没有索引,速度应该差5000倍。