动态SQL生成器(Dynamic SQL Generator)
作者:C/S框架网  发布日期:2011/06/16 22:39:14
动态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


点击下载附件 点击下载附件 (如下载失败,请邮件通知我们寄回给您,或QQ:23404761留言.)
上一篇 下一篇