日期:2014-05-17  浏览次数:20424 次

遍历数据库出现异常
请问一下各位高手,小弟写了一个段遍历数据库的代码:
 
            string path = @"D:\a.txt";
            FileStream f = new FileStream(path, FileMode.Open);
            StreamWriter sw = new StreamWriter(f,Encoding.UTF8);
            DateTime t = System.DateTime.Now;
            string strConnection = "Data Source=221.208.168.124;User ID=sa;Password=sa@123456";
            SqlConnection sqlConnetion = new SqlConnection(strConnection);
            string strDbas = "SELECT Name FROM Master..SysDatabases ORDER BY Name";
            SqlDataAdapter adpterDtbase = new SqlDataAdapter(strDbas, strConnection);
            DataTable dtbase = new DataTable();
            adpterDtbase.Fill(dtbase);
            sqlConnetion.Close();
            try
            {
                sw.WriteLine("一共有" + dtbase.Rows.Count.ToString() + "数据库 ");
                for (int u = 0; u < dtbase.Rows.Count; u++)
                {
                    string sql = "Data Source=221.208.168.124;Initial Catalog=" + dtbase.Rows[u][0].ToString() + ";User ID=sa;Password=sa@123456";
                    SqlConnection conn = new SqlConnection(sql);
                    sw.WriteLine("数据库的名称为" + dtbase.Rows[u][0].ToString());
                    string strSql = "SELECT Name FROM " + dtbase.Rows[u][0].ToString() + "..SysObjects ORDER BY Name";
                    SqlDataAdapter adpterDt = new SqlDataAdapter(strSql, conn);
                    DataTable dt = new DataTable();
                    adpterDt.Fill(dt);
                    try