DataTable自身的查询 和 排序

以前在做信息管理类系统的时候经常遇到查询问题,都是每次查询直接到数据库里查询,

这样往往增大了数据的访问量,增大了数据库的压力,而且编程时会忽略这里的安全处理

问题,比如在这里的SQL注入.
这里介绍使用 DataTable自身的查询功能,一次查询后,就在数据库外查询.

 

核心代码:

  1.             DataTable dt = new DataTable();
  2.             //这里给dt表格绑定数据,这里我就自己构建dt,实际编程时,一班都是在窗体或页面记载是用查询数据库先初始化dt
  3.             dt.Columns.Add(new DataColumn("ID"));
  4.             dt.Columns.Add(new DataColumn("name"));
  5.             DataRow dr0 = dt.NewRow();
  6.             dr0[0] = "1";
  7.             dr0[1] = "张三";
  8.             DataRow dr1 = dt.NewRow();
  9.             dr1[0] = "2";
  10.             dr1[1] = "李四";
  11.             DataRow dr2 = dt.NewRow();
  12.             dr2[0] = "3";
  13.             dr2[1] = "李五";
  14.             dt.Rows.Add(dr0);
  15.             dt.Rows.Add(dr1);
  16.             dt.Rows.Add(dr2);
  17.             //表格数据绑定结束
  18.             string  expression = "name like '%"+textBox1.Text.Trim()+"%' and ID=2";
  19.             DataView dv = dt.DefaultView;
  20.             dv.Sort = "ID desc";
  21.             dv.RowFilter=expression;
  22.            
  23.             //此后就可以把dv的数据绑定给窗体或页面控件了
  24.             //例如 dataGridView1.DataSource=dv;

实例: (windows应用程序)

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace WindowsApplication3
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         DataTable dt = new DataTable();
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.             
  20.             string  expression = "name like '%"+textBox1.Text.Trim()+"%' and ID=2";
  21.             DataView dv = dt.DefaultView;
  22.             dv.Sort = "ID desc";
  23.             dv.RowFilter=expression;
  24.            
  25.             dataGridView1.DataSource=dv;
  26.         }
  27.         
  28.         private void Form1_Load(object sender, EventArgs e)
  29.         {
  30.             
  31.             dt.Columns.Add(new DataColumn("ID"));
  32.             dt.Columns.Add(new DataColumn("name"));
  33.             DataRow dr0 = dt.NewRow();
  34.             dr0[0] = "1";
  35.             dr0[1] = "张三";
  36.             DataRow dr1 = dt.NewRow();
  37.             dr1[0] = "2";
  38.             dr1[1] = "李四";
  39.             DataRow dr2 = dt.NewRow();
  40.             dr2[0] = "3";
  41.             dr2[1] = "李五";
  42.             dt.Rows.Add(dr0);
  43.             dt.Rows.Add(dr1);
  44.             dt.Rows.Add(dr2);
  45.             dataGridView1.DataSource = dt.DefaultView;
  46.         }
  47.     }
  48. }

窗体设计代码

  1. namespace WindowsApplication3
  2. {
  3.     partial class Form1
  4.     {
  5.         /// <summary>
  6.         /// 必需的设计器变量。
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.         /// <summary>
  10.         /// 清理所有正在使用的资源。
  11.         /// </summary>
  12.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13.         protected override void Dispose(bool disposing)
  14.         {
  15.             if (disposing && (components != null))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.             base.Dispose(disposing);
  20.         }
  21.         #region Windows 窗体设计器生成的代码
  22.         /// <summary>
  23.         /// 设计器支持所需的方法 - 不要
  24.         /// 使用代码编辑器修改此方法的内容。
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.button1 = new System.Windows.Forms.Button();
  29.             this.textBox1 = new System.Windows.Forms.TextBox();
  30.             this.dataGridView1 = new System.Windows.Forms.DataGridView();
  31.             ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
  32.             this.SuspendLayout();
  33.             // 
  34.             // button1
  35.             // 
  36.             this.button1.Location = new System.Drawing.Point(312, 44);
  37.             this.button1.Name = "button1";
  38.             this.button1.Size = new System.Drawing.Size(75, 23);
  39.             this.button1.TabIndex = 1;
  40.             this.button1.Text = "button1";
  41.             this.button1.UseVisualStyleBackColor = true;
  42.             this.button1.Click += new System.EventHandler(this.button1_Click);
  43.             // 
  44.             // textBox1
  45.             // 
  46.             this.textBox1.Location = new System.Drawing.Point(159, 46);
  47.             this.textBox1.Name = "textBox1";
  48.             this.textBox1.Size = new System.Drawing.Size(100, 21);
  49.             this.textBox1.TabIndex = 2;
  50.             // 
  51.             // dataGridView1
  52.             // 
  53.             this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  54.             this.dataGridView1.Location = new System.Drawing.Point(181, 87);
  55.             this.dataGridView1.Name = "dataGridView1";
  56.             this.dataGridView1.RowTemplate.Height = 23;
  57.             this.dataGridView1.Size = new System.Drawing.Size(240, 150);
  58.             this.dataGridView1.TabIndex = 3;
  59.             // 
  60.             // Form1
  61.             // 
  62.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  63.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  64.             this.ClientSize = new System.Drawing.Size(683, 266);
  65.             this.Controls.Add(this.dataGridView1);
  66.             this.Controls.Add(this.textBox1);
  67.             this.Controls.Add(this.button1);
  68.             this.Name = "Form1";
  69.             this.Text = "Form1";
  70.             this.Load += new System.EventHandler(this.Form1_Load);
  71.             ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
  72.             this.ResumeLayout(false);
  73.             this.PerformLayout();
  74.         }
  75.         #endregion
  76.         private System.Windows.Forms.Button button1;
  77.         private System.Windows.Forms.TextBox textBox1;
  78.         private System.Windows.Forms.DataGridView dataGridView1;
  79.     }
  80. }

注意点:
 DataTable dt = new DataTable();

是定义的窗体类Form1的变量,而不是某个事件,函数的变量.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值