zoukankan      html  css  js  c++  java
  • 如何编程得到数据库信息

    获取数据库信息:
            public List<string> GetDatabase(string connectionString)
    {
    using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT Name FROM Master.sys.SysDatabases WHERE dbid > 4 ORDER BY Name ", connectionString))
    {
    DataTable table = new DataTable();
    try
    {
    sqlDataAdapter.Fill(table);

    List<string> tables = new List<string>();
    foreach (DataRow row in table.Rows)
    {
    tables.Add(row[0].ToString());
    }
    return tables;
    }
    catch
    {
    throw new ApplicationException("can not connect to server");
    }
    }
    }
    获取数据表信息:
            public List<string> GetTables(string connectionString, string db)
    {
    using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(String.Format("SELECT name FROM {0}.sys.SysObjects WHERE (xtype = 'U' OR xtype = 'V') ORDER BY name", db), connectionString))
    {
    DataTable table = new DataTable();
    try
    {
    sqlDataAdapter.Fill(table);

    List<string> tables = new List<string>();
    foreach (DataRow row in table.Rows)
    {
    tables.Add(row[0].ToString());
    }
    return tables;
    }
    catch
    {
    throw new ApplicationException(String.Format("can not connect to db {0} ", db));
    }
    }
    }
    获取字段信息:
            public DataTable GetFields(string connectionString, string db, string tableName)
    {
    using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(string.Format("SELECT A.[name],[Type] =B.[name],A.[length] FROM {0}.sys.SysColumns AS A LEFT JOIN {0}.sys.SysTypes AS B ON A.[xtype]=B.[xusertype] WHERE A.[id] = OBJECT_ID('{0}.dbo.[{1}]')", db, tableName.Replace("'", "''")), connectionString))
    {
    DataTable table = new DataTable();
    try
    {
    sqlDataAdapter.Fill(table);

    return table;
    }
    catch
    {
    throw new ApplicationException(String.Format("can not connect to db {0}", db));
    }
    }
    }
  • 相关阅读:
    flume,kafka不在一个内网互相打通.md
    尚硅谷Flink2020教程.md
    常用命令.md
    四象限工作效率-事件管理.md
    甘特图目标实施-进度管控.md
    PDCA循环法.md
    SMART大目标拆解小目标.md
    基于内外部竞争环境和竞争条件下的态势分析
    使用Java正则表达式批量提取文本信息
    使用markdown高效编写博客(创建标题)
  • 原文地址:https://www.cnblogs.com/zanxiaofeng/p/1687676.html
Copyright © 2011-2022 走看看