The Flower of Evil
asp.net & XML Web Service

导航

 

代码:

  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7using System.Data.SqlClient;
  8
  9namespace Queries2
 10{
 11    /// <summary>
 12    /// Form1 的摘要说明。
 13    /// </summary>

 14    public class Form1 : System.Windows.Forms.Form
 15    {
 16        private System.Windows.Forms.Label label1;
 17        private System.Windows.Forms.TextBox txtSQL;
 18        private System.Windows.Forms.Button cmdExecute;
 19        private System.Windows.Forms.ListView lvwDS;
 20        /// <summary>
 21        /// 必需的设计器变量。
 22        /// </summary>

 23        private System.ComponentModel.Container components = null;
 24
 25        public Form1()
 26        {
 27            //
 28            // Windows 窗体设计器支持所必需的
 29            //
 30            InitializeComponent();
 31
 32            //
 33            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 34            //
 35        }

 36
 37        /// <summary>
 38        /// 清理所有正在使用的资源。
 39        /// </summary>

 40        protected override void Dispose( bool disposing )
 41        {
 42            if( disposing )
 43            {
 44                if (components != null
 45                {
 46                    components.Dispose();
 47                }

 48            }

 49            base.Dispose( disposing );
 50        }

 51
 52        Windows 窗体设计器生成的代码
115
116        /// <summary>
117        /// 应用程序的主入口点。
118        /// </summary>

119        [STAThread]
120        static void Main() 
121        {
122            Application.Run(new Form1());
123        }

124
125        private void cmdExecute_Click(object sender, System.EventArgs e)
126        {
127            
128                lvwDS.Columns.Clear();
129                lvwDS.Items.Clear();
130                SqlConnection cn = new SqlConnection(@"Data Source=localhost;Integrated Security=SSPI;database=Northwind");
131             try
132               {
133                cn.Open();
134                SqlCommand cmd = cn.CreateCommand();
135                string strSQL = txtSQL.Text;
136                cmd.CommandText=strSQL;
137                SqlDataReader dr=cmd.ExecuteReader();
138                for(int i=0;i<dr.FieldCount;i++)
139                {
140                    ColumnHeader ch = new ColumnHeader();
141                    ch.Text=dr.GetName(i);
142                    lvwDS.Columns.Add(ch);
143                }

144                ListViewItem itmX;
145                while(dr.Read())
146                {
147                    itmX=new ListViewItem();
148                    itmX.Text=dr.GetValue(0).ToString();
149                    for(int i=1;i<dr.FieldCount;i++)
150                    {
151                        itmX.SubItems.Add(dr.GetValue(i).ToString());
152                    }

153                    lvwDS.Items.Add(itmX);
154                }

155                dr.Close();
156                cn.Close();
157            }

158            catch(System.Data.SqlClient.SqlException e1)
159            {
160                MessageBox.Show("在SQL命令里有错误"+"\n错误信息:"+e1.Message,"SQL");
161            }

162            finally
163            {
164                cn.Close();
165            }

166        }

167    }

168}

169

运行截图:
SQL_Tool.gif
posted on 2005-11-13 12:31  The Flower of Evil  阅读(1985)  评论(2)    收藏  举报