自动生成SQL语句(Oracle)

 

Posted on 2006-06-01 10:11 做回自己 阅读(814) 评论(3)   编辑  收藏 引用 网摘 所属分类: C# 414577.html?webview=1
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.Data;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  Cemic.Dal;
None.gif
None.gif
namespace  Cemic.Bll
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 自动生成SQL语句 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public abstract class BuildSql
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected static string str_TableName;        // 数据库表名
InBlock.gif
        protected static string str_KeyField;        // 关键字
InBlock.gif
        protected static string str_KeyValue;        // 关键字的值        
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public BuildSql()dot.gif{}        
InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取INSERT语句#region 获取INSERT语句
InBlock.gif        
public string GetInsertSql(HtmlForm Form1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 获取当前要新增记录的字段及值
InBlock.gif
            Hashtable htFields = GetFieldsList(Form1);
InBlock.gif            
// 获取表中各字段类型
InBlock.gif
            Hashtable htType = GetFieldsType();
InBlock.gif            
// 申明一个存放新增记录各字段的字符串
InBlock.gif
            string str_Fields = string.Empty;
InBlock.gif            
// 申明一个存放新增记录各字段值的字符串
InBlock.gif
            string str_Values = string.Empty;
InBlock.gif
InBlock.gif            
// 生成新增语句
InBlock.gif
            IDictionaryEnumerator enumeratorField = htFields.GetEnumerator();
InBlock.gif            
// 遍历Hashtable表,对要更新操作的各字段进行类型判断
InBlock.gif
            while(enumeratorField.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(htType.ContainsKey(enumeratorField.Key))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Type type 
= htType[enumeratorField.Key.ToString()] as Type;
InBlock.gif
InBlock.gif                    str_Fields 
+= "," + enumeratorField.Key.ToString();
InBlock.gif                    
InBlock.gif                    str_Values 
+= "," + GetByTypeString(type, enumeratorField.Value.ToString());
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
    
InBlock.gif            
// 添加关键字
InBlock.gif
            if(str_KeyField != string.Empty && str_KeyValue != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                str_Fields 
= str_KeyField  + str_Fields;
InBlock.gif                str_Values 
= str_KeyValue + str_Values;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                str_Fields 
= str_Fields.Substring(1, str_Fields.Length-1);
InBlock.gif                str_Values 
= str_Values.Substring(1, str_Values.Length-1);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string cmdText = "Insert into {0}({1}) Values({2})";
InBlock.gif            cmdText 
= string.Format(cmdText, str_TableName, str_Fields, str_Values);
InBlock.gif            
return cmdText;            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取UPDATE语句#region 获取UPDATE语句
InBlock.gif        
public string GetUpdateSql(HtmlForm Form1, object obj_KeyValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 获取当前要更新的字段及值
InBlock.gif
            Hashtable htFields = GetFieldsList(Form1);
InBlock.gif            
// 获取表中各字段类型
InBlock.gif
            Hashtable htType = GetFieldsType();
InBlock.gif            
// 申明一个存放更新操作的的字符串
InBlock.gif
            string str_Fields = string.Empty;
InBlock.gif            
// 申明一个存放条件的字符串
InBlock.gif
            string str_Where = string.Empty;
InBlock.gif
InBlock.gif            
// 生成更新语句
InBlock.gif
            IDictionaryEnumerator enumeratorField = htFields.GetEnumerator();
InBlock.gif            
// 遍历Hashtable表,对要更新操作的各字段进行类型判断
InBlock.gif
            while(enumeratorField.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(htType.ContainsKey(enumeratorField.Key))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Type type 
= htType[enumeratorField.Key.ToString()] as Type;
InBlock.gif                    str_Fields 
+= "," + enumeratorField.Key.ToString() + "=";
InBlock.gif
InBlock.gif                    str_Fields 
+= GetByTypeString(type, enumeratorField.Value.ToString());                    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            str_Fields 
= str_Fields.Substring(1, str_Fields.Length-1);
InBlock.gif            
InBlock.gif            
// 生成更新操作的条件;
InBlock.gif
            if(htType.ContainsKey(str_KeyField))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                str_Where 
= str_KeyField + "=";
InBlock.gif                Type type 
= htType[str_KeyField] as Type;
InBlock.gif
InBlock.gif                str_Where 
+= GetByTypeString(type, obj_KeyValue.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw(new Exception("您操作的表有错误"));
ExpandedSubBlockEnd.gif            }
            
InBlock.gif
InBlock.gif            
string cmdText = "Update {0} Set {1} Where {2}";
InBlock.gif            cmdText 
= string.Format(cmdText, str_TableName, str_Fields, str_Where);
InBlock.gif            
return cmdText;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取DELETE语句#region 获取DELETE语句
InBlock.gif        
public string GetDeleteSql(object obj_KeyValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 获取表中各字段的类型
InBlock.gif
            Hashtable htType = GetFieldsType();
InBlock.gif            
// 申明一个存放条件的字符串
InBlock.gif
            string str_Where = string.Empty;
InBlock.gif
InBlock.gif            
// 生成删除操作的条件
InBlock.gif
            if(htType.ContainsKey(str_KeyField))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                str_Where 
= str_KeyField + "=";
InBlock.gif                Type type 
= htType[str_KeyField] as Type;
InBlock.gif
InBlock.gif                str_Where 
+= GetByTypeString(type, obj_KeyValue.ToString());                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw(new Exception("您操作的表有错误"));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string cmdText = "Delete {0} Where {1}";
InBlock.gif            cmdText 
= string.Format(cmdText, str_TableName, str_Where);
InBlock.gif            
return cmdText;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取类型字符串#region 获取类型字符串
InBlock.gif        
private string GetByTypeString(Type type, string str_Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string str_Tmp = string.Empty;
InBlock.gif
InBlock.gif            
// 对类型进行比较返回相对应的字符串
InBlock.gif
            if(type == typeof(string))                                    
InBlock.gif                str_Tmp 
= "'" + str_Value + "'";    
InBlock.gif            
else if(type == typeof(Decimal))
InBlock.gif                str_Tmp 
= str_Value;
InBlock.gif            
else if(type == typeof(DateTime))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(str_Value.IndexOf(":"> 0)
InBlock.gif                    str_Tmp 
= "To_Date('" + str_Value + "','yyyy-mm-dd hh24:mi:ss')";
InBlock.gif                
else
InBlock.gif                    str_Tmp 
= "To_Date('" + str_Value + "','yyyy-mm-dd')";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
// 返回类型字符串
InBlock.gif
            return str_Tmp;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取所要的字段及值#region 获取所要的字段及值
InBlock.gif        
private Hashtable GetFieldsList(HtmlForm Form1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Hashtable Fields 
= new Hashtable();
InBlock.gif            
InBlock.gif            
for(int i=0; i<Form1.Controls.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(Form1.Controls[i] is TextBox)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// 转换控件
InBlock.gif
                    TextBox txtControl = Form1.Controls[i] as TextBox;
InBlock.gif                    Fields.Add(txtControl.ID, txtControl.Text);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if(Form1.Controls[i] is HtmlInputText)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    HtmlInputText txtControl 
= Form1.Controls[i] as HtmlInputText;
InBlock.gif                    Fields.Add(txtControl.ID, txtControl.Value);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return Fields;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取所要的字段及类型#region 获取所要的字段及类型
InBlock.gif        
private Hashtable GetFieldsType()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string cmdText = "select * From " + str_TableName + " Where rownum <=1";
InBlock.gif
InBlock.gif            SqlTextAction sqlAction 
= new SqlTextAction();
InBlock.gif            DataSet ds 
= sqlAction.SelectByWhere(cmdText);
InBlock.gif            sqlAction 
= null;            
InBlock.gif            DataColumnCollection dc 
= ds.Tables[0].Columns;
InBlock.gif
InBlock.gif            Hashtable Fields 
= new Hashtable();
InBlock.gif        
InBlock.gif            
for(int i=0; i<dc.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 保存字段类型
InBlock.gif
                Fields.Add(dc[i].Caption, dc[i].DataType);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
return Fields;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
    
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class BuildSqlByArticle:BuildSql
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public BuildSqlByArticle()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            str_TableName 
= "DIA_WEB_ARTICLE";
InBlock.gif            str_KeyField 
= "AR_ID";
InBlock.gif            str_KeyValue 
= "DIA_WEB_AR_ID.Nextval";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/meiproject/archive/2007/04/27/729667.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值