我写了一个简单的,你自己扩展一下,主要是补充一下sql 的关键字和自己定一下颜色就可以。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text.RegularExpressions;namespace TheAres.SqlHighLightText
{
/// <summary>
/// Form4 摘要说明。
/// </summary>
public class Form4 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox sqlRichTextBox;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form4()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlRichTextBox = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// sqlRichTextBox
// 
this.sqlRichTextBox.Location = new System.Drawing.Point(56, 32);
this.sqlRichTextBox.Name = "sqlRichTextBox";
this.sqlRichTextBox.Size = new System.Drawing.Size(304, 208);
this.sqlRichTextBox.TabIndex = 0;
this.sqlRichTextBox.Text = "select * from table1";
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(408, 64);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form4
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 294);
this.Controls.Add(this.button1);
this.Controls.Add(this.sqlRichTextBox);
this.Name = "Form4";
this.Text = "Form4";
this.ResumeLayout(false); }
#endregion public static void Main()
{
Application.Run( new Form4() );
} private void HighLightText()
{
string[] keywords = {"select","distinct","from",
"where","order","by","group",
"sum"," is ","null","isnull"};
                         
string[] functions = {"isnull","count","sum"};
string[] strings = {@"'((.|\n)*?)'"};
string[] whiteSpace = {"\t","\n"," "}; this.sqlRichTextBox.SelectAll();
this.sqlRichTextBox.SelectionColor = Color.Black; this.HighLightText(keywords,Color.Blue);
this.HighLightText(functions,Color.Magenta);
this.HighLightText(strings,Color.Red);
this.HighLightText(whiteSpace, Color.Black);
} private void button1_Click(object sender, System.EventArgs e)
{
HighLightText();

}
private void HighLightText(string[] wordList, Color color)
{
foreach (string word in wordList)
{
Regex r = new Regex(word,RegexOptions.IgnoreCase);
           
foreach(Match m in r.Matches( this.sqlRichTextBox.Text ) )
{
this.sqlRichTextBox.Select(m.Index,m.Length);
this.sqlRichTextBox.SelectionColor = color;
}
}
}
}
}