SQl查詢資料庫庫名,表名、表的列名

來源:互聯網
上載者:User
查詢資料庫select * From master.dbo.sysdatabases where name='資料庫名' and status<>512--讀取庫中的所有表名 (當前資料庫)
select name from sysobjects where xtype='u'

--讀取指定表的所有列名
select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='表名')
擷取資料庫表名和欄位
sqlserver中各個系統資料表的作用
    sysaltfiles      主要資料庫                  儲存資料庫的檔案
syscharsets      主要資料庫                  字元集與排序次序
sysconfigures    主要資料庫                  配置選項
syscurconfigs    主要資料庫                  當前配置選項
sysdatabases      主要資料庫                  伺服器中的資料庫
syslanguages      主要資料庫                  語言
syslogins        主要資料庫                  登陸帳號資訊
sysoledbusers    主要資料庫                  連結的伺服器登陸資訊
sysprocesses      主要資料庫                  進程
sysremotelogins主要資料庫                  遠程登入帳號

syscolumns        每個資料庫                列
sysconstrains    每個資料庫                限制
sysfilegroups    每個資料庫                檔案組
sysfiles          每個資料庫                檔案
sysforeignkeys 每個資料庫                外部關鍵字
sysindexs        每個資料庫                索引
sysmenbers        每個資料庫                角色成員
sysobjects        每個資料庫                所有資料庫物件
syspermissions 每個資料庫                許可權
systypes          每個資料庫                使用者定義資料類型
sysusers          每個資料庫                使用者

//
用什麼方法可以得到一個表中所有的列名。SQl語句。
select      列名=name      from      syscolumns      where      id=object_id(N'要查的表名')

use gpStrudy
select      name="name"      from      syscolumns      where      id=object_id(N'bookTable')
獲得欄位的屬性
//這是從一段代碼中考出來的,使用的是using    System.Data.OleDb; 
    public    int    GetTableFields(String    tableName,out    String[]    fields,out    String[]    fieldTypes) 
    ...{ 
    try 
    ...{ 
    OleDbCommand    dc          =    m_OleDb.CreateCommand();//建立一個執行對象用於執行sql查詢 
    dc.CommandText            =    "select    *    from    "    +    tableName; 
    dc.Transaction            =    m_OleTrans; 
   
    OleDbDataReader    dr    =    dc.ExecuteReader();//執行sql查詢 
    //擷取資料庫的架構資訊 
    DataTable    schemaTable    =    dr.GetSchemaTable(); 
   
    int    fieldColumnCount    =    0; 
   
    fields    =    new    String[schemaTable.Rows.Count]; 
    fieldTypes    =    new    String[schemaTable.Rows.Count]; 
   
    for(int    i    =    0;i    <    schemaTable.Columns.Count;i++) 
    ...{ 
    if(schemaTable.Columns[i].ColumnName.IndexOf("ColumnName")    >=    0) 
    ...{ 
    //擷取欄位名稱 
   
    for(int    k    =    0;k    <    schemaTable.Rows.Count;k++) 
    fields[k]    =    schemaTable.Rows[k].ItemArray[i].ToString(); 
   
    fieldColumnCount++; 
    if(fieldColumnCount    >=    2) 
    ...{ 
    dr.Close(); 
    dc.Dispose(); 
   
    dr    =    null; 
    dc    =    null; 
    return    schemaTable.Rows.Count; 
    } 
    } 
    else    if(schemaTable.Columns[i].ColumnName.IndexOf("DataType")    >=    0) 
    ...{ 
    //擷取欄位類型 
   
    for(int    k    =    0;k    <    schemaTable.Rows.Count;k++) 
    fieldTypes[k]    =    schemaTable.Rows[k].ItemArray[i].ToString(); 
   
    fieldColumnCount++; 
    if(fieldColumnCount    >=    2) 
    ...{ 
    dr.Close(); 
    dc.Dispose(); 
   
    dr    =    null; 
    dc    =    null; 
    return    schemaTable.Rows.Count; 
    } 
    } 
   
    } 
   
    dr.Close(); 
    dc.Dispose(); 
   
    dr    =    null; 
    dc    =    null; 
   
    return    0; 
   
    } 
    catch(Exception    ee) 
    ...{ 
    fields            =    new    String[1]; 
    fieldTypes    =    new    String[1]; 
    m_ErrorString    =    "Ado_Application:GetTableFields:error:"    +    ee.Message; 
    return    0; 
    } 
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.