asp/x
01. Enumarate MSSQL database
Show all databases
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Server=tcp:<server_name>.database.windows.net,1433;Initial Catalog=<database_name>;Persist Security Info=False;User ID=<username>;Password=<password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("SELECT name FROM sys.databases", connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Response.Write(reader[0].ToString() + "<br>");
}
reader.Close();
connection.Close();
}
}
</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<h1>List of Databases</h1>
</div>
</form>
</body>
</html>
List all tables from database
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Server=tcp:<server_name>.database.windows.net,1433;Initial Catalog=<database_name>;Persist Security Info=False;User ID=<username>;Password=<password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("SELECT TABLE_NAME FROM information_schema.tables", connection);
SqlDataReader reader = command.ExecuteReader();
tablePlaceholder.Controls.Add(new LiteralControl("<table>"));
tablePlaceholder.Controls.Add(new LiteralControl("<tr><th>Table Name</th></tr>"));
while (reader.Read())
{
tablePlaceholder.Controls.Add(new LiteralControl("<tr><td>" + reader[0].ToString() + "</td></tr>"));
}
tablePlaceholder.Controls.Add(new LiteralControl("</table>"));
reader.Close();
connection.Close();
}
}
</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<h1>List of Tables</h1>
<asp:PlaceHolder ID="tablePlaceholder" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Dump data from table
Dump specific database
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Server=tcp:<server_name>.database.windows.net,1433;Initial Catalog=<database_name>;Persist Security Info=False;User ID=<username>;Password=<password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// change the connection string to the Onlocation_Production database
connection.ChangeDatabase("<DB_NAME>");
SqlCommand tableCommand = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", connection);
SqlDataReader tableReader = tableCommand.ExecuteReader();
while (tableReader.Read())
{
string tableName = tableReader[0].ToString();
Response.Write("<h3>" + tableName + "</h3>");
SqlCommand dataCommand = new SqlCommand("SELECT * FROM " + tableName, connection);
SqlDataReader dataReader = dataCommand.ExecuteReader();
// build the table to display the data
Response.Write("<table border='1' cellpadding='5' cellspacing='0'>");
Response.Write("<tr>");
for (int i = 0; i < dataReader.FieldCount; i++)
{
Response.Write("<th>" + dataReader.GetName(i) + "</th>");
}
Response.Write("</tr>");
while (dataReader.Read())
{
Response.Write("<tr>");
for (int i = 0; i < dataReader.FieldCount; i++)
{
Response.Write("<td>" + dataReader[i].ToString() + "</td>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
dataReader.Close();
}
tableReader.Close();
}
}
</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<h1>Data from Onlocation_Production Database</h1>
</div>
</form>
</body>
</html>
Dump all database
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Server=tcp:<server_name>.database.windows.net,1433;Initial Catalog=<database_name>;Persist Security Info=False;User ID=<username>;Password=<password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("SELECT name FROM sys.databases", connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string databaseName = reader[0].ToString();
Response.Write("<h2>" + databaseName + "</h2>");
// change the connection string to the current database
connection.ChangeDatabase(databaseName);
SqlCommand tableCommand = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", connection);
SqlDataReader tableReader = tableCommand.ExecuteReader();
while (tableReader.Read())
{
string tableName = tableReader[0].ToString();
Response.Write("<h3>" + tableName + "</h3>");
SqlCommand dataCommand = new SqlCommand("SELECT * FROM " + tableName, connection);
SqlDataReader dataReader = dataCommand.ExecuteReader();
// build the table to display the data
Response.Write("<table border='1' cellpadding='5' cellspacing='0'>");
Response.Write("<tr>");
for (int i = 0; i < dataReader.FieldCount; i++)
{
Response.Write("<th>" + dataReader.GetName(i) + "</th>");
}
Response.Write("</tr>");
while (dataReader.Read())
{
Response.Write("<tr>");
for (int i = 0; i < dataReader.FieldCount; i++)
{
Response.Write("<td>" + dataReader[i].ToString() + "</td>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
dataReader.Close();
}
tableReader.Close();
}
reader.Close();
}
}
</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<h1>Data from all Databases and Tables</h1>
</div>
</form>
</body>
</html>
Last updated