2011-01-21 7 views

답변

1

당신은 단지 연결을 열고 데이터베이스를 조회 할 수 있습니다

using(var connection = new SqlConnection(connectionString)) { 
    connection.Open(); 
    using(var command = connection.CreateCommand()) { 
     command.CommandText = "SELECT * FROM SYS.TABLES"; 
     using(var reader = command.ExecuteReader()) { 
      while(reader.Read()) { 
       Console.WriteLine(reader["name"]); 
      } 
     } 
    } 
} 

할 수 있습니다 구글 원하는 다른 정보에 대한 쿼리 문자열.

0

데이터베이스에 SqlConnection을 작성하고 연결을여십시오.

SqlConnection conn = new SqlConnection("Data Source=Servername;Initial Catalog=Marketing;Integrated Security=SSPI"); 
conn.Open(); 

SqlCommand를 만들고 필요한 SQL의 값으로 CommandText을 할당합니다.

SqlCommand cmd = new SqlCommand("PLACE SQL HERE", conn); 

테이블과 행 수 : 사용

SELECT 
    [TableName] = so.name, 
    [RowCount] = MAX(si.rows) 
FROM 
    sysobjects so, 
    sysindexes si 
WHERE 
    so.xtype = 'U' 
    AND 
    si.id = OBJECT_ID(so.name) 
GROUP BY 
    so.name 
ORDER BY 
    2 DESC 

공간 :

EXEC sp_spaceused 'tablename' 
0

스키마 이름을 포함하지만 대부분의 정보는 현재 원하는 도착하지 않는이 스크립트 데이터 베이스. 저장 프로 시저에 적용 할 수있을 것이라고 확신합니다.

SET NOCOUNT ON 
GO 
DECLARE @tblSpaceUsed TABLE 
(
    [name] sysname NOT NULL, 
    [rows] int NOT NULL, 
    [reserved] nvarchar(50) NOT NULL, 
    [reservedKB] int NULL, 
    [data] nvarchar(50) NOT NULL, 
    [dataKB] int NULL, 
    [index] nvarchar(50) NOT NULL, 
    [indexKB] int NULL, 
    [unused] nvarchar(50) NOT NULL, 
    [unusedKB] int NULL 
) 

DECLARE @tableName sysname 
DECLARE @tableNames CURSOR 

SET @tableNames = CURSOR 
FAST_FORWARD 
FOR 
SELECT DISTINCT 
    ss.name + '.' + st.name 
FROM 
    sys.tables st 
    INNER JOIN 
    sys.schemas ss 
     ON st.schema_id = ss.schema_id 

OPEN @tableNames 
FETCH NEXT FROM @tableNames INTO @tableName 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    INSERT INTO @tblSpaceUsed ([name], [rows], [reserved], [data], [index], [unused]) EXEC sp_spaceused @tableName 

    FETCH NEXT FROM @tableNames INTO @tableName 
END 

CLOSE @tableNames 

UPDATE 
    @tblSpaceUsed 
SET 
    [reservedKB] = CONVERT(int, LEFT([reserved], LEN([reserved]) - 3)), 
    [dataKB] = CONVERT(int, LEFT([data], LEN([data]) - 3)), 
    [indexKB] = CONVERT(int, LEFT([index], LEN([index]) - 3)), 
    [unusedKB] = CONVERT(int, LEFT([unused], LEN([unused]) - 3)) 

SELECT 
    * 
FROM 
    @tblSpaceUsed 
관련 문제