动态SQL生成器(Dynamic SQL Generator)
动态SQL生成器(Dynamic SQL Generator) from www.codeproject.com

Summary
Dynamic Generator is a simple Windows application to generate dynamic SQL Stored Procedures on the fly, easily and error free; just enter the server name with authentication, select the database, point to the table to work with, press ‘Generate’, and it’s done.
摘要
动态生成器是个简单的Windows应用程序,用于动态生成SQL存储过程,即简单又无差错,只需输入服务器名称,选择授权方式,选择数据库,指向一个工作表,按’生成’就全搞定了!。
Introduction
Very often, when I’m asked to develop a data driven application or a CMS, I’m asked to implement an “Advanced Search” feature; this can be done using Dynamic SQL.
Although a more efficient way will be using the COALESCE function, dynamic SQL is the solution for doing “All words”, “Any word”, and “Exact Match” string searches.
Writing these statements can be tiresome, note that (unlike usual Stored Procedures) you may need more than one variable for the same field, .i.e., if it’s a range field (date or money), you will need two variables, from and to; if it’s a string variable (varchar, nvarchar, etc.), you will need five variables: four words the user entered, and a flag to check which search type was selected (“All words”, “Any word”, or “Exact Match”).
介绍
经常这样:当我想开发数据驱动应用程序或CMS(Content Management System的缩写,意为“内容管理系统”。)我就想实现一个"高级搜索"功能可以使用Dynamic SQL完成。
虽然更有效的方式可以使用COALESCE函数,但是动态SQL处理如:“All words”,“Any word”和“Exact Match”字符串搜索功能是较好的解决方案.
写这些语句是很讨厌的,请注意(不同于通常的存储过程),你可能需要一个以上的同一字段变量。也就是说,假设字段范围(Date 或 Money类型)需要定义2个变量,由此类推,如果是字符串变量(varchar,nvarchar,等等)你需要定义5个变量:4个用户输入的关键字和一个选取(“All words”, “Any word”, 或 “Exact Match”)搜索类型的标记.
How the code works
Dictionary<string, string> dict_Fields is a dictionary holding the field name as a key, and the field type as value. When the user selects a table, we have to get all the fields within it.
程序如何工作
Dictionary<string, string> dict_Fields 字典类型变量用于保存字段信息,字段名是字典的主键,字段类型是对应的值. 当用户选择一
个数据表,获取所有字段信息保存在该字典内。
选择数据表,获取所有的字段信息:
private void lstbx_Tables_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstbx_Tables.SelectedIndex < 0)
return;
dict_Fields = new Dictionary<string, string>();
cmb_Fields.Items.Clear();
using (SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
// get fields’ names and type of selected table
string strCommand = @"SELECT COLUMN_NAME, DATA_TYPE FROM " +
@"INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = ’" +
lstbx_Tables.SelectedItem.ToString() + "’";
SqlCommand cmd = new SqlCommand(strCommand, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//add to dict_Fields dictionary and fill the "Orderby" combox
dict_Fields.Add(dr.GetString(0), dr.GetString(1));
cmb_Fields.Items.Add(dr.GetString(0));
}
dr.Close();
conn.Close();
}
cmb_Fields.SelectedIndex = 0;
}
www.csframework.com 翻译
原文转自codeproject,转载请注明出处.
http://www.codeproject.com/KB/database/Generate_Dyanmic_SQL.aspx
WinFramework轻量级开发框架 | 代码生成器(CodeGenerator)简介|C/S开发框架
DbCommand生成器测试案例 | DbCommandGenerator.cs|C/S开发框架
SQL命令生成器:GenerateSqlCmdByTableFields,GenerateSqlCmdByObjectClass
ClassGenerator(ORM生成器)下载
代码生成器 CodeGenerator V6.1 | 提供两种生成报表策略(表格/标签,TableObject/TextObject)|C/S开发框架
CSFramework.CodeGeneratorV6.0|代码生成器测试案例|C/S开发框架
CSFramework.CodeGenerator V6.0 代码生成器下载|C/S开发框架
CSFramework.CodeGeneratorV6 代码生成器|C/S开发框架
EntityFramework通用DbContext类,支持动态实体模型,多数据库(MySQL,SqlServer,Oracle)
C# 动态组合SQL脚本LIKE语句及查询参数防SQL注入攻击
C# 动态对象转换为实体对象支持泛型(DynamicToEntity)
C/S系统快速开发平台SQL命令生成器(数据自动更新模型)
C#.NET 定义动态对象dynamic以及转换JSON字符串
C#.NET 定义动态对象dynamic以及属性和方法
如何跟踪SQL命令生成器生成的代码
所有内容全部开放源码? 源码生成器、SQL生成器也是开放源码的?
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 |
在线客服
