用c#做的 sql查询工具的代码:
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 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
53 /**//// <summary>
54 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
55 /// 此方法的内容。
56 /// </summary>
57 private void InitializeComponent()
58 {
59 this.label1 = new System.Windows.Forms.Label();
60 this.txtSQL = new System.Windows.Forms.TextBox();
61 this.cmdExecute = new System.Windows.Forms.Button();
62 this.lvwDS = new System.Windows.Forms.ListView();
63 this.SuspendLayout();
64 //
65 // label1
66 //
67 this.label1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
68 this.label1.Location = new System.Drawing.Point(168, 8);
69 this.label1.Name = "label1";
70 this.label1.Size = new System.Drawing.Size(152, 23);
71 this.label1.TabIndex = 0;
72 this.label1.Text = "在下边写入SQL命令";
73 //
74 // txtSQL
75 //
76 this.txtSQL.Location = new System.Drawing.Point(16, 40);
77 this.txtSQL.Multiline = true;
78 this.txtSQL.Name = "txtSQL";
79 this.txtSQL.Size = new System.Drawing.Size(456, 96);
80 this.txtSQL.TabIndex = 1;
81 this.txtSQL.Text = "";
82 //
83 // cmdExecute
84 //
85 this.cmdExecute.Location = new System.Drawing.Point(200, 144);
86 this.cmdExecute.Name = "cmdExecute";
87 this.cmdExecute.TabIndex = 2;
88 this.cmdExecute.Text = "执行";
89 this.cmdExecute.Click += new System.EventHandler(this.cmdExecute_Click);
90 //
91 // lvwDS
92 //
93 this.lvwDS.GridLines = true;
94 this.lvwDS.Location = new System.Drawing.Point(16, 184);
95 this.lvwDS.Name = "lvwDS";
96 this.lvwDS.Size = new System.Drawing.Size(456, 184);
97 this.lvwDS.TabIndex = 3;
98 this.lvwDS.View = System.Windows.Forms.View.Details;
99 //
100 // Form1
101 //
102 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
103 this.ClientSize = new System.Drawing.Size(488, 373);
104 this.Controls.Add(this.lvwDS);
105 this.Controls.Add(this.cmdExecute);
106 this.Controls.Add(this.txtSQL);
107 this.Controls.Add(this.label1);
108 this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
109 this.Name = "Form1";
110 this.Text = "SQL Tool";
111 this.ResumeLayout(false);
112
113 }
114 #endregion
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
运行截图:
http://ezhihua.cnblogs.com/images/cnblogs_com/ezhihua/SQL_Tool.gif
我不明白的就在这里:
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 }特别是这句:148 itmX.Text=dr.GetValue(0).ToString(); 那个GetValue(0)到底是什么意思。希望哪位高手 ,能详细讲解一下145--154行。 大恩不言谢。 如果没有时间, 只要讲我最不明白的148 行那句也可以 谢谢。
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 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
53 /**//// <summary>
54 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
55 /// 此方法的内容。
56 /// </summary>
57 private void InitializeComponent()
58 {
59 this.label1 = new System.Windows.Forms.Label();
60 this.txtSQL = new System.Windows.Forms.TextBox();
61 this.cmdExecute = new System.Windows.Forms.Button();
62 this.lvwDS = new System.Windows.Forms.ListView();
63 this.SuspendLayout();
64 //
65 // label1
66 //
67 this.label1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
68 this.label1.Location = new System.Drawing.Point(168, 8);
69 this.label1.Name = "label1";
70 this.label1.Size = new System.Drawing.Size(152, 23);
71 this.label1.TabIndex = 0;
72 this.label1.Text = "在下边写入SQL命令";
73 //
74 // txtSQL
75 //
76 this.txtSQL.Location = new System.Drawing.Point(16, 40);
77 this.txtSQL.Multiline = true;
78 this.txtSQL.Name = "txtSQL";
79 this.txtSQL.Size = new System.Drawing.Size(456, 96);
80 this.txtSQL.TabIndex = 1;
81 this.txtSQL.Text = "";
82 //
83 // cmdExecute
84 //
85 this.cmdExecute.Location = new System.Drawing.Point(200, 144);
86 this.cmdExecute.Name = "cmdExecute";
87 this.cmdExecute.TabIndex = 2;
88 this.cmdExecute.Text = "执行";
89 this.cmdExecute.Click += new System.EventHandler(this.cmdExecute_Click);
90 //
91 // lvwDS
92 //
93 this.lvwDS.GridLines = true;
94 this.lvwDS.Location = new System.Drawing.Point(16, 184);
95 this.lvwDS.Name = "lvwDS";
96 this.lvwDS.Size = new System.Drawing.Size(456, 184);
97 this.lvwDS.TabIndex = 3;
98 this.lvwDS.View = System.Windows.Forms.View.Details;
99 //
100 // Form1
101 //
102 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
103 this.ClientSize = new System.Drawing.Size(488, 373);
104 this.Controls.Add(this.lvwDS);
105 this.Controls.Add(this.cmdExecute);
106 this.Controls.Add(this.txtSQL);
107 this.Controls.Add(this.label1);
108 this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
109 this.Name = "Form1";
110 this.Text = "SQL Tool";
111 this.ResumeLayout(false);
112
113 }
114 #endregion
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
运行截图:
http://ezhihua.cnblogs.com/images/cnblogs_com/ezhihua/SQL_Tool.gif
我不明白的就在这里:
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 }特别是这句:148 itmX.Text=dr.GetValue(0).ToString(); 那个GetValue(0)到底是什么意思。希望哪位高手 ,能详细讲解一下145--154行。 大恩不言谢。 如果没有时间, 只要讲我最不明白的148 行那句也可以 谢谢。
小弟还有一处不明就是在这里。134 SqlCommand cmd = cn.CreateCommand();
135 string strSQL = txtSQL.Text;
136 cmd.CommandText=strSQL;为什么 cmd命令可以这么创建。 我以前用的是 用 new 的那个。 这个是怎么回事?
146 {
147 itmX=new ListViewItem();//创建listview的item
148 itmX.Text=dr.GetValue(0).ToString();//getvalue(0)这个0是index,就是在这一行的位置(比如你的sql:select a ,b from c。getvalue(0)得到a,getvalue(1)得到b)
149 for(int i=1;i<dr.FieldCount;i++)//(循环读取行)
150 {
151 itmX.SubItems.Add(dr.GetValue(i).ToString());//在listviewitem里增加item(listviewitem相当于listview中得一行)
152 }
153 lvwDS.Items.Add(itmX);//把增加的listviewitem加进去
154 }
创建并返回一个与 SqlConnection 关联的 SqlCommand 对象。[Visual Basic]
Public Function CreateCommand() As SqlCommand
[C#]
public SqlCommand CreateCommand();
[C++]
public: SqlCommand* CreateCommand();
[JScript]
public function CreateCommand() : SqlCommand;
返回值
一个 SqlCommand 对象。要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版请参见
SqlConnection 类 | SqlConnection 成员 | System.Data.SqlClient 命名空间