Monday, December 6, 2010

Query Tool with Asp .Net



using System.Data;

using System.Data.SqlClient;

using System.Configuration;


public partial class _Default : System.Web.UI.Page

{

SqlConnection cnn, cnn2;

SqlCommand cmd;

SqlDataReader dr;


protected void Page_Load(object sender, EventArgs e)

{

cnn = new SqlConnection("Integrated Security=sspi;Initial Catalog=master");

cnn.Open();

cmd = new SqlCommand("sp_helpDb", cnn);

dr = cmd.ExecuteReader();

TreeNode tn;

if (!IsPostBack)

{

while (dr.Read())

{

ddlDb.Items.Add(dr["name"].ToString());

tn = new TreeNode(dr["name"].ToString());

TreeView1.Nodes[0].ChildNodes.Add(tn);

}

dr.Close();

}

}


protected void btnExecute_Click(object sender, EventArgs e)

{

cnn2 = new SqlConnection(@"integrated Security=sspi;Initial Catalog=" + Session["Db"]);

cnn2.Open();


cmd = new SqlCommand(txtQuery.Text, cnn2);

dr = cmd.ExecuteReader();

GridView1.DataSource = dr;

DataBind();

dr.Close();

}


protected void ddlDb_SelectedIndexChanged(object sender, EventArgs e)

{

Session["Db"] = ddlDb.Text;

}

}




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td colspan="2">
<br />
Select Database
<asp:DropDownList ID="ddlDb" runat="server" AutoPostBack="True" Height="21px"
onselectedindexchanged="ddlDb_SelectedIndexChanged" Width="122px">
</asp:DropDownList>
&nbsp;&nbsp;&nbsp;
<asp:Button ID="btnExecute" runat="server" onclick="btnExecute_Click"
Text="Execute" />
<br />
</td>
</tr>
<tr>
<td>
<asp:TreeView ID="TreeView1" runat="server">
<Nodes>
<asp:TreeNode Text="Database" Value="Database"></asp:TreeNode>
</Nodes>
</asp:TreeView>
</td>
<td>
<table class="style1">
<tr>
<td>
<asp:TextBox ID="txtQuery" runat="server" Height="87px" TextMode="MultiLine"
Width="370px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtResult" runat="server" Height="16px" TextMode="MultiLine"
Visible="False" Width="370px"></asp:TextBox>
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
ASP.NET 4 Unleashed</td>
</tr>
</table>
</td>
</tr>
</table>

</div>
</form>
</body>
</html>

No comments:

Post a Comment