简单的ACCESS查询分析器

这个博客介绍如何利用.NET Framework 1.1和IIS创建一个ASPX页面,实现简单的ACCESS数据库查询功能。用户可以输入连接字符串和SQL语句,点击执行按钮获取查询结果。页面还包含查看历史和清除历史的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只要你的机器上有.net framework 1.1 和 IIS,然后你做个ASPX页面并拷贝到任意虚拟目录下就可以了:
&lt;%@ Page LANGUAGE=&quot;C#&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Data&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Data.OleDb&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Web &quot; %&gt; &lt;%@ Import Namespace=&quot;System.Web.UI.WebControls&quot; %&gt; &lt;%@ Import Namespace=&quot;System.Web.UI.HtmlControls&quot; %&gt; &lt;%@ Import Namespace=&quot;System &quot;%&gt; &lt;%@ Import Namespace=&quot;System.Collections &quot;%&gt; &lt;%@ Import Namespace=&quot;System.ComponentModel &quot;%&gt; &lt;%@ Import Namespace=&quot;System.Drawing &quot;%&gt; &lt;%@ Import Namespace=&quot;System.Web.UI &quot;%&gt; &lt;%@ Import Namespace=&quot;System.Web.SessionState &quot;%&gt;  &lt;html&gt; &lt;HEAD&gt; 		&lt;title&gt;exesql&lt;/title&gt; 		&lt;meta name=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio .NET 7.1&quot;&gt; 		&lt;meta name=&quot;CODE_LANGUAGE&quot; Content=&quot;C#&quot;&gt; 		&lt;meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;&gt; 		&lt;meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;&gt; 		&lt;style&gt; 			.TextBox { WIDTH: 90% } 			.TextArea { WIDTH: 90%; HEIGHT: 200px } 		&lt;/style&gt; 	&lt;/HEAD&gt; <script language="C#" runat="server">

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			this.TextBox_ConnStr.Text = "Provider   =   Microsoft.Jet.OLEDB.4.0   ;   Data Source   =   "+ Server.MapPath("db.mdb");
		}
		
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button_Execute.Click += new System.EventHandler(this.Button_Execute_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		private void Button_Execute_Click(object sender, System.EventArgs e)
		{
			if(Request.Form["history"].Trim() == "clear") this.Label_History.InnerHtml = "";
			this.Label_History.InnerHtml += "<xmp style=/"width:95%;background-color:lightgrey;float:left;border:solid 1px black;margin:1px 1px 1px 1px;/">" + this.TextBox_SqlStr.Text + "
"; this.Label_Message.Text = ""; this.Panel_Result.Controls.Clear(); AccessDBManager.DBConnectionString = this.TextBox_ConnStr.Text.Trim(); if(this.TextBox_SqlStr.Text.IndexOf("|~|") > 0) { string sqlStrList = this.TextBox_SqlStr.Text.Replace("^","{@@}"); foreach(string s in sqlStrList.Split(new char[]{'^'})) { if(s.Length > 5) { exe(s.Replace("{@@}","^")); } } } else { exe(this.TextBox_SqlStr.Text); } } private void exe(string sql) { AccessDBManager.errorMessage = ""; if(sql.ToLower().StartsWith("select")) { DataTable dt = AccessDBManager.GetTableBySQL(sql); System.Web.UI.HtmlControls.HtmlGenericControl hc = new HtmlGenericControl("div"); hc.Attributes.Add("style","width:100%;color:red;background-color:lightgrey;"); hc.InnerHtml = sql + "
Return results: [ " + dt.Rows.Count + " ]"; this.Panel_Result.Controls.Add(hc); DataGrid dg = new DataGrid(); dg.ID = "DG_" + this.Panel_Result.Controls.Count.ToString(); dg.Width = new Unit("100%"); dg.AutoGenerateColumns = true; dg.DataSource = dt; if(dt.Columns.Count > 0) dg.DataBind(); this.Panel_Result.Controls.Add(dg); } else { System.Web.UI.HtmlControls.HtmlGenericControl hc = new HtmlGenericControl("div"); hc.Attributes.Add("style","width:100%;color:red;background-color:lightgrey;"); hc.InnerHtml = sql + "
Execute SQL : [ " + AccessDBManager.ExecuteNonQuerySQL(sql) + " ]"; this.Panel_Result.Controls.Add(hc); } if(AccessDBManager.errorMessage != "") { this.Label_Message.Text += "
" + AccessDBManager.errorMessage; } } public class AccessDBManager { public AccessDBManager() { // // TODO: Add constructor logic here // } public static string errorMessage; public static string DBConnectionString; private OleDbConnection oleConnection = null; private OleDbCommand oleCommand = null; private OleDbDataAdapter sqlCommand = null; private static void LogError(Exception error) { errorMessage = error.Message; } //connect to oracle and excute more than a sql #region need open database connection for more than a sql public bool OpenDatabase() { bool success = false; try { this.oleConnection = new OleDbConnection(DBConnectionString); this.oleConnection.Open(); success = true; } catch(Exception error) { LogError(error); } return success; } public bool CloseDatabase() { bool success = false; try { this.oleCommand.Dispose(); this.sqlCommand.Dispose(); this.oleConnection.Close(); this.oleConnection.Dispose(); this.oleCommand = null; this.sqlCommand = null; this.oleConnection = null; success = true; } catch(Exception error) { LogError(error); } return success; } public int MultiExecuteNonQuerySQL(string sqlStr) { int count = -99; try { this.oleCommand = new OleDbCommand(sqlStr, this.oleConnection); count = this.oleCommand.ExecuteNonQuery(); } catch(Exception error) { LogError(error); } return count; } public DataTable MultiGetTableBySQL(string sqlStr) { DataTable dataTable = new DataTable(); try { this.sqlCommand = new OleDbDataAdapter(sqlStr,this.oleConnection); sqlCommand.Fill(dataTable); } catch(Exception error) { LogError(error); } return dataTable; } #endregion //finish only a sql in oracle #region don't need open database connection public static int ExecuteNonQuerySQL(string sqlStr) { int count = -99; try { using (OleDbConnection connection = new OleDbConnection(DBConnectionString)) { connection.Open(); using (OleDbCommand ocm=new OleDbCommand(sqlStr,connection) ) { count = ocm.ExecuteNonQuery(); } connection.Close(); } } catch(Exception error) { LogError(error); } return count; } public static DataTable GetTableBySQL(string sqlStr) { DataTable dataTable = new DataTable(); try { using (OleDbDataAdapter myCommand = new OleDbDataAdapter (sqlStr,DBConnectionString) ) { myCommand.Fill(dataTable); } } catch(Exception error) { LogError(error); } return dataTable; } #endregion } <body> <form id="Form1" method="post" runat="server"> <TABLE id="Table1" cellSpacing="0" cellPadding="0" width="100%" border="0" o> <TR id="history"> <TD style="WIDTH:150px" valign="top"><input style="border:solid 1px black;" type="button" value="View History" onclick="var tempC = document.getElementById('Label_History');if(tempC.style.display != 'none') {tempC.style.display = 'none';this.value='view history!'} else {tempC.style.display = '';this.value='hide history!'}"><input style="border:solid 1px black;" type="button" value="Clear History" onclick="document.getElementById('Label_History').innerHTML = '';document.getElementById('isClearHistory').value='clear';document.getElementById('Label_History').style.display='none';"><input type="hidden" value="" name="history" id="isClearHistory"></TD> <TD><div id="Label_History" runat="server" style="color:blue;display:none;"></div> </TD> </TR> <TR> <TD> connection string: </TD> <TD> <asp:TextBox CssClass="TextBox" id="TextBox_ConnStr" runat="server"></asp:TextBox></TD> </TR> <TR> <TD valign="top"> sql string : </TD> <TD> <asp:TextBox CssClass="TextArea" id="TextBox_SqlStr" runat="server" TextMode="MultiLine"></asp:TextBox></TD> </TR> <TR> <TD></TD> <TD> <asp:Button id="Button_Execute" runat="server" Text="Execute"></asp:Button></TD> </TR> <TR> <TD colspan="2"> <asp:Label id="Label_Message" runat="server" ForeColor="Red"></asp:Label></TD> </TR> <TR> <TD colSpan="2"> <asp:Panel id="Panel_Result" runat="server"></asp:Panel></TD> </TR> </TABLE> </form> </body> </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值