114 lines
4.1 KiB
C#
114 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using VAR.DatabaseExplorer.Code.DataTransfer;
|
|
|
|
namespace VAR.DatabaseExplorer.Code.DataAccess
|
|
{
|
|
public class TableDA
|
|
{
|
|
public static List<Table> GetAllTables(string connectionString)
|
|
{
|
|
var tables = new List<Table>();
|
|
var cnx = new SqlConnection(connectionString);
|
|
|
|
cnx.Open();
|
|
DataTable dt = cnx.GetSchema("Tables");
|
|
cnx.Close();
|
|
dt.DefaultView.Sort = "TABLE_SCHEMA ASC, TABLE_NAME ASC, TABLE_TYPE ASC";
|
|
dt = dt.DefaultView.ToTable();
|
|
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
Table table = new Table
|
|
{
|
|
Schema = (string)dr["TABLE_SCHEMA"],
|
|
Name = (string)dr["TABLE_NAME"],
|
|
Type = (string)dr["TABLE_TYPE"]
|
|
};
|
|
|
|
tables.Add(table);
|
|
}
|
|
|
|
return tables;
|
|
}
|
|
|
|
public static List<ViewDefinition> GetViewDefinitions(string connectionString, string schema = null, string name = null)
|
|
{
|
|
SqlDataAdapter dataAdapter;
|
|
var cnx = new SqlConnection(connectionString);
|
|
cnx.Open();
|
|
|
|
if (DatabaseDA.SupportedSqlServerVersion(cnx.ServerVersion))
|
|
{
|
|
dataAdapter = new SqlDataAdapter(@"
|
|
SELECT
|
|
sn.name ViewSchema,
|
|
SO.name ViewName,
|
|
ISNULL(SM.definition, SM.definition) AS [Definition]
|
|
FROM sys.sql_modules SM
|
|
INNER JOIN sys.Objects SO ON SM.Object_id = SO.Object_id
|
|
INNER JOIN sys.schemas sn ON SO.schema_id = sn.schema_id
|
|
WHERE SO.type = 'v'
|
|
AND
|
|
(@name IS NULL OR SO.name = @name) AND
|
|
(@schema IS NULL OR sn.name = @schema)
|
|
", cnx);
|
|
dataAdapter.SelectCommand.Parameters.AddWithValue("@Name", (object)name ?? DBNull.Value);
|
|
dataAdapter.SelectCommand.Parameters.AddWithValue("@Schema", (object)schema ?? DBNull.Value);
|
|
}
|
|
else
|
|
{
|
|
cnx.Close();
|
|
return null;
|
|
}
|
|
|
|
var dt = new DataTable();
|
|
dataAdapter.Fill(dt);
|
|
cnx.Close();
|
|
|
|
var definitions = new List<ViewDefinition>();
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
string strViewSchema = Convert.ToString(dr["ViewSchema"]);
|
|
string strViewName = Convert.ToString(dr["ViewName"]);
|
|
string strDefinition = Convert.ToString(dr["Definition"]);
|
|
strDefinition = strDefinition.Replace("\r", "").Replace("\n", "\r\n");
|
|
definitions.Add(new ViewDefinition { ViewSchema = strViewSchema, ViewName = strViewName, Definition = strDefinition });
|
|
}
|
|
|
|
return definitions;
|
|
}
|
|
|
|
public static IEnumerable<TableRow> GetData(string connectionString, string tableFullName)
|
|
{
|
|
var cnx = new SqlConnection(connectionString);
|
|
string strCmd = string.Format("SELECT * FROM {0}", tableFullName);
|
|
var cmd = new SqlCommand(strCmd, cnx);
|
|
cnx.Open();
|
|
SqlDataReader reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
TableRow row = TableRowHelper.ConvertFromDataRecord(reader);
|
|
yield return row;
|
|
}
|
|
cnx.Close();
|
|
}
|
|
|
|
public static void View_DeleteViews(string connectionString, List<Table> views)
|
|
{
|
|
var cnx = new SqlConnection(connectionString);
|
|
cnx.Open();
|
|
|
|
foreach (Table view in views)
|
|
{
|
|
string strCmd = string.Format("DROP VIEW [{0}].[{1}]", view.Schema, view.Name);
|
|
var cmd = new SqlCommand(strCmd, cnx);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
cnx.Close();
|
|
}
|
|
}
|
|
} |