自动输出SQLServer存储过程依赖列表到EXCEL文件

来源:计算机等级考试    发布时间:2012-08-27    计算机等级考试视频    评论


  itemStyle.Font.Weight = ExcelFont.BoldWeight;
  sheet.Cells[0, 0].Value = string.Format("{0} database dictionary", db);
  sheet.Cells[4, 0].Value = "Name";
  sheet.Cells[4, 0].Style = nameStyle;
  sheet.Cells[4, 1].Value = "Dependencies";
  sheet.Cells[4, 1].Style = nameStyle;
  sheet.Cells[4, 2].Value = "Type";
  sheet.Cells[4, 2].Style = nameStyle;
  string connectionString = string.Format("Password={0};Persist Security Info=True;User ID=sa;Initial Catalog={1};Data Source={2}", password, db, dataSource);
  using (SqlConnection connection = new SqlConnection(connectionString))
  {
  connection.Open();
  sheet.Cells[5, 0].Value = "Stored Procedures";
  sheet.Cells[5, 0].Style = typeStyle;
  DataSet data = new DataSet();
  using (SqlCommand command = new SqlCommand("SELECT * FROM sysobjects WHERE XTYPE='p' ORDER BY NAME", connection))
  {
  SqlDataAdapter adapter = new SqlDataAdapter(command);
  adapter.Fill(data);
  DataTable objects = data.Tables[0];
  int index = 6;
  for (int i = 0; i < objects.Rows.Count; i++)
  {
  string objectName = objects.Rows[i]["name"].ToString();
  sheet.Cells[index, 0].Value = objectName;
  sheet.Cells[index, 0].Style = itemStyle;
  DataSet data2 = new DataSet();
  using (SqlCommand command2 = new SqlCommand(string.Format("exec sp_depends '{0}'", objectName), connection))
  {
  adapter = new SqlDataAdapter(command2);
  adapter.Fill(data2);
  }
  if (data2.Tables.Count > 0)
  {
  DataTable dependencies = data2.Tables[0];
  Dictionary<string, KeyValuePair<string, string>> uniqueDependencies = new Dictionary<string, KeyValuePair<string, string>>();
  for (int j = 0; j < dependencies.Rows.Count; j++)
  {
  string itemName = dependencies.Rows[j]["name"].ToString();
  if (!uniqueDependencies.ContainsKey(itemName))
  uniqueDependencies.Add(itemName, new KeyValuePair<string, string>(itemName, dependencies.Rows[j]["type"].ToString()));
  }

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答