# asp/x

## 01. Enumarate MSSQL database

* Show all databases

```aspnet
<%@ 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

```aspnet
<%@ 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&#x20;

```aspnet
<%@ 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

```csharp
<%@ 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>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.h4rithd.com/languages/asp-x.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
