www.pudn.com > classifier.rar > sqlQuery.cs


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data.OleDb; 
using System.Data; 
 
namespace classifier 
{ 
	///  
	/// sqlQuery 的摘要说明。 
	///  
	public class sqlQuery : System.Windows.Forms.Form 
	{ 
		private System.Windows.Forms.TextBox cmdBox; 
		private System.Windows.Forms.Label label1; 
		private System.Windows.Forms.DataGrid resultGrid; 
		private System.Windows.Forms.Button queryBtn; 
		private System.Windows.Forms.TextBox errorBox; 
		///  
		/// 必需的设计器变量。 
		///  
		private System.ComponentModel.Container components = null; 
 
		public sqlQuery() 
		{ 
			// 
			// Windows 窗体设计器支持所必需的 
			// 
			InitializeComponent(); 
 
			// 
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
			// 
		} 
 
		///  
		/// 清理所有正在使用的资源。 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if(components != null) 
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region Windows 窗体设计器生成的代码 
		///  
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改 
		/// 此方法的内容。 
		///  
		private void InitializeComponent() 
		{ 
			this.cmdBox = new System.Windows.Forms.TextBox(); 
			this.label1 = new System.Windows.Forms.Label(); 
			this.resultGrid = new System.Windows.Forms.DataGrid(); 
			this.queryBtn = new System.Windows.Forms.Button(); 
			this.errorBox = new System.Windows.Forms.TextBox(); 
			((System.ComponentModel.ISupportInitialize)(this.resultGrid)).BeginInit(); 
			this.SuspendLayout(); 
			//  
			// cmdBox 
			//  
			this.cmdBox.Location = new System.Drawing.Point(112, 40); 
			this.cmdBox.Multiline = true; 
			this.cmdBox.Name = "cmdBox"; 
			this.cmdBox.Size = new System.Drawing.Size(344, 88); 
			this.cmdBox.TabIndex = 0; 
			this.cmdBox.Text = "textBox1"; 
			//  
			// label1 
			//  
			this.label1.Location = new System.Drawing.Point(24, 40); 
			this.label1.Name = "label1"; 
			this.label1.Size = new System.Drawing.Size(72, 23); 
			this.label1.TabIndex = 1; 
			this.label1.Text = "输入命令:"; 
			//  
			// resultGrid 
			//  
			this.resultGrid.DataMember = ""; 
			this.resultGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText; 
			this.resultGrid.Location = new System.Drawing.Point(16, 184); 
			this.resultGrid.Name = "resultGrid"; 
			this.resultGrid.Size = new System.Drawing.Size(880, 488); 
			this.resultGrid.TabIndex = 2; 
			//  
			// queryBtn 
			//  
			this.queryBtn.Location = new System.Drawing.Point(464, 72); 
			this.queryBtn.Name = "queryBtn"; 
			this.queryBtn.TabIndex = 3; 
			this.queryBtn.Text = "查询"; 
			this.queryBtn.Click += new System.EventHandler(this.queryBtn_Click); 
			//  
			// errorBox 
			//  
			this.errorBox.Location = new System.Drawing.Point(568, 40); 
			this.errorBox.Multiline = true; 
			this.errorBox.Name = "errorBox"; 
			this.errorBox.Size = new System.Drawing.Size(328, 88); 
			this.errorBox.TabIndex = 4; 
			this.errorBox.Text = "textBox1"; 
			//  
			// sqlQuery 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
			this.ClientSize = new System.Drawing.Size(912, 733); 
			this.Controls.Add(this.errorBox); 
			this.Controls.Add(this.queryBtn); 
			this.Controls.Add(this.resultGrid); 
			this.Controls.Add(this.label1); 
			this.Controls.Add(this.cmdBox); 
			this.Name = "sqlQuery"; 
			this.Text = "Sql查询"; 
			((System.ComponentModel.ISupportInitialize)(this.resultGrid)).EndInit(); 
			this.ResumeLayout(false); 
 
		} 
		#endregion 
 
		private void queryBtn_Click(object sender, System.EventArgs e) 
		{ 
			OleDbConnection sqlCon=db.dbOpertation.getConnection(); 
			sqlCon.Open(); 
			string cmd=cmdBox.Text; 
			DataTable dt=new DataTable(); 
			OleDbDataAdapter sqlAd=new OleDbDataAdapter(cmd,sqlCon); 
 
			try 
			{ 
				sqlAd.Fill(dt); 
				resultGrid.DataSource=dt; 
				errorBox.Text="查询完成"; 
			} 
			catch(Exception ex) 
			{ 
				errorBox.Text="出错了:"+ex.Message; 
 
				//MessageBox.Show("出错了!"); 
			} 
			sqlCon.Close(); 
 
		} 
	} 
}